Jump to content
We've recently updated our Privacy Statement, available here ×

How to generate a report using Struts/EJB?


CharleyDC5

Recommended Posts

Hi folks,

I am actually using IBM DB2 UDFs (User Defined Functions) as a datasource for all my reports. I use iReport as my report designer and JasperReports as my report engine. I would like to stop having to mess with DB2 UDFs for my reporting needs. Reports are getting launched from a J2EE web application.

I am looking for a way to pass a Collection of objects to my report so I could re-use some of my listing methods that feeds the Web application itself. For example, I have a Cars Management screen that allows the user to list, add, update, delete and view cars. In the list screen, the user can list the cars by using a filter (let's say : Brand). So, I can list all Honda car by selecting Honda as my Brand filter. All the results are printed dynamically in the web application (DB2 to EJB to Struts to JSP/HTML).

Now, let's say I have a button in that list screen that allows the user to generate a PDF report (better presentation). Here is my problem. Right now, the report is getting launched without a problem, but I have to use a UDF in order to get a resultset for my report. I would really like to use the same method that I used in the Cars list screen as it is the same content.

Attached is how I actually generate my reports.

Can I pass my Collection of Cars value objects to my report?

Here is what I use :
- Persistence framework : EJB 2.0
- MVC (Model View Controller) framework: Struts 1.2.9
- Report designer : iReport 3.7.1
- Report engine : JasperReports 3.7.1
- Application server : jBoss 4.0.4GA
- DBMS (Database Management System) : IBM DB2 UDB 9.5 Workgroup Edition

Thank you VERY much for help.

Charles

Code:
public ActionForward showCarsRpt(ActionMapping mapping,                         ActionForm form, HttpServletRequest request,                         HttpServletResponse response) throws ParseException, SQLException {                          ServletContext context = servlet.getServletConfig().getServletContext();                       CarRptFilterForm carRptFilterForm = (CarRptFilterForm) form;                                              reportFile = new File(context.getRealPath("/reports/car/cars.jasper"));                                if (!reportFile.exists()) {                         throw new JRRuntimeException("File car.jasper not found. The report design must be compiled first.");                 }                                  Map parameters = new HashMap();                 parameters.put("ReportTitle", "Car Report");                 parameters.put("BaseDir", reportFile.getParent() + "\\");                           parameters.put("REPORT_LOCALE", request.getSession().getAttribute("org.apache.struts.action.LOCALE"));                 parameters.put("strLang", getLocale(request).getLanguage());                 parameters.put("intBrandId", Integer.valueOf(carRptFilterForm.getStrCentreCoutId()));                                                  Connection con = null;                        try {           InitialContext ctx = new InitialContext();           DataSource ds = (DataSource) ctx.lookup("java:/myDataSource");           con = ds.getConnection();           con.setReadOnly(true);                 } catch (Exception e) {                         e.printStackTrace();                 }        try {           ReportTools.exportReportToPDF(reportFile, parameters, con, response);       } catch (JRException e) {           e.printStackTrace();       } catch (SQLException e) {           e.printStackTrace();       } finally {           con.close();       }                  return null;               }public static void exportReportToPDF(File reportFile, Map parameters,        Connection con, HttpServletResponse response)        throws JRException, SQLException {		        File destFile = new File(JasperRunManager.runReportToPdfFile(reportFile.getPath(), parameters, con));        con.close();        response.setContentType("application/pdf");        response.setHeader("Content-Disposition", "attachment; filename=\"" + destFile.getName() + "\";)");        java.io.FileInputStream fileInputStream = null;        try {            fileInputStream = new java.io.FileInputStream(destFile.getPath());        } catch (FileNotFoundException e) {            e.printStackTrace();        }        try {            ServletOutputStream outputStream;            int i;            outputStream = response.getOutputStream();            while ((i = fileInputStream.read()) != -1) {                outputStream.write(i);            }            outputStream.flush();            fileInputStream.close();            outputStream.close();        } catch (IOException e) {        }    }
Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Thanks for the info!

Is it possible to have a Collection inside the CustomBean? How would Jasperreports/iReport deal with it for grouping?

For instance :

public class AbsenceCustomBean {
 private String date;
 private String phonenumber;
 private String description;
 private Collection updates;

 public AbsenceCustomBean(String date, String phonenumber, String description, Collection updates) {
  date= date;
  phonenumber = phonenumber;
  description = description;
  updates = updates;
 }

 Every instance of the AbsenceCustomBean may have 1 or more updates (1-N relationship). So, I would like to print something similar to that :

2010-10-12      888.999.3333      Off sick
        Updated by USR111
        Updated by USR333
        Updated by USR222
        Updated by USR111

2010-10-13      777.333.2222       Business travel to Toronto
        Updated by USR999
        Updated by USR048
        Updated by USR982
 

etc.. etc..

Other questions :

- Did you create the fields manually in iReport or you added the application's JAR file into the iReport classpath?

- Would it be possible to use full Java object code in the JRXML report? I mean, instead of creating a manual factory class that represents a row in my report, can I write some pure Java just like in Eclipse? I.E. : Absence.getUpdates().getStrUpdaterLoginName() in order to print USR999.

Thank you very much for help

Charles

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 year later...

Everytime I got one error :

javax.servlet.ServletException: Servlet execution threw an exception

In this line::

File destFile = new File(JasperRunManager.runReportToPdfFile(reportFile.getPath(), parameters, con));

 

How am I overcome it. Please help me. It's urgent.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...