我的系统:jsp+strtus+ejb+dao+db
我的思路:在Action(ro DispatchAction)组装好报表所需的参数,然后传递给统一的显示servlet
我的实现
1.显示servlet
2.参数class
3.Action
1.显示servlet(此处为html)
public class JRHtmlServlet extends HttpServlet {
/**
*
*/
public void service(
HttpServletRequest request,
HttpServletResponse response
) throws IOException, ServletException{
//得到所需参数
JRParameterInterface jreportParam = null;
jreportParam = (JRParameterInterface)request.getAttribute("jreportParam");
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
try
{
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(jreportParam.getJasperFile().getPath());
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
jreportParam.getParameters(),
jreportParam.getDataSet()
);
JRHtmlExporter exporter = new JRHtmlExporter();
Map imagesMap = new HashMap();
request.getSession().setAttribute("IMAGES_MAP", imagesMap);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "JRReport/image?image=");
exporter.exportReport();
}
catch (JRException e)
{
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Web Application Sample</title>");
// out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
out.println("<pre>");
e.printStackTrace(out);
out.println("</pre>");
out.println("</body>");
out.println("</html>");
}
}
}
2.参数class
public interface JRParameterInterface {
//set所需参数
void setParameters(Object object, Object object1);
//返回所需file
File getJasperFile();
//返回页面的parameter
Map getParameters();
//返回页面的数据,如果为空,返回 (new JREmptyDataSource()),否则返回 (new WebappDataSource(data))
JRDataSource getDataSet();
}
实现类
public class JReportParam implements JRParameterInterface {
private DataSet jrData;
private File jasperFile;
private JRDataSource jrDataSource;
private Map parameterMap;
public JReportParam(File jasperFile,DataSet data) {
this.jasperFile = jasperFile;
jrData = data;
parameterMap = new HashMap();
}
public JReportParam(File jasperFile) {
this(jasperFile,(DataSet)null);
}
public JReportParam(File jasperFile,JRDataSource JRdata) {
this.jasperFile = jasperFile;
jrDataSource = JRdata;
parameterMap = new HashMap();
}
//set所需参数
public void setParameters(Object object, Object object1){
parameterMap.put(object, object1);
};
//返回所需file
public File getJasperFile(){
return jasperFile;
};
//返回页面的parameter
public Map getParameters(){
return parameterMap;
};
//返回页面的数据,如果为空,返回 (new JREmptyDataSource()),否则返回 (new WebappDataSource(data))
public JRDataSource getDataSet(){
if (jrDataSource == null){
if ((jrData==null) || (jrData.getRetCode().equals("NO_DATA"))){
jrDataSource = new JREmptyDataSource();
}else{
jrDataSource = new WebappDataSource(jrData);
}
}
return jrDataSource;
}
}
3.Action
//需要得到绝对路径path
File reportFile = new File(path +"demo.jasper");
JReportParam jreportParam = new JReportParam(reportFile, dataset);
jreportParam.setParameters("ReportTitle", "my demo");
httpServletRequest.setAttribute("jreportParam", jreportParam);
actionForward = actionmapping.findForward(reportType);