Struts2 + JasperReport Application 2: jasperReport Web Forms Printing

2010-03-29  来源:本站原创  分类:Java  人气:462 

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>
相关文章
  • Struts2 + JasperReport Application 2: jasperReport Web Forms Printing 2010-03-29

    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 Perso

  • jatoolsPrinter (kit form. Print Control) is a specialized tool for achieving web chromatography printing 2010-04-09

    jatoolsPrinter (kit form. Print Control) is a specialized tool for achieving web chromatography printing. As a web application developers, we often encounter in the browser to print reports, demand for the paper, these needs the browser's own print f

  • Recommended a Flex-based Web document printing solutions (transfer) 2011-09-28

    http://blog.csdn.net/sqlhub/article/details/6823957 ITEYE XDOC details on the website below. Free report XDOC Mastering (a) Introduction http://xdoc.iteye.com/blog/866342 Free report XDOC Mastering (b) installation http://xdoc.iteye.com/blog/866362 F

  • Web Forms 2.0 或将被 HTML 5 代替 2014-12-09

    尽管为时尚早,HTML 5 终究要来,这个未来的 Web 明星包含了人们太多的期待,人们似乎忘了已经在 Web Forms 中所投入的精力,最近 HTML 工作组宣布,Web Forms 2.0 将被 HTML 5 取代. 关于 HTML 5 的一些资料: HTML 5 正在改变 Web HTML 5 令人期待的 5 项功能 什么是 Web Forms 2.0? 早在2004年,第一个 Web Forms 2.0 工作草案就出台了,当时,这个项目更像一些空想家的痴人说梦,而不是一个正式的 W3C

  • Return to the application server and web server integration 2009-05-03

    On the application server and web server consolidation, there are a lot of information now, but all speak of the half-baked. According to this a few days the experience of integrating tomcat and iis again talk to this topic. First, we should be the a

  • Access technologies including Java Applet Application of the Web page and, IE crashes 2010-05-07

    Problem <! - BEGIN SECTION 1 -> use the Internet Explorer browser to access Java technology Applet Application contains the Web page, Internet Explorer browser crashes. <! - END SECTION 1 -> Reason <! - BEGIN SECTION 2 -> Here are some p

  • ASP.NET Web Forms 4.0中的双向Routing支持 2014-04-04

    .NET Framework 3.5 SP1已经包含了ASP.NET Routing引擎.现在微软已经在ASP.NET WebForms 4.0中增加了对Routing引擎更好的支持,它使用表达式构造器进行双向Routing. Channel 9刚发布了一个10-4的新视频,展示如何在ASP.NET 4.0中使用这个新功能. 下面是视频中展示的一段代码.他们使用了一个经典示例,展示如何将Product.aspx?category=Jerseys映射至Product/Jerseys.在使用ASP.

  • 在ASP.NET Web Forms中用System.Web.Optimization取代SquishIt 2014-10-01

    将一个ASP.NET Web Forms项目从.NET Framework 4.0升级至.NET Framework 4.5之后,发现SquishIt竟然引发了HTTP Error 500.0 - Internal Server Error. SquishIt是一个开源的支持ASP.NET的js/css打包工具,项目地址:https://github.com/jetheredge/SquishIt,出生早于Microsoft ASP.NET Web Optimization Framework(

  • Web Forms 2.0 2015-03-17

    Web Forms 2.0 是一个很有意思的东东,是 HTML 5 的组成部分.它的目标是提升表单的使用性 (usability),基本上就是为 input 元素的 type 属性增加一些值,如 type="email":还有一些新属性,如 required.根据 type 由浏览器实现各种功能.比如,<input type="email" required="required" />,从字面上即可看出,这是一个必须填写,且格式是电子

  • Struts2 + Spring + iBatis on the web application of best practices Series 2 (Access Control chapter I) 2010-03-29

    Access control for a web applications is almost indispensable. When the web application to access certain resources which, if you are not logged in then, it will be redirected to the login page, and only after the login will only be allowed to visit.

  • Struts2 + Spring + iBatis on the web application of best practices Series 3 (Access Control chapter below) 2010-03-29

    The last one in this series, we introduced a cookie-based access control methods, the careful reader will find that the realization of this approach is ultimately based on Struts2 interceptors mechanism, which means it can only protect web applicatio

  • Struts2 + Spring + iBatis on the web application of best practices series of four (CRUDL articles) 2010-03-29

    Let us first look at what is CRUD, CRUD or Create-Read-Update-Delete the English abbreviation, but I would also like to add one here, L, namely List, combine to make up CRUDL. CRUDL this scenario or model are common among most of the applications, th

  • Struts2 + Spring + iBatis on the web application of best practices Series 5 (page articles) 2010-03-29

    Paging is also a frequent topic of discussion all, on-line there were a lot of paging method of introduction, but often merely confined to the web layer or data access layer of a page component, for a typical three-tier web applications I have not se

  • Struts2 + Spring + iBatis on the web application of best practices Series 6 (Code articles) 2010-03-29

    This chapter focuses on how to use the Struts2 to achieve a common code (checkcode) function. First, we have a CheckCodeSession class is used to generate a good to preserve our checkcode. This class is very simple, mainly to save the code itself, cre

  • Struts2 + Spring + iBatis on the web application of best practices Series 7 (Demo articles) 2010-03-29

    Finally reached the final of this series of articles, demo have been produced, and can be downloaded here demo.rar Download and use the demo, please make sure the following environment is properly installed and configured. jdk, this does not say, I b

  • Reprinted PHP Application Framework Design: 4 - Forms and Events 2010-02-24

    Transfer from: http://www.lbsharp.com/wordpress/index.php/2005/10/13/php-application-framework-design-4-forms-and-events/ This is part 4 of a multi-part series on the design of a complete application framework written in PHP. In part 1, we covered th

  • JFreeChart application on the Web 2010-04-03

    First, the jar package: jcommon-1.0.16.jar and jfreechart-1.0.13.jar copied to the Web application directory in the / WEB-INF/lib directory. And in the web.xml file, add the following code: <servlet> <servlet-name>DisplayChart</servlet-name

  • Struts2简单的验证,访问web元素 2013-12-09

    (五)Struts2简单的验证 这里使用的简单验证是Struts2中最简单的验证. 但是不得不说,struts2的验证系统还是没有JSF的好用. 首先是struts.xml文件配置 <package name="default" namespace="/" extends="struts-default"> <!-- action name is Visit URL's path,you can write 'hello.jsp

  • Agile Development Application to the Web has brought the spring of this 2009-09-08

    As Web applications become increasingly large and medium-sized enterprises by pro-gaze, 90% of the enterprises are focused on the deployment of such a basic, simple operation, saving the late-to-business Web application maintenance costs; J2EE,. Net

  • How to create mobile Web Forms VisualStdio 2010-05-26

    Quote Development of the browser for mobile devices and development of ASP.NET pages for the desktop browser, the page is no essential difference. This is the MSDN on about "ASP.NET Mobile Web Development Overview" in the first sentence. Mobile