您的位置:首页 > 编程语言 > ASP

iReport + jasperreports + struts 2 集成开发报表

2013-03-14 17:54 393 查看
项目准备:
                1、使用iRrport 设计一个jrxml文件并把其编译成jasper格式的文件
                2、下载项目中所需要的包,基本上是下面这些,下面的这些我是使用SSH2的框架,可以看情况而使用那些包



项目开发《本项目使用的是javabean作为数据源》:

JavaBean:
package com.jr.bean;

import java.math.BigDecimal;

/**
* 员工信息bean
* @author HRQ
*
*/
public class Employee {
private Integer id;
private String name;
private BigDecimal saly;
private String detpName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getSaly() {
return saly;
}
public void setSaly(BigDecimal saly) {
this.saly = saly;
}
public String getDetpName() {
return detpName;
}
public void setDetpName(String detpName) {
this.detpName = detpName;
}

}
action:
package com.jr.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.jr.bean.Employee;
import com.jr.service.IEmployeeService;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 6093380186451210429L;
private IEmployeeService emservice;
private List<Employee> list;

@Override
public String execute() throws Exception {
if(list == null){
list = new ArrayList<Employee>();
Employee em = new Employee();
em.setId(1001);
em.setName("张三");
em.setDetpName("游戏部");
em.setSaly(new BigDecimal(4500));
list.add(em);
em = new Employee();
em.setId(1002);
em.setDetpName("开发部");
em.setSaly(new BigDecimal(3500));
em.setName("王五");
list.add(em);
em = new Employee();
em.setId(1003);
em.setDetpName("开发部");
em.setSaly(new BigDecimal(3500));
em.setName("serch");
list.add(em);
}
return SUCCESS;
}

public String jasperImage(){

return SUCCESS;
}

public IEmployeeService getEmservice() {
return emservice;
}

public void setEmservice(IEmployeeService emservice) {
this.emservice = emservice;
}

public List<Employee> getList() {

return list;
}

public void setList(List<Employee> list) {
this.list = list;
}

}


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>
<constant name="struts.action.extension" value="action" />
<package name="test" extends="struts-default,jasperreports-default" >
<action name="testjasper" 	class="com.jr.action.EmployeeAction" >
<result name="success" type="jasper">
<param name="location">WEB-INF/jaspers/report2.jasper</param>
<param name="dataSource">list</param>
<param name="imageServletUrl"><![CDATA[/image?image=]]></param>
<param name="format">HTML</param>
<param name="documentHeader">bill_no2</param>
</result>
<result name="input">/test.jsp</result>
</action>
</package>
</struts>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>JaspreRepots</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置资源 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring-*.xml</param-value>
</context-param>
<!-- 配置CharacterEncoding,设置字符集
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>  -->

<!-- 将HibernateSession开关控制配置在Filter,保证一个请求一个session,并对lazy提供支持
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>-->

<!-- 配置spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<!--  <init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/WEB-INF/conf/struts.xml</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 页面session配置 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<servlet >
<servlet-name>imageServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>
<!-- 错误页面
<error-page>
<error-code>404</error-code>
<location>/error404.html</location>
</error-page>-->
</web-app>

项目问题:打印HTML格式图片不出来
解决办法:
在struts.xml文件中写以下代码



在web.xml中配置servlet



备注:struts.xml文件中的

<param name="imageServletUrl"><![CDATA[/image?image=]]></param>

image访问的路径就是web.xml文件中配置的servlet访问的路径
这样就可以解决图片不显示的问题了,但是访问这个action后缀名必须带上.action,否则拦截不到
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息