Adding static views to a 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 the Spring Web MVC application scaffolded by Roo configures static views using the <view-controller> 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. Refer to the Auto-generating Spring MVC controllers and JSPX views from JPA entities recipe for details on pre-configured static views in the Roo generated web application.

In this recipe, we will look at the web mvc install view command of Roo, which creates a static view.

Getting ready

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

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

Execute the ch04_web-app.roo script that creates the flight-app Roo project, sets up Hibernate as the persistence provider, configures MySQL as the database for the application, creates the Flight and FlightDescription JPA entities and defines a many-to-one relationship between the 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 the Roo shell from the C: oo-cookbookch04-recipe directory.

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

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

Execute the perform eclipse command to update the 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

Created SRC_MAIN_WEBAPPWEB-INFviewsstaticviews
Created SRC_MAIN_WEBAPPWEB-INFviewsstaticviewshelp.jspx
Managed SRC_MAIN_WEBAPPWEB-INFi18napplication.properties
Managed SRC_MAIN_WEBAPPWEB-INFviewsmenu.jspx
Created SRC_MAIN_WEBAPPWEB-INFviewsstaticviewsviews.xml
Managed SRC_MAIN_WEBAPPWEB-INFspringwebmvc-config.xml

How it works...

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

Argument

Description

path

Specifies the sub-folder inside the /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, the following actions are taken by Spring Roo in response to executing the command:

  • Creates a /static/views directory inside the /WEB-INF/views folder. Roo uses the value of the path argument to determine the directory to create.
  • Creates a help.jspx file inside the /WEB-INF/views/static/views directory. The value of the viewName argument is used as the name of the JSPX file.
  • Adds a property with value Help to the application.properties, that is, the value of the 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 a /WEB-INF/views/static/views/views.xml tiles definitions XML file, containing a single tiles definition for showing the 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 <view-controller> element to the webmvc-config.xml to allow access to the help.jspx view without requiring to write a controller, as shown here:
    <mvc:view-controller path="/static/view/help"/>

See also

  • Refer to the Manually creating a Spring MVC controller for a JPA entity recipe for details on how to create a custom controller and view
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.138.104