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>
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Struts2 + JasperReport Application 2: jasperReport Web Forms Printing

  • To a generic hibernate example DAO

    Reprint: http://blog.csdn.net/dingx package sgf4web.dao; import java.io.Serializable; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.hibernate.*; import org.hibernate.criterion.*; import org.springframework.

  • hibernate (jpa) composite primary key annotation statement Ways

    In the design of the database tables are designed with a composite primary key of the table, that table's record by more than one field joint identification, such as: Table CREATE TABLE TB_HOUR_DATA ( STAT_DATE DATE NOT NULL, PATH_ID NUMBER(20) NOT NULL,

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Message Dri

  • java read file

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa

  • Based on Spring's Hibernate Search full-text search function of sample

    Database: Oracle 9i JDBC Driver: OJDBC14 Development Environment: Eclipse-JEE Spring version: Spring 2.0.6 Hibernate version: Hibernate Core 3.2.5/Hibernate Annotation 3.3.0/Hibernate Validator 3.0.0/Hibernate Search 3.0.0 Beta4 / / jdbc.properties (JDBC

  • Hibernate Mapping Types

    Hibernate mapping types divided into two categories: built-in mapping types and mapping types of customers. Built-in mapping types is responsible for some common Java types are mapped to the corresponding SQL type; In addition, Hibernate also allows users

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries