3 min read

Adding static views to Roo-generated web application

A static view in a Spring Web MVC application is a view for which you don’t explicitly create a controller class. We saw earlier that Spring Web MVC application scaffolded by Roo configures static views using element of Spring’s mvc schema. The static views don’t have an explicit controller, but behind the scenes Spring’s built-in ParameterizableViewController is used for rendering static views. Here, we will look at web mvc install view command of Roo, which creates a static view.

Getting ready

Delete contents of ch04-recipe sub-directory inside C:roo-cookbook directory.

Copy ch04_web-app.roo script into ch04-recipe directory.

Execute ch04_web-app.roo script that creates flight-app Roo project, sets up Hibernate as persistence provider, configures MySQL as the database for the application, creates Flight and FlightDescription JPA entities and defines many-to-one relationship between Flight and FlightDescription entities. If you are using a different database than MySQL or your connection settings are different than what is specified in the script, then modify the script accordingly.

Start Roo shell from C:roo-cookbookch04-recipe directory.

Execute the controller all command to create controllers and views corresponding to JPA entities in flight-app project, as shown here:

.. roo> controller all --package ~.web

Execute perform eclipse command to update project’s classpath settings, as shown here:

.. roo> perform eclipse


Now, import the flight-app project into your Eclipse IDE.

How to do it…

To add static views to a Roo-generated web application execute the web mvc install view command, as shown here:

.. roo> web mvc install view –path /static/views
–viewName help –title Help


How it works…

The following table describes the arguments that web mvc install view command accepts:

Argument Description
path Specifies the sub-folder inside /WEB-INF/views/ folder in which the view is created.
viewName The name of the view JSPX file.
title Specifies the name of the menu option with which the static view is accessible.

As the output from the web mvc install view command suggests, following actions are taken by Spring Roo in response to executing the command:

  • Creates /static/views directory inside /WEB-INF/views folder. Roo uses the value of path argument to determine the directory to create.
  • Creates help.jspx file inside /WEB-INF/views/static/views directory. The value of viewName argument is used as the name of the JSPX file.
  • Adds a property with value Help to application.properties, that is, the value of title argument is used as the value of the newly added property. The property is used by menu.jspx to show a Help menu option. The Help menu option allows access to the newly created help.jspx view.
  • Creates /WEB-INF/views/static/views/views.xml tiles definitions XML file, containing a single tiles definition for showing help.jspx view, as shown here:

    <tiles-definitions>
    <definition extends=”default” name=”static/views/help”>
    <put-attribute name=”body”
    value=”/WEB-INF/views/static/views/help.jspx”/>
    </definition>
    </tiles-definitions>

    
    
  • Adds a element to webmvc-config.xml to allow accessing help.jspx view without requiring to write a controller, as shown here:

    <mvc:view-controller path=”/static/view/help”/>

    
    

     

LEAVE A REPLY

Please enter your comment!
Please enter your name here