Monday, July 13, 2015

Jasper report with netbeans IDE, mysql,JavaEE, Primefaces. part 4

Hello guys.
This is the last part of the tutorial series.
 In this part, we will design a report using iReport plugin inside netbeans IDE.
In our web page folder, create a new folder call it jasper.



Right click on the folder and go to new>File>Other in the category, select Report and pick empty report. We want to keep things as simple.

 Give it the name employeelist.

A blank report will appear in the designer tab like this.

Drag and drop a static text in the palette and place it on the title section. This will be our report title. Double click on it to change text and edit it to My Report.
If the palette on the right is not present at default, you can open it by going to Window>IDE Tools>Palette.
Your report should now look like this.



We are going to add column headers for our report. drag and drop several static text objects and place them like this

Edit all of them and the first should be name, second IdNumber and the other one category. Make sure that you place them inside the column headers section and shrink the section as shown in the image.

in the details section, drag and drop several TextFields from the palette and arrange them like this.
We are going to pass a list of employee object to The jasper report engine. The engine will extract information about the object using fields. we need to set this field. in the report inspector, Right click on Fields and click on add field. rename the added field by right clicking on it then rename.name it 'name' without quotes. do the same for the other two. Remember the field names should match those of the objects variables.

Build the report by clicking here. If there are no errors, report should build successfully.

Open the List.xhtml inside the employee folder, locate this line
<p:commandButton id="deleteButton" icon="ui-icon-trash"  value="#{bundle.Delete}" actionListener="#{employeeController.destroy}" update=":growl,datalist" disabled="#{empty employeeController.selected}"/>
and below it add the following line
<p:commandButton id="print" icon="ui-icon-print"  value="Print" actionListener="#{employeeController.print()}" ajax="false"/>

inside the controller package, open the EmployeeController.java and add the following code just below the class

public void print() throws IOException, JRException
    {
        JasperPrint jasperPrint;
        System.out.println("calling print");
        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(getItems());
        FacesContext facesContext = FacesContext.getCurrentInstance();
        String reportPath = facesContext.getExternalContext().getRealPath("/jasper/employeelist.jasper");
        Map params = new HashMap();
        jasperPrint = JasperFillManager.fillReport(reportPath, params, beanCollectionDataSource);
        HttpServletResponse httpServletResponse = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + "all" + ".pdf");
        try (ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream()) {
            JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
            servletOutputStream.flush();
        }
        FacesContext.getCurrentInstance().responseComplete();
    }

open the porm.xml located in the Project Files folder.

Locate the </dependacies> closing tag and add the following code just above it.

<dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.1.0</version>
        </dependency>

you should have something similar to this.

This line adds jasper report library into our project. 
build the project again and run.
When you run the project, you should see a report generated when you click on the print button. The complete project with resources can be downloaded from here




1 comment:

  1. Hi,
    Please Inform regarding how to get the javabean data source to get the fields for the Report.
    Thanks
    Raichand

    ReplyDelete