这个 struts 2 教程解释了如何在你的动态 web 应用程序中集成 Jasper iReport
来生成 PDF 报告。在开始编码之前,我们需要在 Struts 2 项目中包含 Jasper iReport JAR 依赖项。
Struts 2 和 Jasper iReport 集成需要这些 jar 文件。
有时先写哪个代码或如何开始编码很重要?要让您的代码无错误,请按照以下步骤操作……
步骤- 1: 第一步是创建表脚本。
CREATE TABLE `pdfreport` (
`NAME` varchar(20) DEFAULT NULL,
`EMAIL` varchar(50) DEFAULT NULL,
`MOBILE` varchar(12) DEFAULT NULL,
`COLLEGE` varchar(50) DEFAULT NULL,
`COURSE` varchar(10) DEFAULT NULL,
`SKILLS` varchar(100) DEFAULT NULL,
`MARKS1` int(3) DEFAULT NULL,
`MARKS2` int(3) DEFAULT NULL,
`MARKS3` int(3) DEFAULT NULL
);
第 2 步: 设计您的报告。
学生报告.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
name="studentReport" pageWidth="595" pageHeight="842" columnWidth="555"
leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
uuid="ba63635c-ad40-41b0-9974-2f6d4d62ac4e">
<property name="ireport.zoom" value="1.0" />
<property name="ireport.x" value="0" />
<property name="ireport.y" value="0" />
<parameter name="logo" class="java.lang.String" />
<queryString>
<![CDATA[]]>
</queryString>
<field name="NAME" class="java.lang.String">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="email" class="java.lang.String">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="mobile" class="java.lang.String">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="college" class="java.lang.String">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="course" class="java.lang.String">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="skills" class="java.lang.String">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="marks1" class="java.lang.Integer">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="marks2" class="java.lang.Integer">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<field name="marks3" class="java.lang.Integer">
<fieldDescription><![CDATA[]]>
</fieldDescription>
</field>
<background>
<band splitType="Stretch" />
</background>
<title>
<band height="79" splitType="Stretch">
<image>
<reportElement x="0" y="0" width="208" height="59"
uuid="5dfbe355-b454-49e2-a674-d44dac7559c1" />
<imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>
<line>
<reportElement x="0" y="69" width="552" height="2"
forecolor="#FFFF00" uuid="fb69bdb0-2581-4077-9048-32a081feac19" />
</line>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="552"
height="35" backcolor="#CCCCCC" uuid="0baae80b-49e8-4cc1-84e5-84c4dc4deccc" />
<textElement textAlignment="Center">
<font size="26" />
</textElement>
<text><![CDATA[Student Reports]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="41" splitType="Stretch" />
</columnHeader>
<detail>
<band height="123" splitType="Stretch">
<textField>
<reportElement x="0" y="71" width="185" height="20"
uuid="45be6758-b007-4fc8-bed2-d805dd1212a1" />
<box>
<pen lineWidth="0.25" />
<topPen lineWidth="0.25" />
<leftPen lineWidth="0.25" />
<bottomPen lineWidth="0.25" />
<rightPen lineWidth="0.25" />
</box>
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{marks1}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="71" width="185" height="20"
uuid="e2900688-a1e6-4bd6-824f-1fec11357fa2" />
<box>
<pen lineWidth="0.25" />
<topPen lineWidth="0.25" />
<leftPen lineWidth="0.25" />
<bottomPen lineWidth="0.25" />
<rightPen lineWidth="0.25" />
</box>
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{marks2}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="370" y="71" width="185" height="20"
uuid="e948ed81-1b35-4801-972a-cb3721978c6b" />
<box>
<pen lineWidth="0.25" />
<topPen lineWidth="0.25" />
<leftPen lineWidth="0.25" />
<bottomPen lineWidth="0.25" />
<rightPen lineWidth="0.25" />
</box>
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{marks3}]]></textFieldExpression>
</textField>
<staticText>
<reportElement mode="Opaque" x="0" y="51" width="185"
height="20" backcolor="#CCCCCC" uuid="1d29d647-fff0-4523-b12e-a9b9ee1e7f85" />
<box>
<pen lineWidth="0.25" />
<topPen lineWidth="0.25" />
<leftPen lineWidth="0.25" />
<bottomPen lineWidth="0.25" />
<rightPen lineWidth="0.25" />
</box>
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[Subject 1]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="185" y="51" width="185"
height="20" backcolor="#CCCCCC" uuid="be9225ad-4eea-49a9-aa82-20ee92ad365c" />
<box>
<pen lineWidth="0.25" />
<topPen lineWidth="0.25" />
<leftPen lineWidth="0.25" />
<bottomPen lineWidth="0.25" />
<rightPen lineWidth="0.25" />
</box>
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[Subject 2]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="370" y="51" width="185"
height="20" backcolor="#CCCCCC" uuid="ccd258aa-657e-44d3-8462-79c384616791" />
<box>
<pen lineWidth="0.25" />
<topPen lineWidth="0.25" />
<leftPen lineWidth="0.25" />
<bottomPen lineWidth="0.25" />
<rightPen lineWidth="0.25" />
</box>
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[Subject 3]]></text>
</staticText>
<textField>
<reportElement x="0" y="20" width="92" height="20"
uuid="fc8f42e0-0ae0-493e-9a4f-3e695d71be7e" />
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="92" y="20" width="92" height="20"
uuid="a56767ed-655c-4cb4-b9b2-3a0bfb1a0a26" />
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="184" y="20" width="92" height="20"
uuid="193b7881-25dc-445f-af6a-f26232466fa3" />
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{mobile}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="276" y="20" width="92" height="20"
uuid="abc9a75c-5118-4172-be15-7ad75414095d" />
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{college}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="368" y="20" width="92" height="20"
uuid="2b350d01-3274-43ae-8bd4-c3feaf277b69" />
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{course}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="460" y="20" width="92" height="20"
uuid="1239ea7f-5ca4-4aa4-80d3-9d1f4c3dad06" />
<textElement textAlignment="Center" />
<textFieldExpression><![CDATA[$F{skills}]]></textFieldExpression>
</textField>
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="92"
height="20" backcolor="#CCCCCC" uuid="a0f72a52-fdac-49cd-9845-ea82ca2b7d93" />
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[NAME]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="92" y="0" width="92"
height="20" backcolor="#CCCCCC" uuid="0d30c4d9-52ed-4965-815e-4fa98dc98b87" />
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[Email]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="184" y="0" width="92"
height="20" backcolor="#CCCCCC" uuid="68f75f19-987f-4f38-a307-38f35a819608" />
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[Mobile]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="276" y="0" width="92"
height="20" backcolor="#CCCCCC" uuid="ed266987-3428-446d-881d-ba7057107025" />
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[College]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="368" y="0" width="92"
height="20" backcolor="#CCCCCC" uuid="12906bc7-1143-4064-bf6c-fc2c38c9b58c" />
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[Course]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="460" y="0" width="92"
height="20" backcolor="#CCCCCC" uuid="ebba8c0c-fcfc-424d-844c-8d5ca576b05d" />
<textElement textAlignment="Center">
<font isBold="true" />
</textElement>
<text><![CDATA[Skills]]></text>
</staticText>
<line>
<reportElement x="0" y="107" width="552" height="1"
uuid="2221b6a3-4dbc-4719-ac8b-14e9313f4463" />
</line>
</band>
</detail>
<columnFooter>
<band splitType="Stretch" />
</columnFooter>
<pageFooter>
<band height="1" splitType="Stretch" />
</pageFooter>
<summary>
<band splitType="Stretch" />
</summary>
</jasperReport>
步骤– 3: 在项目 lib
文件夹中包含所有必需的 jar。
步骤– 4: 在 web.xml
中添加 Struts 2 过滤器
web.xml
<web-app>
<display-name>Struts2JasperReportIntegration</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
步骤 – 5: 创建 a DAO
类。
报告DAO.java
package org.websparrow;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ReportDAO {
public ResultSet getPdf() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
String query = "SELECT NAME, email, mobile, college, course, skills, marks1, marks2, marks3 FROM pdfreport";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
return rs;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
**步骤– 6:**创建Action
类。
ReportAction.java
package org.websparrow;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.ResultSet;
import java.util.HashMap;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
public class ReportAction extends ActionSupport {
public ResultSet rs = null;
public String submit = null;
public InputStream fileInputStream;
public String jasperPath = "";
public String pdfName = "";
public String rpt = "";
//Generate Getters and Setters...
@Override
public String execute() throws Exception {
try {
if (submit.equals("pdf")) {
rs = new ReportDAO().getPdf();
jasperPath = ServletActionContext.getServletContext().getRealPath("/Reports");
pdfName = "Student Report";
rpt = "studentReport.jrxml";
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
HashMap<String, Object> pm = new HashMap<String, Object>();
String logo = jasperPath + "/ws.jpg";
pm.put("logo", logo);
JasperReport jr = JasperCompileManager.compileReport(jasperPath + "/" + rpt);
JasperPrint jp = JasperFillManager.fillReport(jr, pm, resultSetDataSource);
JasperExportManager.exportReportToPdfFile(jp, jasperPath + pdfName + ".pdf");
fileInputStream = new FileInputStream(new File(jasperPath + pdfName + ".pdf"));
}
} catch (Exception e) {
e.printStackTrace();
}
return "SUCCESS";
}
}
第 7 步: 设计您的 User Interface
页面。
索引.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<style type="text/css">
button {
background-color: #008CBA;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 12px;
}
</style>
</head>
<body style="text-align: center;">
<h1>Struts2 and Jasper iReport Integration Example</h1>
<form method="POST" name="rpt" action="StudentReport.action">
<button type="submit" name="submit" value="pdf">Generate Report</button>
</form>
</body>
</html>
步骤– 8: 映射 struts.xml
中的 Action 类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default" namespace="/">
<action name="StudentReport" class="org.websparrow.ReportAction">
<result name="SUCCESS" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${pdfName}.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
第 9 步: 最后一切都完成了,现在启动您的服务器并运行项目。
输出 :
找到用于测试应用程序的 URL localhost:8080/Struts2JasperReportIntegration/
。我们将得到如下所示的输出。
Screen 1
Screen 2
Screen 3
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.websparrow.org/struts/struts2-and-jasper-ireport-integration-example
内容来源于网络,如有侵权,请联系作者删除!