Struts2 + JasperReport应用二:jasperReport Web形式打印

将jasperReport设计文件和编译文件放到webRoot下面的jasper文件夹下面。

public class Person {
	private String person_Id;

	private String person_name;

	private String person_age;

	private String person_address;
}

 

public class PersonService {
	public List<Person> getAllPerson() {
		List<Person> perList = new ArrayList<Person>();
		perList.add(new Person("101", "小博", "22", "湖北"));
		perList.add(new Person("102", "张三", "21", "湖南"));
		perList.add(new Person("103", "李四", "23", "江苏"));
		perList.add(new Person("104", "王五", "22", "上海"));
		return perList;
	}
}

 

构造我们的applet如下:

public class JRPrinterApplet extends javax.swing.JApplet {
	private URL url = null;
	public void init() {
		String strUrl = getParameter("REPORT_URL");
		if (strUrl != null) {
			try {
				url = new URL(getCodeBase(), strUrl);// 从获得html参数中获得报表URL
				// System.out.println("url=" + url.toURI());//要是servlet的路径
			} catch (Exception e) {
				StringWriter swriter = new StringWriter();
				PrintWriter pwriter = new PrintWriter(swriter);
				e.printStackTrace(pwriter);
				JOptionPane.showMessageDialog(this, swriter.toString());
			}
		} else {
			JOptionPane.showMessageDialog(this, "Source URL not specified");
		}
	}
	public void start() {
		if (url != null) {
			try {
				Object obj = JRLoader.loadObject(url);// 发送对象请求,获得JasperPrint对象
				JasperPrintManager.printReport((JasperPrint) obj, true);// 调用方法打印所获得的JasperPrint对象
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 构造我们的action如下:

public class JRPrintAction extends ActionSupport {
	@Override
	public String execute() throws Exception {
		File reportFile = new File(ServletActionContext.getRequest()
				.getRealPath("/jasper/preson.jasper"));
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("year", "2009");
		parameters.put("unit_mc", "武汉XX科技有限公司");
		List<Person> personList = new PersonService().getAllPerson();
		JasperPrint jasperPrint = null;
		try{
		JasperReport jasperReport = (JasperReport) JRLoader
				.loadObject(reportFile);
		jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
				new JRBeanCollectionDataSource(personList));
		}catch (Exception e) {
			throw e;
		}
		if(null != jasperPrint){
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/octet-stream");
			ServletOutputStream ouputStream = response.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
			oos.writeObject(jasperPrint);
			oos.flush();
			oos.close();
			ouputStream.flush();
			ouputStream.close();
		}
		return null;
	}
}

 

struts.xml配置如下:

<action name="jrPrint"
			class="com.mengya.action.JRPrintAction">
			<result name="success">/index.jsp</result>
		</action>

 web.xml配置如下:

<filter>
		<filter-name>Struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>Struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

将JRPrinterApplet的编译文件放到webRoot下面的applet文件夹下面

页面调用如下:

<input type="button" value="打印" name="button1" οnclick="print();">

<script>
  	function print(){ 
		var url = "jrPrint.action"; 
		document.write('<APPLET ID="JrPrt" CODE = "JRPrinterApplet.class" CODEBASE = "applets" ARCHIVE = "jasperreports-3.5.2-applet.jar,commons-logging-1.0.2.jar,commons-collections-2.1.jar" WIDTH = "0" HEIGHT = "0">'); 
		document.write('<PARAM NAME = "type" VALUE="application/x-java-applet;version=1.2.2">'); 
		document.write('<PARAM NAME = "scriptable" VALUE="false">'); 
		document.write('<PARAM NAME = "REPORT_URL" VALUE ="'+url+'">'); 
		document.write('</APPLET>'); 
	}
  </script>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值