Recently, the charm is coincident, the body is the head, and the paste is paste and will be a simple report. The result is the focus, and it will be less sleep. . . . . . Just combined with Struts, JasperReport has done a report, and recently seen a few days Spring, itch is difficult. Hey, long sigh, then do a summary of the comfort of the pain (maybe I should first improve IQ). Hey, human beings always like to show off, so I will first say that my JasperReport is applying on Spring. Second, let's talk about the difference between and ordinary web applications (including struts), and of course it is to make up. (Think See my ugly friends, don't be too disappointed), 吼! ! We know, Spring has built-in support for multiple output formats, such as PDF, XLS, etc., and report usually requires production of multiple formats, then they combine, isn't it a natural place, Wang Ba Green Beans? ? Anyway, I think so. Since each format is similar, I only say the process of PDF. Developing a web application, of course, there is a web.xml first, define the entry of Spring org.SpringFramework.web.Servlet.dispatcherServlet
Here is the * .pdf is not less. If you want to submit to XXX.htm first, then return to PDF, you will be prompted to download xxx.htm. Spring's support for PDF is through AbstractPdfView, the user should inherit this class you write PDF content, but JasperReport already has the function of generating PDF, and we want, just with its MVC framework, return to customers when needed Want PDF page, so we directly inherit the AbstractView (bottom is a general program specific code) public class iampdfview extends abstractview {
Private log logger = logfactory.getlog (this.getClass ());
Public IAMPDFVIEW () {setContentType ("Application / PDF");
/ * * @See org.springframework.web.servlet.view.AbstractView # renderMergedOutputModel (java.util.Map, * javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) * / protected void renderMergedOutputModel (Map Model, httpservletRequest request, httpservletResponse response) throws exception {jreportparam jreportparam = (jreportparam) Model.get ("jreportparam");
BYTE [] bytes = null;
try {bytes = JasperRunManager.runReportToPdf (jReportParam .getJasperFilePath (), jReportParam.getParameters (), jReportParam.getDataSet ());} catch (JRException e) {logger.info ( "zzzzzzzzzzzzzzzzz IamPdfView Exception e:!" e); }
ByteArrayOutputStream baos = new ByteArrayOutputStream (); baos.write (bytes); response.setContentLength (baos.size ()); response.setContentType (getContentType ()); ServletOutputStream out = response.getOutputStream (); baos.writeTo (out) Out.flush (); out.close ();
}
} Where JreportParam is a simple package for report parameters. Spring provides a lot of viewresolver, I choose org.springframework.web.servlet.View.ResourceBundLeviewResolver, because you can write a page related information writing configuration file, such as pdfview.class = com.iam.testreport.report.view.IAMPDFVIEW When Return New ModelandView ("PDFView", com.iam.teestreport.report.view.Iampdfview can be called. Let's talk about the difference between ordinary web applications (including struts), in fact, Spring is more viewresolver than them, and its default_content_type = "text / html; charSet = ISO-8859-1"; so we need to expand AbstractView or photo report servlet is Name.pdf (this method does not extends the unique class of Spring).