iReport + jasperreports + struts 2 集成開發報表

項目準備:
                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,否則攔截不到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章