77范文网 - 专业文章范例文档资料分享平台

Generating Reports and PDFs From a Web Application(3)

来源:网络收集 时间:2018-12-17 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

Mapping the JasperReports ImageServlet

The JasperReports framework includes a servlet named ImageServlet for displaying images in generated HTML reports. Even if you do not include images in your report, you must add a mapping for this servlet, because JasperReports uses blank images for spacing.

Complete the following steps to map the image URI to ImageServlet. You use the image URI later when you add code to export the report to HTML.

1. In the Files window expand TravelReport, expand web, expand WEB-INF, and double-click the

web.xml node to open it in the XML editor. 2. In the editing toolbar, click Servlets to open the Servlet Editor. 3. In the Servlets editor, click Add Servlet Element.

4. The Add Servlet dialog box appears as shown in the following figure.

Figure 6: Add Servlet Dialog Box

5. In the Add Servlet dialog box, type the following values and click OK.

Text Box Servlet Name Servlet Class URL Patterns

Value ImageServlet net.sf.jasperreports.j2ee.servlets.ImageServlet /image

6. Close web.xml and save the changes.

7. Now, the application knows that when it receives a request that is addressed to image, the

application should send the request to the ImageServlet instance.

Adding the Report Generation Logic

Follow these steps to add logic to the application bean for generating the report.

1. In the Projects window, double-click the Application Bean node to open ApplicationBean1.java

in the editing area. 2. Add the following code to the end of the file before the final end brace.

Code Sample 4: Report Generation Code /**

*

Prefix to the resource name for compiled reports.

*/

private static final String PREFIX = \

/**

*

Suffix to the resource name for compiled reports.

*/

private static final String SUFFIX = \

/**

*

Valid content types for reports that we can produce.

*/

private static final String[] VALID_TYPES =

{ \ // Standard HTML representation \ // Adobe Portable Document Format };

/**

*

Generate the specified report, in the specified output * format, based on the specified data.

*

* @param name Report name to be rendered

* @param type Content type of the requested report (\ * @param data ResultSet containing the data to report * @exception IllegalArgumentException if the specified * content type is not recognized

* @exception IllegalArgumentException if no compiled report definition * for the specified name can be found */

public void jasperReport(String name, String type, ResultSet data) {

jasperReport(name, type, data, new HashMap()); }

/**

*

Generate the specified report, in the specified output * format, based on the specified data.

*

* @param name Report name to be rendered

* @param type Content type of the requested report (\ * or \

* @param data ResultSet containing the data to report * @param params Map of additional report parameters *

* @exception IllegalArgumentException if the specified * content type is not recognized

* @exception IllegalArgumentException if no compiled report definition * for the specified name can be found

or \ */

public void jasperReport(String name, String type, ResultSet data, Map params) {

// Validate that we recognize the report type // before potentially wasting time filling the // report with data boolean found = false;

for (int i = 0; i < VALID_TYPES.length; i++) { if (VALID_TYPES[i].equals(type)) { found = true; break; } }

if (!found) {

throw new IllegalArgumentException(\ + \ }

// Look up the compiled report design resource ExternalContext econtext = getExternalContext();

InputStream stream = econtext.getResourceAsStream(PREFIX + name + SUFFIX); if (stream == null) {

throw new IllegalArgumentException(\ + \ }

// make sure cursor is in front of the first record try {

data.beforeFirst(); } catch (Exception e) {

throw new FacesException(e); }

// Fill the requested report with the specified data

JRResultSetDataSource ds = new JRResultSetDataSource(data); JasperPrint jasperPrint = null; try {

jasperPrint = JasperFillManager.fillReport(stream, params, ds); } catch (RuntimeException e) { throw e;

} catch (Exception e) {

throw new FacesException(e); } finally { try {

stream.close(); } catch (IOException e) { ; } }

// Configure the exporter to be used, along with the custom // parameters specific to the exporter type JRExporter exporter = null;

HttpServletResponse response = (HttpServletResponse) econtext.getResponse();

FacesContext fcontext = FacesContext.getCurrentInstance(); try {

response.setContentType(type); if (\ exporter = new JRPdfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); } else if (\ exporter = new JRHtmlExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter());

// Make images available for the HTML output HttpServletRequest request = (HttpServletRequest)

fcontext.getExternalContext().getRequest(); request.getSession().setAttribute(

ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setParameter(

JRHtmlExporterParameter.IMAGES_MAP, new HashMap()); // The following statement requires mapping /image // to the imageServlet in the web.xml. //

// This servlet serves up images including the px // images for spacing. //

// Serve up the images directly so we

// don't incur the extra overhead associated with // with a JSF request for a non-JSF entity.

exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + \ }

} catch (RuntimeException e) { throw e;

} catch (Exception e) {

throw new FacesException(e); }

// Enough with the preliminaries ... export the report already try {

exporter.exportReport(); } catch (RuntimeException e) { throw e;

} catch (Exception e) {

throw new FacesException(e); }

// Tell JavaServer Faces that no output is required fcontext.responseComplete(); }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Generating Reports and PDFs From a Web Application(3)在线全文阅读。

Generating Reports and PDFs From a Web Application(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/367498.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: