Jasperreports+jaspersoft studio学习教程(四)- JavaBean作为数据源填充数据
程序员文章站
2022-05-18 17:16:38
...
转载:https://blog.csdn.net/shiyun123zw/article/details/79180123
在实际项目中,一般使用java对象作为数据源的方式更多,这种方式会更加灵活多变。对于mvc结构更为契合。上面介绍了用JDBC数据源时,用到了Fields对象。本篇主要是使用Fileds对象来匹配JavaBean对象。
5.1 在项目中新建user对象(添加get,set方法),结构如下:
5.2 新建报表模板DemoReport4.jrxml
5.2.1按照user对象新建Fileds(注意数据类型一致)
5.2.2 去掉多余的band,只保留Title,Detail,PageFooter。去掉band的前后距,步骤:模板(DemoReport4)右键 -> Show Propertes -> Page Format -> Edit Page Format
5.2.3 将Filed拖动到 Detail 1 Band。并设计模板,保持Filed的高与Detail Band的高一致
设置页码,打开组件面板 在Composite Elements栏找到Page Number 拖入到 Page Footer 中
保存,并将jrxml文件放入到项目webroot下新建的jrxml目录中。
5.3 编写servlet注入JavaBean数据源。新建JasperServlet3
5.3.1 JasperServlet3的doget如下:
此处代码略;往下看; 5.3.2 启动tomcat并访问
Maven结构代码如下:
User.java
package com.accord.demo;
public class User {
private String name;
public User() {
super();
}
public User(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JasperServletJavaBean.java
package com.accord.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.accord.demo.User;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class JasperServletJavaBean extends HttpServlet {
private static final long serialVersionUID = 1L;
public JasperServletJavaBean() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletConfig().getServletContext();
try {
//JasperCompileManager.compileReportToFile("E:/report/DbReport.jrxml");
JasperCompileManager.compileReportToFile(context.getRealPath("/jasper/DemoReport04.jrxml"));
} catch (JRException e1) {
e1.printStackTrace();
}//编译jrxml文件,生成jasper文件
File reportFile = new File(context.getRealPath("/jasper/DemoReport04.jasper"));
if (!reportFile.exists())
throw new JRRuntimeException("FileWebappReport.jasper "
+ "not found. The report design must be compiledfirst.");
String jasperPath = context.getRealPath("/jasper/DemoReport04.jasper");
FileInputStream isRef = null;
ServletOutputStream sosRef = null;
try {
isRef = new FileInputStream(reportFile);
sosRef = response.getOutputStream();
//组装list数据源
List<User> list = new ArrayList<User>();
for (int i = 1;i<=100;i++) {
User user = new User();
user.setName("小明" + i);
list.add(user);
}
//javabean为数据源注入报表数据
JasperRunManager.runReportToPdfStream(isRef, sosRef, null,new JRBeanCollectionDataSource(list));
response.setContentType("application/pdf");
} catch (Exception e) {
e.printStackTrace();
}finally {
sosRef.flush();
sosRef.close();
}
}
}
DemoReport04.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="DemoReport04" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a4cc8842-e711-43ea-bab5-c3fb8a0707c1">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="msql"/>
<queryString language="SQL">
<![CDATA[select * from company]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="com.jaspersoft.studio.field.label" value="name"/>
<property name="com.jaspersoft.studio.field.tree.path" value="company"/>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="54" splitType="Stretch">
<staticText>
<reportElement x="216" y="12" width="122" height="30" uuid="2af80fa7-f0d5-4b35-a084-bf1818a9296e"/>
<textElement>
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[名称明细表]]></text>
</staticText>
</band>
</title>
<detail>
<band height="84" splitType="Stretch">
<textField>
<reportElement x="240" y="64" width="100" height="20" uuid="d8be8542-ab13-4c4e-bdca-ad95a363ddc5"/>
<textElement>
<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="54" splitType="Stretch">
<textField>
<reportElement x="441" y="23" width="100" height="30" uuid="5eeb73dc-d3e6-452d-87b9-6ee4fb03021c"/>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JasperReports</groupId>
<artifactId>JasperReports02</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JasperReports02 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 解决中文乱码 -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itextasian</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<finalName>JasperReports02</finalName>
</build>
</project>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>JasperServlet</servlet-name>
<display-name>JasperServlet</display-name>
<description></description>
<servlet-class>com.accord.servlet.JasperServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>JasperServletDb</servlet-name>
<display-name>JasperServletDb</display-name>
<description></description>
<servlet-class>com.accord.servlet.JasperServletDb</servlet-class>
</servlet>
<servlet>
<servlet-name>JasperServletJavaBean</servlet-name>
<display-name>JasperServletJavaBean</display-name>
<description></description>
<servlet-class>com.accord.servlet.JasperServletJavaBean</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JasperServlet</servlet-name>
<url-pattern>/JasperServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JasperServletDb</servlet-name>
<url-pattern>/JasperServletDb</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JasperServletJavaBean</servlet-name>
<url-pattern>/JasperServletJavaBean</url-pattern>
</servlet-mapping>
</web-app>