Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
...nobody




JSPWiki v2.2.33

[RSS]


Hide Menu

AppFuseJasperReports


This is version 39. It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]


JasperReport on AppFuse - Putting JasperReports to work on AppFuse.

About this Tutorial

This tutorial will show you how to add a Controller to generate reports from AppFuse database using JasperReports.

Table of Contents

  • [1] Create the UserReportController Controller
  • [2] Configuring JasperReports on AppFuse
  • [3] Designing Reports
  • [4] Putting all together

Create the UserReportController Controller [#1]

One of the forms to call a report is to implement a new controller. We go to use one of the great characteristic that Spring MVC possess, the possibility of implement/extend one of existing controllers - one in special, MultiActionController. For that we will create the UserReportController controller, which will extends from MultiActionController controller:


public class UserReportController extends MultiActionController {
    private transient final Log log = LogFactory.getLog(UserReportController.class);
    private UserManager userManager = null;

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager mgr) {
        this.userManager = mgr;
    }

    public ModelAndView userReport(HttpServletRequest request, HttpServletResponse response)throws Exception {
      if (log.isDebugEnabled()) {
        log.debug("entering 'userReport' method...");
      }
      Map model = new HashMap();
      //It is the path of our webapp - is there a better way to do this?
      String reportResourcePath =  getServletContext().getRealPath("/");

      String format = request.getParameter("format");
      //Default format to pdf
      if (StringUtils.hasText(format)){
        if (!(format.equalsIgnoreCase("pdf"|| format.equalsIgnoreCase("html")
            || format.equalsIgnoreCase("csv"|| format.equalsIgnoreCase("xls"))){
            format = "pdf";
        }
      }else{
        format = "pdf";
      }

        model.put("format", format);
        model.put("WEBDIR", reportResourcePath);
        model.put("dataSource", getUserManager().getUsers(new User()));
        return new ModelAndView("userMultiFormatReport", model);
    }
}

MultiActionController resolves method names based on a method name resolver. The default method name resolver is InternalPathMethodNameResolver, which resolves method names based on URL patterns. But Spring comes with two other method name resolvers:

  • ParameterMethodNameResolver—Resolves the execution method name based on a parameter in the request
  • PropertiesMethodNameResolver—Resolves the name of the execution method by consulting a list of key/value pairs

Regardless of which method name resolver you choose, you’ll need to wire it into the methodNameResolver property of the MultiActionController to override the default. In the configuration file action-servlet.xml, makes the following modifications - we will use the first method to decide names:


‹bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"
    ‹property name="paramName"
       ‹value›action‹/value›
    ‹/property›
‹/bean›

‹bean id="userReportController" class="org.appfuse.webapp.action.UserReportController"
     ‹property name="methodNameResolver" ref="methodNameResolver"/›
     ‹property name="userManager" ref="userManager"/›
‹/bean›

The requests would be of the following form: /appfuse/userReport.html?action=userReport and thus for ahead, for each method that you add in the class userReportController. Another thing that we do not have to forget is to establish which URL will use to call this controller. As this URL is one of the administrative tasks, we will go to place this mapping in the Bean adminUrlMapping:


        ‹property name="mappings"
            ‹props›
                ‹prop key="/activeUsers.html"›filenameController‹/prop›
                ‹prop key="/users.html"›userController‹/prop›
                ‹prop key="/userReport.html"›userReportController‹/prop›
                ‹prop key="/flushCache.html"›filenameController‹/prop›
                ‹prop key="/reload.html"›reloadController‹/prop›
            ‹/props›
        ‹/property›

Now the most important, let's verify if everything is correct through the execution of the test of unit for this class. We already know that the current test will fail! This because we do not create our report and therefore we do not define who will resolve the name of the JasperReports's report file, userList.jrxml. Then, in our test class UserReportControllerTest, we have to test the existence of view userReport with format pdf:


public class UserReportControllerTest extends BaseControllerTestCase {
    private UserReportController c;
    private MockHttpServletRequest request;
    private ModelAndView mav;

    protected void setUp() throws Exception {
        // needed to initialize a user
        super.setUp();
        c = (UserReportControllerctx.getBean("userReportController");
    }

    protected void tearDown() {
        c = null;
    }

    public void testUserReportDefalutPdf() throws Exception {
        mav = c.userReport(new MockHttpServletRequest(),
                (HttpServletResponsenull);
        Map m = mav.getModel();
        assertEquals("Default format is pdf",m.get("format").toString(),"pdf");
        assertEquals(mav.getViewName()"userMultiFormatReport");
    }
}

Execute ant test-web -Dtestcase=UserReportController.

Configuring JasperReports on AppFuse [#2]

Now the necessary steps to (AppFuseAddLibrary) set up JasperReports on AppFuse. For that, we will include the lastest release version of JasperReports, which in the hour of this writing was 1.2.0. JasperReports itself depends on the following projects: * BeanShell * Commons BeanUtils - it comes with AppFuse * Commons Collections - it comes with AppFuse * Commons Digester - it comes with AppFuse * Commons Logging - it comes with AppFuse * iText * POI


Attachments:
ReportsTest.java Info on ReportsTest.java 891 bytes
Reports2.java Info on Reports2.java 2600 bytes
UserReportControllerTest.java Info on UserReportControllerTest.java 963 bytes
userreportview.png Info on userreportview.png 17481 bytes
lib.properties Info on lib.properties 6258 bytes
UserReportController.java Info on UserReportController.java 2074 bytes
Reports.java Info on Reports.java 2384 bytes
userreport-smaller.png Info on userreport-smaller.png 128635 bytes
action-servlet.xml Info on action-servlet.xml 7044 bytes
userList.jrxml Info on userList.jrxml 20590 bytes
userreport.png Info on userreport.png 83924 bytes


Go to top   More info...   Attach file...
This particular version was published on 06-Nov-2006 13:52:54 MST by Gilbertoca.