(For more resources on Spring, see here.)
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
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:
|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:
<definition extends="default" name="static/views/help">
- Adds a
element to webmvc-config.xml to allow accessing help.jspx view without requiring to write a controller, as shown here: