Strudents.jrxml文件略
将Strudents.jrxml放在web应用的WebRoot\Jasper\Strudents.jrxml
往项目里加入支持Jasperreport的jar文件
struts2-jasperreport-plugin-2.0.12.jar
jasperreports-2.0.5.jar
commons-digester-1.7.jar
commons-beanutile-1.7.jar
commons-collections-2.1.jar
itext-1.3.1.jar
iTextAsian.jar
poi-3.0-alpha3-20061212.jar
写一个提供数据源的javaBean:
package bean;
public class Student {
private int stuid;
private String stuname;
private String stuphoto;
public Student(int stuid,String stuname,String stuphoto){
this.stuid = stuid;
this.stuname = stuname;
this.stuphoto = stuphoto;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuphoto() {
return stuphoto;
}
public void setStuphoto(String stuphoto) {
this.stuphoto = stuphoto;
}
}
写一个action:
package action;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import net.sf.jasperreports.engine.JasperCompileManager;
import org.apache.struts2.ServletActionContext;
import bean.Student;
import com.opensymphony.xwork2.ActionSupport;
public class StudentJasperAction extends ActionSupport {
private List student;
public List getStudent() {
student = new ArrayList();
for(int i=0;i<50;i++){
Student stu = new Student(i,"张三"+i,"1503669100"+i);
student.add(stu);
}
return student;
}
public String execute() throws Exception {
// TODO Auto-generated method stub
String sourceFile = ServletActionContext.getServletContext()
.getRealPath("/Jasper/Strudents.jrxml");
File parent = new File(sourceFile).getParentFile();
JasperCompileManager.compileReportToFile(sourceFile,new File(parent,"/Strudents.jasper").getAbsolutePath());
return super.execute();
}
}
修改struts.xml:
<?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>
<package name="struts2" extends="jasperreports-default">
<action name="PDF" class="action.StudentJasperAction">
<result name="success" type="jasper">
<param name="location">Jasper/Strudents.jasper</param>
<param name="format">PDF</param>
<param name="dataSource">student</param>
</result>
</action>
<action name="HTML" class="action.StudentJasperAction">
<result name="success" type="jasper">
<param name="location">Jasper/Strudents.jasper</param>
<param name="format">HTML</param>
<param name="dataSource">student</param>
</result>
</action>
<action name="XML" class="action.StudentJasperAction">
<result name="success" type="jasper">
<param name="location">Jasper/Strudents.jasper</param>
<param name="format">XML</param>
<param name="dataSource">student</param>
</result>
</action>
<action name="XLS" class="action.StudentJasperAction">
<result name="success" type="jasper">
<param name="location">Jasper/Strudents.jasper</param>
<param name="format">XLS</param>
<param name="dataSource">student</param>
</result>
</action>
</package>
</struts>
写一个jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
打印、预缆报表 <hr>
<a href="PDF.action">PDF格式报表</a><br/>
<a href="HTML.action">HTML格式报表</a><br/>
<a href="XML.action">XML格式报表</a><br/>
<a href="XLS.action">EXCLE格式报表</a>
</body>
</html>
为了支持HTML显示时图片加载不上的问题,我们放一个图片文件进去,地址是WebRoot\images\px 命名一定要和我的一样。
作者: ( http://www.macower.com)