Struts2 + JasperReport Application 2: jasperReport Web Forms Printing

Be jasperReport design document and compile files on webRoot the following jasper folder below.

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", " Klein  ", "22", " Hubei  "));
                perList.add(new Person("102", " Smith, John  ", "21", " Hunan  "));
                perList.add(new Person("103", " Paul Lee  ", "23", " Jiangsu  "));
                perList.add(new Person("104", " Wangwu  ", "22", " Shanghai  "));
                return perList;
        }
}

Construction of our applet is as follows:

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);//  From the HTML parameters in a report URL
                                // System.out.println("url=" + url.toURI());// If the servlet path
                        } 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);//  Send an object request, obtain JasperPrint object
                                JasperPrintManager.printReport((JasperPrint) obj, true);//  Call the method to print the JasperPrint object
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
        }
}

Structure of our action is as follows:

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", " Wuhan XX technology co., Ltd.  ");
                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 configuration is as follows:

<action name="jrPrint"
                       >
                        <result name="success">/index.jsp</result>
                </action>

web.xml configuration is as follows:

<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>

Be JRPrinterApplet compile files on webRoot the following applet the following folder

Page call is as follows:

<input type="button" value=" Print  " name="button1">

<script>
        function print(){
                var url = "jrPrint.action";
                document.write('<APPLET 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>
分类:Java 时间:2010-03-29 人气:456
分享到:
blog comments powered by Disqus

相关文章

  • Classic String Object into several forms of [transfer] 2010-10-25

    Classic String Object into several forms of [transfer] Java project in the actual development and applications, often need to use the String object into this basic function. This method will be used to convert a summary. Commonly used method Object #

  • Extend JavaScript's String Object 2010-08-17

    Extend JavaScript's String object methods: 1. ReplaceAll method String.prototype.replaceAll = function(regStr, replaceStr) { var reg = new RegExp(regStr, "gm"); return this.replace(reg, replaceStr); } 2. Trim method String.prototype.trim = funct

  • Please do not take the "String s = new String (" xyz "); created a number of String instances" to the interview now 2010-09-29

    This quote is used to restore high-level language virtual machines circles to be a problem, a Java pen test questions. Had seen enough already Tucao as weakness, but this could not bear it for another one broke out. Written too long to open a separat

  • Subject: Please do not take the "String s = new String (" xyz "); create a number of String instances" to the interview now 2010-12-02

    Had seen enough already Tucao as weakness, but this could not bear it for another one broke out. Written too long to open a separate post altogether. Incidentally Advertising: The JVM students interested comrades please support high-level language vi

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • Object is converted to JSON string 2010-03-29

    Keywords: json package com.sy.test; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; / ** * Providing the object is converted to JSON string method * @ Author author Li Youth * @ Version Version 0.01 * @ Filename

  • Java String object, the classic problem of (new String ()) [switch] 2011-08-05

    Look at an example, the code is as follows: Java code public class Test { public static void main (String [] args) { String str = "abc"; String str1 = "abc"; String str2 = new String ("abc"); System.out.println (str == str1);

  • javaBean与Map String,Object 互转 2014-04-15

    1. 为什么要实现javaBean与Map<String,Object>相互转换? 用过spring的都知道spring的MVC框架中有一个BaseCommandController对象,利用这个对象我们就可以很方便的将从客户端传递过来的参数封装到一个JavaBean对象中去,而不需要我们request.getParameter("name");bean.setName(name);了,从而也简化了不少的工作.如果大家用过BeanUtils.populate的话,就知道,这

  • String object understanding in progress 2008-06-12

    In-depth JAVA virtual machine "is so described in the book: JVM run-time data area of memory model consists of five parts: [1] [2] methods that heap [3] JAVA stack [4] [5] PC register native method stacks For String s = "haha", its virtual

  • Java String object on a small summary in the 2011-05-05

    String is a pair of double quotes character sequence, in the JAVA language, the string data is actually achieved by the String class. Java string class is divided into two types: one is the change in the program will not be the same length of string;

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.844 (s). 12 q(s)