Retrofit jasperreports for STRUTS2 plug-in to add a variety of data source capabilities

Source: Internet
Author: User

JasperReports is a very useful reporting engine, and recently studied the problem of joining applications with STRUTS2. The data sources supported by the plugins currently downloaded from the STRUTS2 Web site are very simple and can only support the data form of the list< entity class >, which limits the programmer's flexibility in developing the report. JasperReports supports a variety of data sources, such as Jrmapcollectiondatasource in list<map> form, or hibernate with the application of Jrhibernatelistdatasource, and so on, the current STRUTS2 plug-in that is struts2-jasperreports-plugin people very uncomfortable, a few minutes to change it, Let IT support all data sources that jasperreports can use.

The modification process is as follows:

1. Use the SVN tool to get the Struts2-jasperreports-plugin source code from the STRUTS2 code warehouse, open the Jasperreportsresult.java file, and find the following code:

       //Handle IE Special Case:it sends a "contype" request first.
       //TODO Set content type to config settings?
        if ("Contype". Equals (Request.getheader ("User-agent")) {
             try {
                 Response.setcontenttype ("application/pdf");
                Response.setcontentlength (0);

                Servletoutputstream outputstream = Response.getoutputstream ();
                Outputstream.close ();
           } catch (IOException e) {
                 Log.error ("Error writing report Output ", e);
                throw New Servletexception (E.getmessage (), E);
           }
            return;
       }

Construct the data source for the report.
Valuestack stack = Invocation.getstack ();

After the code is modified, define a Jrdatasource interface and then determine if the data source provided in the action satisfies the Jrdatasource interface, and the code is modified as follows:

Construct the data source for the report.
Valuestack stack = Invocation.getstack ();

Object Datasourceobject=stack.findvalue (DataSource);
Jrdatasource Jrdatasource=null;
The following determines whether the Jrdatasource interface is satisfied
try{
Jrdatasource= (Jrdatasource) DataSourceObject;
}catch (Exception e) {
Jrdatasource = new Valuestackdatasource (stack, dataSource);
}

Finally, modify the parameters that populate the data when the report is generated, as follows:

Jasperprint = Jasperfillmanager.fillreport (jasperreport, parameters, Jrdatasource);

That's all you can do.

In addition, this Struts2-jasperreports-plugin code seems to be a bit of a problem with my understanding of jasperreports, actually extracting the field data first by description, I use other reporting tools to extract data by pressing FieldName, so by the way, change the code code in Valuestackdatasource as follows:

Public Object GetFieldValue (Jrfield field) throws Jrexception {
Todo:move the code to return a valuestackdatasource to a seperate
method when and if the Jrdatasource interface are updated to support
This.
String expression = Field.getname ();

if (expression = = null) {
Description is optional so use the field name as a default
expression = Field.getdescription ();
}

Compile there are some small problems, the log class can not find, casually changed to log4j or anything, after testing is cool.

For example, when applied together with the JdbcTemplate in spring:
String query_sql= "Select B.name,a.class_name,a.edu_start_year,d.student_name from Class_info a" +
"INNER JOIN teacher B on a.teacher_id=b.teacher_id inner join Student_class C" +
"On A.class_info_code=c.class_info_code inner JOIN student D" +
"On c.student_id=d.student_id where a.class_info_code=?" ORDER by B.name,a.class_name ";
List<map> tmp_datalist=this.jdbctemplate.queryforlist (query_sql,new object[]{integer.valueof (this.class_ Info_code)},new Int[]{types.integer});
This.datalist = new Jrmapcollectiondatasource (tmp_datalist);
Note that this is the DataList object that provides data to Struts2-jasperreports-plugin, the data type is Jrmapcollectiondatasource, It is ok to specify DataSource as DataList in the configuration parameters of Struts2.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.