使用Struts2框架的Jasper报告 [英] Jasper Report with Struts2 Framework
问题描述
我在 Struts2
框架中使用 Jasper
报告创建了一个应用程序。
I've created an application in Struts2
framework with Jasper
reporting.
在单击按钮的索引页面中,我将以PDF格式显示报告。
In the index page when I click a button then I will get report as pdf displayed.
但是我非常关心我的实现。实现我所做的是正确的方法吗?
But I'm very much concerned about my implementation. Does the implementation what I did is the correct way?
因为我在几个教程中注意到在struts2中调用jasper有点像:
Since I noticed in several tutorials that calling jasper in struts2 is somewhat like:
<action name="myJasperTest" class="com.acme.test.action.JasperAction">
<result name="success" type="jasper">
<param name="location">/jasper/our_compiled_template.jasper</param>
<param name="dataSource">myList</param>
<param name="format">PDF</param>
</result>
</action>
但我不知道如何通过上述方式将参数传递给查询?
任何人都可以告诉我一些解决方案或建议
but i don't know how to pass parameter to query with the above way? can anyone please tell me some solutions or suggestions
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<s:form action="getUserDetails" method="post">
<s:submit value="Show" onclick="this.form.target='_blank';return true;"/>
</s:form>
</body>
</html>
JasperReport.java
package commonpackage;
import com.opensymphony.xwork2.ActionSupport;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import org.apache.struts2.ServletActionContext;
public class JasperReport extends ActionSupport
{
private String userid="1";
public InputStream inputStream;
public String getUserDetails() throws ClassNotFoundException{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ortms", "root", "");
String str;
str = ServletActionContext.getServletContext().getRealPath("/reports/myreport.jrxml");
JasperDesign jd=JRXmlLoader.load(str);
Map parameters = new HashMap();
parameters.put("userid", userid);
net.sf.jasperreports.engine.JasperReport jr = JasperCompileManager.compileReport(jd);
JasperPrint jp=JasperFillManager.fillReport(jr, parameters,cn);
JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, ServletActionContext.getServletContext().getRealPath("/reports/myreport.pdf"));
exporter.exportReport();
File file = new File(ServletActionContext.getServletContext().getRealPath("/reports/myreport.pdf"));
inputStream = new DataInputStream( new FileInputStream(file));
return SUCCESS;
} catch (Exception e) {
System.out.println("Exception in getUserDetails in JasperReport class");
e.printStackTrace();
}
return SUCCESS;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public InputStream getInputStream() {
return inputStream;
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="example.xml"/>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="getUserDetails" class="commonpackage.JasperReport" method="getUserDetails">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">inline;filename="UserReport.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
myreport.jrmxl
<?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="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="70b2040b-a566-48b5-8e8e-a01d402007e3">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="userid" class="java.lang.String">
<defaultValueExpression><![CDATA[0]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT
table_users.`id` AS table_users_id,
table_users.`user_code` AS table_users_user_code,
table_users.`first_name` AS table_users_first_name,
table_users.`last_name` AS table_users_last_name
FROM
`table_users` table_users WHERE table_users.`id`=$P{userid}]]>
</queryString>
<field name="table_users_id" class="java.lang.Integer"/>
<field name="table_users_user_code" class="java.lang.String"/>
<field name="table_users_first_name" class="java.lang.String"/>
<field name="table_users_last_name" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="33" y="0" width="100" height="20"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="132" y="0" width="100" height="20"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[CODE]]></text>
</staticText>
<staticText>
<reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="232" y="0" width="100" height="20"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[FIRST NAME]]></text>
</staticText>
<staticText>
<reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="332" y="0" width="100" height="20"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[LAST NAME]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="33" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{table_users_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="133" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{table_users_user_code}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="232" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{table_users_first_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="332" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{table_users_last_name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
推荐答案
关于文档,你应该这样做:
Regards to the Document, you should do this:
reportParameters - (2.1.2+) OGNL expression used to retrieve a map of report
parameters from the value stack. The parameters may be accessed in the report
via the usual JR mechanism and might include data not part of the dataSource,
such as the user name of the report creator, etc.
所以你应该有这样的配置:
So you should have this configuration:
<action name="myJasperTest" class="com.acme.test.action.JasperAction">
<result name="success" type="jasper">
<param name="location">/jasper/our_compiled_template.jasper</param>
<param name="dataSource">myList</param>
<param name="format">PDF</param>
<param name="exportParameters">parameterMap</param>
</result>
</action>
这是文档
这篇关于使用Struts2框架的Jasper报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!