Let me talk about the choice of reporting framework first. JasperReport and BIRT are both good JAVA reporting tools, but the integration of BIRT and Spring is more troublesome. There is a good article in the Spring official website forum, but I think it is still quite troublesome. Jasper is simpler.
There are many integration methods between SpringMVC and JasperReport. Here is the official method in the Spring documentation. This method is relatively simple and requires very little code. The disadvantage is that it is not automatic. Every time a report is added, the configuration file needs to be modified and the service restarted. It is not suitable for WEB services that specialize in reporting.
1. Add in the configuration file:
- <beans:beanid="viewResolver"class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
- <beans:propertyname="basename"value="views"/>
- </beans:bean>
Note the order of resolvers.
2. Add views.properties to the classpath. My files are placed under src/main/resources.
The content of views is:
- #Set report output format
- simpleReport.(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
- #Set report file path
- simpleReport.url=/WEB-INF/reports/report1.jasper
- #Data key value
- simpleReport.reportDataKey=datasource
Each report requires a set of the above configuration. Spring supports automatic selection of report input formats at runtime. For details, you can check the official documentation of spring.
3. Edit the report with iReport. iReport version is 4.5.1
The pdf of iReport has a traditional Chinese problem, and there are many introduction articles on the Internet. There are itext-2.1.7.jar, iTextAsian-2.1.7.jar and iText-AsianCmaps-2.1.7 packages under lib of ireport. (downloadable in attachment)
I thought iReport4.5.1 could be more advanced, but I didn't expect to use iText-2.1.7 and the other two supporting packages, don't use the latest version, the latest version is invalid.
1) Add the above 3 packages to iReport Tools-->Options-->Classpath
2) In iReport Tools-->Options-->Fonts, tick the checkboxes in front of these 3 packages.
3) When editing the Text box of the report, edit its properties as follows
Pdf Font name:STSong-Light;
Pdf Embedded: Check it
Pdf Enccoding:UniGB-UCS2-H (Chinese Simplified)
4) Add these three jar packages to the ClassPath of the Eclipse project.
In this way, the pdf can display Chinese normally.
Copy the edited .jasper file to the Eclipse project.
4. Write Controller.
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- @Controller
- @RequestMapping("/report.html")
- publicclass ReportCtrl {
- privatestaticfinal Logger logger = LoggerFactory.getLogger(ReportCtrl.class);
- @RequestMapping()
- public ModelAndView showReport(ModelMap model, HttpServletRequest request) {
- logger.info("ReportCtrl-showReport start!");
- DataSource map = new DataSource();
- map.setPlanno("APP1-1039-0990");
- map.setPlanname( "Shanghai Taxi" );
- List<DataSource> beanData = new ArrayList<DataSource>();
- beanData.add (map);
- model.addAttribute( "datasource" , beanData); //datasource and views.properties are configured the same
- logger.info("ReportCtrl-showReport end!");
- return new ModelAndView( "simpleReport" , model); //simpleReport is the same as the one configured in views.properties
- }
- //bean definition
- publicclass DataSource {
- private String planno = "";
- private String planname = "";
- public String getPlanno() {
- return planno;
- }
- publicvoid setPlanno(String planno) {
- this.planno = planno;
- }
- public String getPlanname() {
- return planname;
- }
- publicvoid setPlanname(String planname) {
- this.planname = planname;
- }
- }
- }
Run to display the report.