Spring Roo 1.1: Working with Roo-generated Web Applications

Exclusive offer: get 80% off this eBook here
Spring Roo 1.1 Cookbook

Spring Roo 1.1 Cookbook — Save 80%

Over 60 recipes to help you speed up the development of your Java web applications using the Spring Roo development tool

₨924.00    ₨184.80
by Ashish Sarin | September 2011 | Open Source

Spring Roo is an easy-to-use tool for rapidly developing Java enterprise applications using well-recognized frameworks such as Spring, Hibernate, AspectJ, Spring Web Flow, Spring Security, GWT, Flex, and so on. Spring Roo takes care of creating project structure for your enterprise application, adding support to use Maven for building and deploying the application, creating application architecture based on your choice of technologies, creating unit and integration tests based on your choice of testing framework, and so on.

In this article by Ashish Sarin, author of Spring Roo 1.1 Cookbook, we'll look at how Spring Roo simplifies developing the web layer of an enterprise application using Spring Web MVC. We'll also look at how Spring Roo lets us quickly test our web application locally.

 

(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 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-cookbook\ch04-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"/>

Spring Roo 1.1 Cookbook Over 60 recipes to help you speed up the development of your Java web applications using the Spring Roo development tool
Published: September 2011
eBook Price: ₨924.00
Book Price: ₨1,540.00
See more
Select your format and quantity:

 

(For more resources on Spring, see here.)

Internationalizing Roo-generated web applications

Roo support internationalization of the complete UI by using resource bundles for labels and messages. Here, we will look at web mvc install language command of Roo and see how it simplifies internationalizing Roo-generated web user interface.

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-cookbook\ch04-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...

For internationalizing Roo-generated web user interface execute web mvc install language, as shown here:

.. roo> web mvc install language --code es

Created SRC_MAIN_WEBAPP\WEB-INF\i18n\messages_es.properties
Created SRC_MAIN_WEBAPP\images\es.png
Managed SRC_MAIN_WEBAPP\WEB-INF\views\footer.jspx

How it works...

The web mvc install language command accepts a single argument: code, which identifies the language code for which the support needs to be added to the web application. The code argument accepts a pre-defined language code, depending upon the languages supported by Spring Roo. Spring Roo contains translations for the standard messages and labels for the following language codes: de (German), en (English), es (Espanol), it (Italian), nl (Dutch), and sv (Swedish).

When web mvc install language command is executed, Roo processes the command by taking following actions:

  • Creates messages_es.properties in /WEB-INF/i18n/ folder
  • Copies an image icon (es.png) for the language in images directory
  • Updates /WEB-INF/views/footer.jspx to show the image icon for the language

To check if the support for the Espanol language is correctly installed, deploy and run flight-app application. The following screenshot shows the home page of flight-app application, after Espanol language support is added:

Spring Roo 1.1: Working with Roo-generated Web Applications

As the given screenshot shows, an additional image icon is displayed to allow users to change language of the web application to Espanol. When a user clicks the image icon corresponding to a language, lang request parameter is set in the request, which is used by LocaleChangeInterceptor (configured in /WEB-INF/spring/webmvc-config.xml) for changing the current locale.

The CookieLocaleResolver configured in /WEB-INF/spring/webmvc-config.xml stores a cookie named locale in the browser, so that users don't need to change their preferred language every time they access the web application.

There's more...

The messages.properties contains messages and labels that are common to all Roo-generated web applications and application.properties contains application-specific messages and labels.

As Roo can't provide translations for application-specific messages and labels, web mvc install language command doesn't create an application_.properties file. It is left up to the developer to create application_.properties file for specific language codes and provide translations.

Summary

In this article, we saw how the web mvc install view command of Roo is used to create a static view. We also saw, how the web mvc install language command of Roo simplifies the internationalizing of Roo-generated web user interface.


Further resources on this subject:


Spring Roo 1.1 Cookbook Over 60 recipes to help you speed up the development of your Java web applications using the Spring Roo development tool
Published: September 2011
eBook Price: ₨924.00
Book Price: ₨1,540.00
See more
Select your format and quantity:

About the Author :


Ashish Sarin

Ashish Sarin has more than 11 years of experience architecting and developing web applications and portals using Java EE and the Portlets API. He has authored many articles on portlets and rich internet applications using Spring, Liferay, DWR, DOJO, JSF, and Spring Portlet MVC. He is also the author of Portlets in Action published by Manning Publications.

Books From Packt


Spring Security 3
Spring Security 3

Spring Python 1.1
Spring Python 1.1

JSF 2.0 Cookbook
JSF 2.0 Cookbook

OSGi and Apache Felix 3.0 Beginner's Guide
OSGi and Apache Felix 3.0 Beginner's Guide

Spring Roo 1.1 Cookbook book and eBook
Spring Roo 1.1 Cookbook book and eBook

Learning Vaadin: RAWy
Learning Vaadin: RAW

Liferay Portal Systems Development: RAW
Liferay Portal Systems Development: RAW

Learning jQuery 1.3
Learning jQuery 1.3


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software