Introducing Spring Roo, Part 6: Develop Spring MVC and GWT applications using Spring Roo 1.2 and deploy them on Cloud Foundry

Quickly develop and deploy Spring MVC and GWT apps

Spring Roo advanced considerably with the release of Version 1.2. This article introduces the new features in the current release like multi-module project, MongoDB support, JSF support, and many more. You then will develop a multi-module GWT web application using Spring Roo and deploy the application to Cloud Foundry.

Share:

Shekhar Gulati, Senior Java Consultant, Xebia

Photo of Shekar GulatiShekhar Gulati is a Java consultant working with Xebia India. He has six years of enterprise Java experience. He has extensive experience in Spring portfolio projects, such as Spring, Spring-WS, and Spring Roo. His interests are Spring, NoSQL databases, Hadoop, RAD frameworks like Spring Roo, cloud computing (mainly PaaS services like Google App Engine, CloudFoundry, OpenShift), Hadoop. He is an active writer and writes for JavaLobby, Developer.com, IBM developerWorks and his own blog at http://whyjava.wordpress.com/. You can follow him on twitter @ http://twitter.com/#!/shekhargulati.



07 September 2012

Also available in Chinese Russian Japanese Portuguese

Features in Spring Release 3.1

Get more information about the features Spring Release 3.1, the latest and greatest release of the Spring framework:

In Part 1 and Part 2 of this series, you developed a conference application using Spring Roo 1.1. In Part 3 and Part 5 you developed Spring Roo add-ons. In between, in Part 4, you looked at Cloud Foundry and Spring Roo integration. Since Part 2, we have not discussed the many new features or changes introduced in Spring Roo over the last year. Many commands which worked in previous versions no longer work or are deprecated. Another important facet of the current version is the use of Spring Release 3.1, which is the latest and greatest release of the Spring framework with features like Profile, Environment abstraction, Cache abstraction, and many more. Working with the current version of Spring Roo means utilization of the latest version of the Spring framework. In this article, you will take a quick look at the latest version of Spring Roo, that is Version 1.2.1, and then create a multi-module Google Web Toolkit (GWT) and Spring Web Model–View–Controller (MVC) application which you will then deploy to Cloud Foundry.

Introducing Spring Roo 1.2

Further explore Spring Roo features

Spring Roo is a command line rapid application development tool for building Spring applications. Spring Roo marched ahead in the past year with major releases 1.1.5 and 1.2, which made Spring Roo more powerful and enterprise developer-friendly. The major features or changes introduced in these releases are:

  1. MongoDB support: With older versions of Spring Roo, Spring Roo applications required RDBMS (such as MySQL or PostgreSQL) as the back end, except for Google App Engine BigTable support (but it never worked with relationships). Beginning in Version 1.2, MongoDB can be used as the datastore. MongoDB is a popular NoSQL document datastore. The ability to build Spring MongoDB applications showcases the extensible nature of Spring Roo. To view all the MongoDB-related commands, type:
    help --command mongo
  2. JavaServer Faces (JSF) 2.0 support: Prior to version 1.2, by default Spring Roo supported GWT and Spring MVC as view options, but with version 1.2, Spring Roo also supports JSF 2.0 as one of the view options. Spring Roo currently supports Apache MyFaces and Oracle Mojarra JSF implementations, and PrimeFaces as its component library. It was one of the popular JIRA issues which was resolved in version 1.2. To view all the available commands for JSF, type:
    roo> help --command "web jsf" 
    * web jsf all - Create JSF managed beans for all entities  
    * web jsf media - Add a cross-browser generic player to embed multimedia content  
    * web jsf scaffold - Create JSF managed bean for an entity  
    * web jsf setup - Set up JSF environment

    This article will not cover building JSF 2.0 applications, which may be covered in future articles. Developers wishing to build JSF applications can try the bikeshop.roo application, which comes bundled with Spring Roo. A samples folder is in the Roo distribution.

  3. Multi-module Maven project support: This is the one most popular feature requests demanded by the community which is now available. Before Version 1.2, all application code had to be written in one Maven project. This was inhibitive in building enterprise applications which commonly have different submodules for different application concerns like web, persistence, service, and others. Now a parent project can define different submodules for persistence, service, and web. This will be covered in detail later in this article where you learn to build a multi-module Maven project using Spring Roo.
  4. Repository and service layer support: Until Version 1.2, Spring Roo did not provide the way to have Repository (or DAO) and service layer in an application. By default, it supported Active Record pattern which ensured rich entity classes with all the persistence-related methods. This was also one of the popular features requested by the community which is now available. By default, Spring Roo still supports Active Record as the implementation strategy, but a repository can instead be chosen by creating entity with –activeRecord false. This will also be covered in detail later in this article.
  5. More robust and powerful database support for reverse engineering support: Although covered in Part 2, this add-on has gone through major improvements over the last year. Some of the improvements made are multi-schema support, allowing DBRE to make database connection through Java Naming and Directory Interface (JNDI), reverse engineering views, and support for repositories instead of Active Record entity objects. In addition, many bugs were fixed. DBRE is not in scope of this article. For more details, refer to the Spring Roo documentation in Resources.
  6. Improved Spring Roo Add-on API: Part 5 covered creating Spring Roo add-ons. In that article, I used Spring Roo Version 1.2 because of the changes to the API. Some classes which existed before Version 1.2 no longer exist or are deprecated in Version 1.2. The Spring Roo team made the API more consistent and in-line with the current Spring Roo add-ons. Updating your add-ons to Version 1.2 may be problematic.
  7. Improved GWT support: Spring Roo GWT support has improved in the latest releases. Earlier, one single command gwt setup did all the magic from creating proxies and request to other UI-related items. This single command was deprecated and replaced with the 10 commands in Listing 1. GWT will be covered later in this article.
    Listing 1. Example of web gwt help
    roo> help --command "web gwt"
    * web gwt all - Locates all entities in the project and creates GWT requests, 
         proxies, and creates the scaffold
    * web gwt gae update - Updates the GWT project to support GAE
    * web gwt proxy all - Locates all entities in the project and creates GWT proxies
    * web gwt proxy request all - Locates all entities in the project and creates GWT 
         requests and proxies
    * web gwt proxy request type - Creates a proxy and request based on 
         the specified type
    * web gwt proxy type - Creates a GWT proxy based on the specified type
    * web gwt request all - Locates all entities in the project and creates GWT requests
    * web gwt request type - Creates a GWT proxy based on the specified type
    * web gwt scaffold - Creates a GWT request, proxy and scaffold for the specified type
    * web gwt setup - Install Google Web Toolkit (GWT) into your project

Apart from the above-mentioned features or improvements, I will highlight other minor changes throughout this article. Let's start with creating a new Spring Roo 1.2 conference application.


Getting started with Spring Roo 1.2

Download and install these prerequisites. (See Resources.)

  1. Java™ 6 package
  2. Apache Maven 3
  3. Spring Roo 1.2 and above

This article uses Spring Roo 1.2.1, which is the current Spring Roo release.

Building the conference application

You will create the same conference application created in Part 1 and Part 2. A Speaker can give one or more talk and Talk will be given by only one speaker. Figure 1 shows the simple class diagram. (Speaker contains firstName, lastName, email, organization, birthDate, age, gender and Talk contains title and description.)

Figure 1. Class diagram of the Speaker and Talk tables
Diagram of the schema showing the Speaker and Talk tables joined

Creating a multi-module project

Rather than create the whole project in one Maven project, you will create a parent conference project which will contain two submodules: core and view. The core module will contain all the domain, repository, and service classes. The view submodule will also be a Project Object Model (POM) project which will have two submodules: MVC and GWT. The MVC project will contain Spring MVC application and GWT project will have GWT application. The conference application has two UIs: One based on Spring MVC and the other based on GWT. Figure 2 shows the project structure.

Figure 2. Maven project structure
Screen capture showing the file structure of the Maven project

The structure in Figure 2 resembles enterprise applications with many submodules. Now let's create this using Spring Roo.

  1. Open your operating system command-line shell.
  2. Create a directory named conference using the mkdir command.
  3. Go to the conference directory in your shell.
  4. Type roo. This command will initiate the Roo shell.
  5. The first command entered on the Roo shell is project command. With Version 1.2, this command has been updated to support POM projects. The POM projects themselves do not contain any code; they only have a pom.xml file. The packaging of these projects is POM instead of JAR or WAR. To create a conference POM project, type:
    project --topLevelPackage org.xebia.conference --projectName conference 
            --java 6  --packaging POM

    The command looks similar to the project command used in Part 1 with just one change: It has one other attribute called packaging. The packaging attribute can take one value of BUNDLE, JAR, POM or WAR. For the parent project, the value is POM. From Version 1.2, the project command can take one more attribute called parent. As the name suggests, for a project POM to inherit from some other POM, use the parent attribute. This will be showcased when you create a submodule. View the pom.xml file generated by this command and verify that the pom.xml has the packaging POM.

  6. With the conference POM project created, create core and view submodules using the module command. The module command is similar to the project command, but it creates Maven modules instead of Maven projects. To create the core module, type the following command:
    module create --moduleName core --topLevelPackage org.xebia.conference.core 
         --parent org.xebia.conference:conference:0.1.0.BUILD-SNAPSHOT

    The module create command requires two mandatory attributes: moduleName and topLevelPackage. The moduleName attribute is used to specify the name of the module and topLevelPackage attribute is used to specify the package name of the module. topLevelPackage will also be the groupId of this module. Apart from these mandatory attributes, there are three optional attributes:

    • –-java to specify Java version
    • --packaging to specify the Maven packaging of module
    • --parent to specify whether this module inherits from some other Maven project

    In the command shown above, core module is inheriting from conference project. The parent attribute takes value in the form groupId:artifactId:version. These values can be obtained from conference/pom.xml file. The pom.xml file of the core module shows that it has parent tag (as in Listing 2).

    Listing 2. Parent section of pom.xml file
       <parent> 
            <groupId>org.xebia.conference</groupId> 
            <artifactId>conference</artifactId> 
            <version>0.1.0.BUILD-SNAPSHOT</version> 
        </parent>

    You can also take a look at parent project pom.xml, that is conference/pom.xml, and verify that it has a module tag containing the core module (as in Listing 3).
    Listing 3. Module section of pom.xml file
    <modules> 
       <module>core</module> 
    </modules>
  7. Next, let's create the view submodule. Before you can create that, however, shift your focus back to root project with this module focus command:
    module focus --moduleName ~

    The tilde (~) specifies the root project. Optionally, type the name of the Maven project or Maven module. For example, the following command achieved the same result of switching to core project:

    module focus --moduleName core
  8. Now create the view module. This module will also be the POM project which will have two submodules: MVC and GWT. To create the view module, type:
    module create --moduleName view --topLevelPackage org.xebia.conference.view 
         --packaging POM --java 6
  9. To create the GWT and MVC module submodules, execute the commands in Listing 4:
    Listing 4. Creating GWT and MVC
    module create --moduleName gwt --topLevelPackage org.xebia.conference.view.gwt 
       --java 6 --parent org.xebia.conference.view:view:0.1.0.BUILD-SNAPSHOT 
    
    module focus --moduleName view 
    
    module create --moduleName mvc --topLevelPackage org.xebia.conference.view.mvc
       --java 6 --parent org.xebia.conference.view:view:0.1.0.BUILD-SNAPSHOT

The above nine steps result in a multi-module project with four submodules: core, view, GWT, and MVC. The GWT and MVC are submodules of view submodule. Now, fire the perform package command from within Roo shell, which will execute a full build of all the submodules.

Adding persistence

Now that you have created the Maven module structure, you can add persistence support. Use the Java Persistence API (JPA) setup command to setup persistence-related configuration. The persistence setup command is deprecated so use of persistence setup command is not recommended. Persistence setup was deprecated because Spring Roo now supports different types of persistent datastore solutions like RDBMS and document datastore like MongoDB. Persistence setup was a rather generic name, therefore, in the future if Spring Roo decides to add support for some other NoSQL database like Cassandra, expect the command to be a Cassandra setup. To add persistence support to the application, type the command in Listing 5. You'll do this in the core module, so first switch to core module with the module focus command.

Listing 5. Command to add persistence support
module focus --moduleName core 
jpa setup --database DERBY_EMBEDDED --provider HIBERNATE

Spring Roo now supports fifteen databases, so it is likely that you can choose your database. The JPA setup command, like persistence setup command, has two required attributes: database and provider, that is a JPA provider like Hibernate, or Datanucleus in the case of Google App Engine. The remaining attributes are the same as they were with persistence command, so I will not discuss them here.

Clearing the Roo Shell

Previously, you might have tried to clear the Roo shell using the clear or cls command. To run operating system commands now, type ! <os command>. To clear the command, type:

core roo> ! clear

Creating Speaker and Talk entities

If you enter the hint command, it will advise creating an entity. The entity command has changed to entity JPA to distinguish them from MongoDB entities, which are created using entity mongo command. Another major change to entity command is the choice of having Active Record entities or not. In this article, you will use Repositories instead of Active Record entities. To create Speaker and Talk entities, type the commands in Listing 6:

Listing 6. Creating entities
entity jpa --class ~.domain.Speaker --testAutomatically 
     --activeRecord false --serializable 
entity jpa --class ~.domain.Talk --testAutomatically 
     --activeRecord false –serializable

Listing 6 explicitly asks Roo to not generate activeRecord entities. The default mechanism is still activeRecord, therefore, you must specify activeRecord false or it will generate Active Record entities.

Adding fields to entities

Field command is the same and nothing has changed in its syntax. Type the commands in Listing 7 to add fields to entity classes.

Listing 7. Add fields to entity classes
field string --fieldName firstname --class ~.domain.Speaker --notNull 
field string --fieldName lastname --notNull 
field string --fieldName email --class ~.domain.Speaker --unique --notNull --regexp 
     ^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})
field string --fieldName organization 
field date --fieldName birthdate --type java.util.Date --past --notNull 
field number --type java.lang.Long --fieldName age --min 25 --max 60 
field date --fieldName created --type java.util.Date --notNull 
field string --fieldName title --class ~.domain.Talk --notNull 
field string --fieldName description --notNull --sizeMax 4000 
field date --fieldName created --type java.util.Date --notNull 
field set --fieldName talks --type ~.domain.Talk --class ~.domain.Speaker 
     --cardinality ONE_TO_MANY 
field reference --fieldName speaker --type ~.domain.Speaker --class ~.domain.Talk 
     --notNull

Note the use of regex in email, which will validate the email against the regex. Also, note that the field name is created. Fields with names created of type date are not updatable. YouThis is a minor enhancement, but useful. In Listing 8, the code snippet shows the updatable attribute value in column annotation is set to false.

Listing 8. Example of updatable set to false
@NotNull 
@Column(updatable = false) 
@Temporal(TemporalType.TIMESTAMP) 
@DateTimeFormat(style = "M-") 
private Date created = new Date();

Adding repository and service layers

Further explore Spring JPA and Spring Data

The Spring Roo community demanded an alternative approach to ActiveRecord pattern as repository and service layers are more commonly used in enterprise applications and developers are more comfortable with them. Spring Roo 1.2 makes it possible to have Repositories using Spring JPA project. Spring JPA is part of the umbrella Spring Data project that makes it easy to implement JPA-based repositories. Spring Roo uses Spring JPA repositories which require simply writing repository interfaces, and Spring JPA provides implementation for them. It also supports adding the custom finder method apart from basic CRUD operations. You can define a finder method in your repository interface as follows:

List<Person> findPersonByNameAndCountry(String name, String country)

Spring JPA will provide an implementation for this method by breaking the name of this method and will return a list of all Person with the provided name and country. It reduces code, but make sure the method name is always inline with entity properties. If Person class does not have name or country property, it will fail. This article does not cover the Spring Data JPA project, but consider reading its documentation.

To add Repository interfaces for your Speaker and Talk entities, type the commands in Listing 9:

Listing 9. Adding Repository interfaces
repository jpa --interface ~.repository.SpeakerRepository --entity ~.domain.Speaker 
repository jpa --interface ~.repository.TalkRepository --entity ~.domain.Talk

The repository jpa command has two attributes: one is the name of the interface and the second is the entity for which you want to create the repository. In this case, I specified creating a SpeakerRepository in com.xebia.conference.core.repository package for Speaker entity. The same is true for Talk entity. In the output of the command in Listing 10), you can see that it added spring-data-jpa dependency in pom.xml, and created SpeakerRepository.java interface and its corresponding SpeakerRepository_Roo_Jpa_Repository.aj ITD file.

Listing 10. Example output
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/repository 
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/repository/SpeakerRepository.java 
Created core|SPRING_CONFIG_ROOT/applicationContext-jpa.xml 
Updated core|ROOT/pom.xml 
             [added dependency org.springframework.data:spring-data-jpa:1.0.2.RELEASE] 
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/repository/
                                       SpeakerRepository_Roo_Jpa_Repository.aj
Created core|SRC_TEST_JAVA/org/xebia/conference/core/domain/
                                       SpeakerDataOnDemand_Roo_DataOnDemand.aj 
Created core|SRC_TEST_JAVA/org/xebia/conference/core/domain/
                                       SpeakerIntegrationTest_Roo_IntegrationTest.aj

The most interesting artifact generated by repository jpa command is *Repository_Roo_Jpa_Repository.aj ITD. The inter-type declaration (ITD) file contains the three declarations in Listing 11.

Listing 11. Declarations in ITD file
privileged aspect SpeakerRepository_Roo_Jpa_Repository { 

 declare parents: SpeakerRepository extends JpaRepository<Speaker, Long>; //1
    
 declare parents: SpeakerRepository extends JpaSpecificationExecutor<Speaker> ; //2
    
 declare @type: SpeakerRepository: @Repository; //3
    
}

The first line says that SpeakerRepository interface should extend JpaRepository interface. The JpaRepository interface contains declarations for all the CRUD methods and other commons methods. The second line says that SpeakerRepository interface should extend JpaSpecificationExecutor interface. This interface allows execution of Specifications based on the JPA criteria API. The third and final line makes sure that SpeakerRepository interface should have @Repository annotation.

Adding service layers

Although it is not required to have a service layer if you are using Repositories, in most scenarios it make sense to have a service layer which has all the business logic. You will find the need for service class as you start moving away from Roo-generated CRUD application, therefore, it makes sense to have a service class for entity classes. To create services for Speaker and Talk entities, execute the commands in Listing 12 on Roo shell:

Listing 12. Creating services
service --interface ~.service.SpeakerService --entity ~.domain.Speaker 
service --interface ~.service.TalkService --entity ~.domain.Talk

The service command for SpeakerService generated the output in Listing 13:

Listing 13. Output from SpeakerService
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/service 
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/service/SpeakerService.java
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/service/SpeakerServiceImpl.java
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/service/
                                       SpeakerService_Roo_Service.aj
Updated core|SRC_TEST_JAVA/org/xebia/conference/core/domain/
                                       SpeakerDataOnDemand_Roo_DataOnDemand.aj
Updated core|SRC_TEST_JAVA/org/xebia/conference/core/domain/
                                       SpeakerIntegrationTest_Roo_IntegrationTest.aj
Created core|SRC_MAIN_JAVA/org/xebia/conference/core/service/
                                       SpeakerServiceImpl_Roo_Service.aj

Apart from creating a service folder and modifying test classes, it created SpeakerService, implementation SpeakerServiceImpl, and two ITDs: SpeakerService_Roo_Service.aj and SpeakerServiceImpl_Roo_Service.aj. The SpeakerService_Roo_Service.aj is ITD for SpeakerService interface and makes sure that SpeakerService interface contains all the required method declarations. The SpeakerServiceImpl_Roo_Service.aj contains the implementations method defined in SpeakerService_Roo_Service.aj ITD and adds all these implementations to SpeakerServiceImpl class at compile time.

With this, your core module has all the code required for this article. Now you will create GWT and MVC UI using Spring Roo.

Build a GWT user interface

Google Web Toolkit (GWT) (see Resources) is an open source development toolkit to build complex and rich JavaScript applications in the Java programming language. With GWT, it is as easy for Java developers to write complex Ajax-enabled web applications as it is for developers to write code in Java code. Plus GWT compiles the code to produce highly optimized JavaScript.

Spring Roo provides support for scaffolding GWT applications from the domain model.

In the previous releases, a single command called gwt setup did all the magic from setting up GWT to creating views for entities to ensuring Google App Engine compatibility. Now gwt setup command is broken down into ten different commands, giving developers more control. Listing 14 shows the commands.

Listing 14. Help output for web gwt
view/gwt roo> help --command "web gwt" 
* web gwt all - Locates all entities in the project and creates GWT requests, proxies 
     and creates the scaffold 
* web gwt gae update - Updates the GWT project to support GAE 
* web gwt proxy all - Locates all entities in the project and creates GWT proxies 
* web gwt proxy request all - Locates all entities in the project and creates GWT 
     requests and proxies 
* web gwt proxy request type - Creates a proxy and request based on the specified type 
* web gwt proxy type - Creates a GWT proxy based on the specified type 
* web gwt request all - Locates all entities in the project and creates GWT requests 
* web gwt request type - Creates a GWT proxy based on the specified type 
* web gwt scaffold - Creates a GWT request, proxy and scaffold for the specified type
* web gwt setup - Install Google Web Toolkit (GWT) into your project

To create a GWT front end for the domain model, you need two commands: web gwt setup and web gwt all. Use the other commands to:

  • Create proxies, requests, or both requests and proxies when you do not want to scaffold the UI
  • Create proxies, requests, or both requests and proxies plus scaffold the UI
  • Update the GWT project to support Google App Engine

This article only covers web gwt setup and web gwt all. You can use the other commands easily based on your knowledge of how these two commands work.

Setting up GWT project

Spring Roo now follows a convention that all its view add-ons (like GTW, MVC, and JSF) have a setup command. The setup command has the responsibility of adding required Maven dependencies, some configuration files, some dummy classes, and creating the folder structure required by the view technology. To setup the GWT project, type the command in Listing 15, which also shows the output of the command.

Listing 15. Sample output from web gwt setup
view/gwt roo> web gwt setup 

Created view/gwt|ROOT/src/main/webapp/WEB-INF/spring 
Created view/gwt|ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml 
Created view/gwt|ROOT/src/main/webapp/WEB-INF/web.xml 
Updated view/gwt|ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml 
Created view/gwt|SRC_MAIN_JAVA/org/xebia/conference/view/gwt 
Created view/gwt|SRC_MAIN_JAVA/org/xebia/conference/view/gwt/App.gwt.xml 
Created view/gwt|SRC_MAIN_JAVA/org/xebia/conference/view/gwt/client 
Created view/gwt|SRC_MAIN_JAVA/org/xebia/conference/view/gwt/client/AppEntryPoint.java 
Updated view/gwt|ROOT/src/main/webapp/WEB-INF/web.xml 
Updated view/gwt|ROOT/pom.xml 
   [added dependencies org.springframework:spring-webmvc:${spring.version}, 
   org.springframework.webflow:spring-js-resources:2.2.1.RELEASE, 
   commons-digester:commons-digester:2.1, commons-fileupload:commons-fileupload:1.2.2, 
   javax.servlet.jsp.jstl:jstl-api:1.2, org.glassfish.web:jstl-impl:1.2, 
   javax.el:el-api:1.0, joda-time:joda-time:1.6, javax.servlet.jsp:jsp-api:2.1, 
   commons-codec:commons-codec:1.5; updated project type to war; 
   added repository http://maven.springframework.org/external; 
   added dependencies com.google.gwt:gwt-servlet:2.4.0, com.google.gwt:gwt-user:2.4.0, 
   org.json:json:20090211, com.google.gwt.inject:gin:1.5.0, 
   javax.validation:validation-api:1.0.0.GA, 
   javax.validation:validation-api:1.0.0.GA:sources, xalan:xalan:2.7.1; 
   added plugin org.codehaus.mojo:gwt-maven-plugin:2.2.0; 
   added plugin org.codehaus.mojo:exec-maven-plugin:1.2]

The output of the command shows that it has just created a template GWT project with required Maven dependencies and plugins, a sample AppEntryPoint class, a sample GWT module descriptor App.gwt.xml, and web.xml which is required by all web applications. The web gwt setup command does not perform anything interesting, so let's take a look at the web gwt all command. To scaffold a GWT UI for the domain, execute the following command:

web gwt all --proxyPackage ~.client.proxy --requestPackage ~.client.request

The web gwt all command does all the heavy lifting and creates the GWT module deployment descriptor ApplicationScaffold.gwt.xml, GWT entry point Scaffold.java, Proxy classes SpeakerProxy and TalkProxy, various Activity classes for Speaker and Talk entities, RequestFactory classes for Speaker and Talk, and various Place classes.

To setup the log4j logging for GWT project, type:

logging setup --level INFO

To quit the shell, type q or quit. These commands will create a fully functional GWT front-end conference application.

To run the application, go to the gwt directory from command line, type mvn gwt:run (which starts the jetty server and launches GWT development mode). To open the application in the default browser, click Launch Default Browser, or copy the URL to a clipboard and open the application in another browser.

The application will run at http://127.0.0.1:8888/ApplicationScaffold.html?gwt.codesvr=127.0.0.1:9997. You will see the screen in Figure 3. below. Click Talks or Speaker to view all talks or speakers.

Figure 3. Running GWT Application
Screen capture of the GWT application running in a browser

Build a Spring MVC user interface

This section briefly looks at creating Spring MVC web view for the domain model you created in this article. The 'controller all' command mentioned in Part 1 is deprecated now, and not recommended. Before you use the new commands, first focus on the mvc module. Type this command on Roo shell:

module focus --moduleName view/mvc

Spring Roo's new setup command does all the configuration related to setting up Spring MVC applications. Now you have a controller if you want to scaffold controllers or merely want to do setup. To setup Spring MVC in your application, type:

view/mvc roo> web mvc setup

The output of this command shows that this command did setup related to adding Spring webmvc-config.xml context file, added Maven dependencies, added static resource files, and added Spring Roo tag library. This command did not create views for entities. This is useful for those who do not need a view for their entities. You can manually create controllers and expose RESTful web services.

After completing the basic setup of the conference application, the next step is creating controllers and views for the entities. Type the following command:

view/mvc roo> web mvc all --package ~.web

The web mvc all command will create controllers and jspx views for all the entities. To scaffold a single entity, use the web mvc scaffold command to provide it the name of the entity and the fully qualified name of controller. Listing 16 shows many other web mvc commands. Part 2 covers some of these commands so all are not covered here.

Listing 16. Help output for web mvc
view/mvc roo> help --command "web mvc" 
* web mvc all - Scaffold Spring MVC controllers for all project entities 
     without an existing controller 
     * web mvc controller - Create a new manual Controller (where you write the methods)
* web mvc embed document - Embed a document for your WEB MVC application 
* web mvc embed generic - Embed media by URL into your WEB MVC application 
* web mvc embed map - Embed a map for your WEB MVC application 
* web mvc embed photos - Embed a photo gallery for your WEB MVC application 
* web mvc embed stream video - Embed a video stream into your WEB MVC application 
* web mvc embed twitter - Embed twitter messages into your WEB MVC application 
* web mvc embed video - Embed a video for your WEB MVC application 
* web mvc embed wave - Embed Google wave integration for your WEB MVC application 
* web mvc finder add - Adds @RooWebFinder annotation to MVC controller type 
* web mvc finder all - Adds @RooWebFinder annotation to existing MVC controllers 
* web mvc install language - Install new internationalization bundle for MVC 
     scaffolded UI.
* web mvc install view - Create a new static view. 
* web mvc json add - Adds @RooJson annotation to target type 
* web mvc json all - Adds or creates MVC controllers annotated with @RooWebJson 
     annotation 
* web mvc json setup - Set up Spring MVC to support JSON 
* web mvc language - Install new internationalization bundle for MVC scaffolded UI. 
* web mvc scaffold - Create a new scaffold Controller (that is where Roo maintains CRUD 
     functionality automatically) 
* web mvc setup - Setup a basic project structure for a Spring MVC / JSP application 
* web mvc update tags - Replace an existing application tagx library with the latest 
     version (use --backup option to backup your application first) 
* web mvc view - Create a new static view.

Deploying to Cloud Foundry

Now that you have created a conference application, it makes sense to deploy it. You can deploy Spring applications to the Cloud Foundry public cloud without any modifications. Part 4 covers Cloud Foundry in detail, including how to deploy Spring Roo applications from within the Roo shell using the Spring Roo Cloud Foundry add-on. In release 1.2.1, however, the support seems to be broken. Therefore, in this article you will use vmc ruby gem to deploy the conference application on Cloud Foundry. Follow these steps:

  1. Install the vmc client. For a step-by-step tutorial called "Installing the Command-Line Interface (vmc)," see Resources.
  2. Login to Cloud Foundry public cloud using the credentials that you registered at Cloud Foundry. Type the vmc login command and it will ask for your email and password as in Listing 17.
    Listing 17. Example of vmc login
    shekhar@shekhar:~/dev/conference/view/mvc/target$ vmc login 
    Attempting login to [http://api.cloudfoundry.com] 
    Email: shekhargulati84@gmail.com 
    Password: ************* 
    Successfully logged into [http://api.cloudfoundry.com]
  3. Once you install the vmc client, do the full Maven build of the conference application. To do this, type:
    mvn clean install
  4. After you build the project, push the conference application to Cloud Foundry. In this article, you will push two applications: one for Spring MVC and the other for GWT. To push the Spring MVC conference application, go to the conference/view/mvc/target folder and type vmc push as in Listing 18.
    Listing 18. Example of pushing Spring MVC application
    shekhar@shekhar:~/dev/conference/view/mvc/target$ vmc push  
    Would you like to deploy from the current directory? [Yn]: Y 
    Application Name: mvcconference  
    Application Deployed URL [mvcconference.cloudfoundry.com]:  
    Detected a Java SpringSource Spring Application, is this correct? [Yn]: Y  
    Memory Reservation (64M, 128M, 256M, 512M, 1G) [512M]:  
    Creating Application: OK  
    Would you like to bind any services to 'mvcconference'? [yN]: N  
    Uploading Application:  
    Checking for available resources: OK  
    Processing resources: OK  
    Packing application: OK  
    Uploading (91K): OK  
    Push Status: OK  
    Staging Application: OK  
    Starting Application: OK

    Cloud Foundry vmc client will ask questions related to the name of the application, type of the application, URL to deploy, memory reservation, and whether to bind any service or not, and finally upload and deploy the application to Cloud Foundry. You can view the Spring MVC application running at http://mvcconference.cloudfoundry.com/

  5. Push the GWT application to Cloud Foundry. Switch to conference/view/gwt/target folder, and type vmc push as in Listing 19.
Listing 19. Example of pushing GWT application
shekhar@shekhar:~/dev/conference/view/gwt/target$ vmc push 
Would you like to deploy from the current directory? [Yn]: Y 
Application Name: gwtconference 
Application Deployed URL [gwtconference.cloudfoundry.com]: 
Detected a Java SpringSource Spring Application, is this correct? [Yn]: Y 
Memory Reservation (64M, 128M, 256M, 512M) [512M]: 
Creating Application: OK 
Would you like to bind any services to 'gwtconference'? [yN]: N 
Uploading Application: 
  Checking for available resources: OK 
  Processing resources: OK 
  Packing application: OK 
  Uploading (5M): OK   
Push Status: OK 
Staging Application: OK                                                         
Starting Application: OK

View the running application at: http://gwtconference.cloudfoundry.com/

Download the source code of the conference application.


Conclusion

This article reintroduced Spring Roo. You learned about new features like multi-module Maven projects, the repository and service layer introduced in Spring Roo 1.2, and then created the application using Roo 1.2 which incorporated some of the new features. You also looked at using Spring Roo to create GWT applications. Finally, you deployed your Spring MVC and GWT application to Cloud Foundry. Some new features like JSF and MongoDB support were not reviewed in this article.

Part 7 of this series will discuss creating Spring MongoDB applications using Spring Roo and deploying to Cloud Foundry.


Download

DescriptionNameSize
Sample codeconference.zip340KB

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Open source on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Java technology
ArticleID=833309
ArticleTitle=Introducing Spring Roo, Part 6: Develop Spring MVC and GWT applications using Spring Roo 1.2 and deploy them on Cloud Foundry
publish-date=09072012