IReport整合Struts2

 

Strudents.jrxml文件略

 

Strudents.jrxml放在web应用的WebRoot\Jasper\Strudents.jrxml

 

往项目里加入支持Jasperreportjar文件
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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值