Blogs

Part-III: Print Web Dynpro Java applications using JasperReports
Jawed Ali
Business Card
Company: Siemens Pakistan Engineering Co. Ltd.
Posted on Oct. 30, 2009 08:06 AM in Enterprise Portal (EP), Java Programming, Web Dynpro

Subscribe.Subscribe
Print. Print
Permalink Permalink
In Part-I and Part-II of the blog series we discussed about JasperReports and developed a Java example to show how we can use them in code. In the final part of the series we will discuss how we can integrate the Jasperports in WebDynpro.

 

We have already developed the code for generating PDF report; please refer to Part-II for more information. Here we will only discuss about integrating it into WebDynpro application. We will create WebDynpro application, which will take name as input and generate PDF report accordingly.

 

image

 

You need to copy HelloReport.java and HelloReport.jrxml, to respective folders as shown in the Figure 1, from the project created as part of Part-II of this blog series. If you haven’t read Part-II please read it before going further. Also if you don’t find folders as shown in the figure create them accordingly. Similarly you should also copy your JasperReport jar files to lib folder.

Your WebDynpro application should contain a view as show in the Figure 1 above.  Create an input field and its text property should be bind to a context value attribute “name” of type string. Similarly create a button and bind its onAction event to GetReport action. If you go to Implementation tab, you will see a method created onActionGetReport, this method will be the center of our development. Rest of the form elements are just decoration. Code to be placed in your onActionGetReport method is:

public void onActionGetReport(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionGetReport(ServerEvent)
    try{
        HashMap data = new HashMap();
        data.put("NAME", wdContext.currentContextElement().getName());
        ArrayList dataList = new ArrayList();
        dataList.add(data);
         
        HelloReport report = new HelloReport(dataList);
        byte[] pdfData= report.getReportData();
        String reportFileName = "HelloJasperReport.pdf";            
    
        IWDResource pdfResource = WDResourceFactory.createResource(pdfData, reportFileName, WDWebResourceType.PDF);
        String urlToFile = pdfResource.getUrl(WDFileDownloadBehaviour.AUTO.ordinal());

        IWDWindow pdfWindow = wdComponentAPI.getWindowManager().createNonModalExternalWindow(urlToFile, "View Report");
        pdfWindow.setWindowPosition(5, 5);
        pdfWindow.setWindowSize(600, 600);
        pdfWindow.show();
     }catch(Exception e)
     {       
         wdComponentAPI.getMessageManager().reportException("Couldnot generate downloadble/print version: "+e.getMessage(), true);
     }   
    //@@end
  }


Now build your WebDynpro project and run it.

Suggestions:
You could deploy all the jars separately as a external library and use them in your subsequent projects. In this way you don’t have to copy jars to your lib folder and it will make the deployment archive smaller in size.

 

Jawed Ali is a Java & NetWeaver Consultant for SIEMENS, Pakistan. He has been working on Java, J2EE and Web technologies for past seven years.


Add to: del.icio.us | Digg | Reddit


Comment on this articleTechniques discussed in this blog are specific to my project requirements, if you have suggestions to improve it please drop comments.
Comment on this weblog
Showing messages 1 through 5 of 5.

Titles Only Main Topics Oldest First

  • error in compileReport
    2009-11-03 00:04:19 Pietro Rossi Business Card [Reply]

    hello
    I found this very interesting blog ...
    I followed these steps but I found an error when it comes to this syntax:
    JasperReport report = JasperCompileManager.compileReport (HelloReport.class.getResourceAsStream ( "/ FirstExample.jrxml"));


    the error is:
    Could not generate downloadble / print version: com.sap.engine.lib.xml.parser.NestedSAXParserException: Generic Exception: -> com.sap.engine.lib.xml.util.NestedException: Validation of XML instance document failed. No external pattern is detected.


    I put the same jar that you specified.


    Have you any idea to solve this problem?
    Thanks
    Regards
    Banana

    • error in compileReport
      2009-11-04 01:29:08 Jawed Ali Business Card [Reply]

      There shouldn't be any space between "/" and "FirstExample.jrxml".


      You can download the source code from the link below:
      http://www.jawed.info/1257342000118/WDJJRTest.rar

      • error in compileReport
        2009-11-04 05:17:20 Pietro Rossi Business Card [Reply]

        Thank you for your answer
        I tried without space but the same error.


        I downloaded the rar file that you gave me.
        I put the jar libraries under the lib folder.
        The only library that I have found is the commons-logging-api-1.0.2.jar but I put the commons-logging-api-1.0.3.jar
        and compiles without problems.
        Deploy this error:
        java.lang.NoClassDefFoundError: sun.awt.X11GraphicsEnvironment (initialization failure)


        at java.lang.J9VMInternals.initialize(J9VMInternals.java:134)
        at java.lang.Class.forNameImpl(Native Method)
        at java.lang.Class.forName(Class.java:119)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:91)
        at net.sf.jasperreports.engine.util.JRGraphEnvInitializer.initializeGraphEnv(JRGraphEnvInitializer.java:58)
        ... 38 more



        ....


        In Detailed Error Information
        Detailed Exception Chain:
        java.lang.NoClassDefFoundError: sun.awt.X11GraphicsEnvironment (initialization failure)
        at java.lang.J9VMInternals.initialize(J9VMInternals.java:134)
        at java.lang.Class.forNameImpl(Native Method)
        at java.lang.Class.forName(Class.java:119)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:91)
        at net.sf.jasperreports.engine.util.JRGraphEnvInitializer.initializeGraphEnv(JRGraphEnvInitializer.java:58)
        at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:363)
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:77)
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:59)
        at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:147)
        at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:83)
        at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)
        at com.sap.jasper.HelloReport.getReportData(HelloReport.java:37)
        at com.sap.jasper.WDJJRTestView.onActionEstraiNome(WDJJRTestView.java:160)
        at com.sap.jasper.wdp.InternalWDJJRTestView.wdInvokeEventHandler(InternalWDJJRTestView.java:140)
        at com.sap.tc.webdynpro.progmodel.generation.DelegatingView.invokeEventHandler(DelegatingView.java:87)
        at com.sap.tc.webdynpro.progmodel.controller.Action.fire(Action.java:67)
        at com.sap.tc.webdynpro.clientserver.window.WindowPhaseModel.doHandleActionEvent(WindowPhaseModel.java:420)
        at com.sap.tc.webdynpro.clientserver.window.WindowPhaseModel.processRequest(WindowPhaseModel.java:132)
        at com.sap.tc.webdynpro.clientserver.window.WebDynproWindow.processRequest(WebDynproWindow.java:335)
        at com.sap.tc.webdynpro.clientserver.cal.AbstractClient.executeTasks(AbstractClient.java:143)
        at com.sap.tc.webdynpro.clientserver.session.ApplicationSession.doProcessing(ApplicationSession.java:319)
        at com.sap.tc.webdynpro.clientserver.session.ClientSession.doApplicationProcessingStandalone(ClientSession.java:713)
        at com.sap.tc.webdynpro.clientserver.session.ClientSession.doApplicationProcessing(ClientSession.java:666)
        at com.sap.tc.webdynpro.clientserver.session.ClientSession.doProcessing(ClientSession.java:250)
        at com.sap.tc.webdynpro.clientserver.session.RequestManager.doProcessing(RequestManager.java:149)
        at com.sap.tc.webdynpro.serverimpl.defaultimpl.DispatcherServlet.doContent(DispatcherServlet.java:62)
        at com.sap.tc.webdynpro.serverimpl.defaultimpl.DispatcherServlet.doPost(DispatcherServlet.java:53)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)



        You have any suggestions for me?
        Thanks
        Banana


Showing messages 1 through 5 of 5.