Java Reference
In-Depth Information
Integrating JasperReports with
JavaServer Faces
JavaServer Faces (JSF) is the standard technology for developing user interfaces for
Java server applications. In theory, JavaServer Faces is view technology agnostic (that
is, it can be used to develop user interfaces for a variety of technologies). However,
in practice, JSF is almost always used to develop web applications.
JSF applications typically consist of JSP and backing beans, with the latter serving as
controllers for the former. JSF and JasperReports integration can be accomplished by
creating a backing bean that will generate a report. The following example illustrates
this technique:
package net.ensode.jsf;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
public class ReportGenerator
{
public void generateReport(ActionEvent actionEvent)
throws ClassNotFoundException, SQLException, IOException,
JRException
{
Connection connection;
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)
context.getExternalContext().getResponse();
InputStream reportStream = context.getExternalContext().
getResourceAsStream( " /reports/DbReport.jasper");
ServletOutputStream servletOutputStream =
response.getOutputStream();
 
Search WWH ::




Custom Search