ROO – 2: Creating a simple application: Simple User Record

If you have not checked earlier my post on “ROO for rapid spring development“, please do it.

Here we will create another application, which will create the entity Logins with two fields username and password.

Project Setup

roo> project --topLevelPackage org.grassfield.userapp
Created ROOT/pom.xml
Created SRC_MAIN_RESOURCES
Created SRC_MAIN_RESOURCES/log4j.properties
Created SPRING_CONFIG_ROOT
Created SPRING_CONFIG_ROOT/applicationContext.xml

Database Setup

roo> jpa setup --provider HIBERNATE --database MYSQL Created SPRING_CONFIG_ROOT/database.properties Please update your database details in src/main/resources/META-INF/spring/database.properties. Updated SPRING_CONFIG_ROOT/applicationContext.xml Created SRC_MAIN_RESOURCES/META-INF/persistence.xml Updated ROOT/pom.xml [added dependencies mysql:mysql-connector-java:5.1.18, org.hibernate:hibernate-core:3.6.9.Final, org.hibernate:hibernate-entitymanager:3.6.9.Final, org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final, org.hibernate:hibernate-validator:4.2.0.Final, javax.validation:validation-api:1.0.0.GA, cglib:cglib-nodep:2.2.2, javax.transaction:jta:1.1, org.springframework:spring-jdbc:${spring.version}, org.springframework:spring-orm:${spring.version}, commons-pool:commons-pool:1.5.6, commons-dbcp:commons-dbcp:1.3]

Edit the database.properties and add the mysql database name, username, password.

Ensure you have created the database on MySQL, and the user has access to the database.

Entity Setup

roo> entity jpa --class ~.User --testAutomatically
 Reserved SQL keyword 'User' is not permitted as simple type name
 ~.User roo> entity jpa --class ~.Login --testAutomatically
 Created SRC_MAIN_JAVA/org/grassfield/userapp
 Created SRC_MAIN_JAVA/org/grassfield/userapp/Login.java
 Created SRC_TEST_JAVA/org/grassfield/userapp
 Created SRC_TEST_JAVA/org/grassfield/userapp/LoginDataOnDemand.java
 Created SRC_TEST_JAVA/org/grassfield/userapp/LoginIntegrationTest.java
 Created SRC_MAIN_JAVA/org/grassfield/userapp/Login_Roo_Configurable.aj
 Created SRC_MAIN_JAVA/org/grassfield/userapp/Login_Roo_Jpa_Entity.aj
 Created SRC_MAIN_JAVA/org/grassfield/userapp/Login_Roo_Jpa_ActiveRecord.aj
 Created SRC_MAIN_JAVA/org/grassfield/userapp/Login_Roo_ToString.aj
 Created SRC_TEST_JAVA/org/grassfield/userapp/LoginIntegrationTest_Roo_Configurable.aj
 Created SRC_TEST_JAVA/org/grassfield/userapp/LoginDataOnDemand_Roo_DataOnDemand.aj
 Created SRC_TEST_JAVA/org/grassfield/userapp/LoginIntegrationTest_Roo_IntegrationTest.aj
 Created SRC_TEST_JAVA/org/grassfield/userapp/LoginDataOnDemand_Roo_Configurable.aj

Add Fields to Entity

~.Login roo> field string --fieldName username --notNull
 Updated SRC_MAIN_JAVA/org/grassfield/userapp/Login.java
 Updated SRC_TEST_JAVA/org/grassfield/userapp/LoginDataOnDemand_Roo_DataOnDemand.aj
 Created SRC_MAIN_JAVA/org/grassfield/userapp/Login_Roo_JavaBean.aj
~.Login roo> field string --fieldName password --notNull
 Updated SRC_MAIN_JAVA/org/grassfield/userapp/Login.java
 Updated SRC_TEST_JAVA/org/grassfield/userapp/LoginDataOnDemand_Roo_DataOnDemand.aj
 Updated SRC_MAIN_JAVA/org/grassfield/userapp/Login_Roo_JavaBean.aj

Setup Spring MVC

~.Login roo> web mvc setup
 Created ROOT/src/main/webapp/WEB-INF/spring
 Created ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml
 Created ROOT/src/main/webapp/WEB-INF/web.xml
 Updated ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml
 Created ROOT/src/main/webapp/images
 Created ROOT/src/main/webapp/images/add.png
 Created ROOT/src/main/webapp/images/banner-graphic.png
 Created ROOT/src/main/webapp/images/create.png
 Created ROOT/src/main/webapp/images/delete.png
 Created ROOT/src/main/webapp/images/favicon.ico
 Created ROOT/src/main/webapp/images/list.png
 Created ROOT/src/main/webapp/images/resultset_first.png
 Created ROOT/src/main/webapp/images/resultset_last.png
 Created ROOT/src/main/webapp/images/resultset_next.png
 Created ROOT/src/main/webapp/images/resultset_previous.png
 Created ROOT/src/main/webapp/images/show.png
 Created ROOT/src/main/webapp/images/springsource-logo.png
 Created ROOT/src/main/webapp/images/update.png
 Created ROOT/src/main/webapp/styles
 Created ROOT/src/main/webapp/styles/alt.css
 Created ROOT/src/main/webapp/styles/standard.css
 Created ROOT/src/main/webapp/WEB-INF/classes
 Created ROOT/src/main/webapp/WEB-INF/classes/alt.properties
 Created ROOT/src/main/webapp/WEB-INF/classes/standard.properties
 Created ROOT/src/main/webapp/WEB-INF/layouts
 Created ROOT/src/main/webapp/WEB-INF/layouts/default.jspx
 Created ROOT/src/main/webapp/WEB-INF/layouts/layouts.xml
 Created ROOT/src/main/webapp/WEB-INF/views
 Created ROOT/src/main/webapp/WEB-INF/views/header.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/menu.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/footer.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/views.xml
 Created ROOT/src/main/webapp/WEB-INF/views/dataAccessFailure.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/index-template.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/index.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/resourceNotFound.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/uncaughtException.jspx
 Created ROOT/src/main/webapp/WEB-INF/tags/form
 Created ROOT/src/main/webapp/WEB-INF/tags/form/create.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/dependency.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/find.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/list.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/show.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/update.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/checkbox.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/column.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/datetime.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/display.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/editor.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/input.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/reference.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/select.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/simple.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/table.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/textarea.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/category.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/item.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/menu.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util
 Created ROOT/src/main/webapp/WEB-INF/tags/util/language.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/load-scripts.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/pagination.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/panel.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/placeholder.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/theme.tagx
 Created ROOT/src/main/webapp/WEB-INF/i18n
 Created ROOT/src/main/webapp/WEB-INF/i18n/messages.properties
 Created ROOT/src/main/webapp/images/en.png
 Updated ROOT/src/main/webapp/WEB-INF/i18n/application.properties
 Updated ROOT/src/main/webapp/WEB-INF/web.xml
 Updated 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 dependency org.apache.tiles:tiles-jsp:2.2.2]
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/footer.jspx
~.Login roo> web mvc all --package ~.web
 Created SRC_MAIN_JAVA/org/grassfield/userapp/web
 Created SRC_MAIN_JAVA/org/grassfield/userapp/web/LoginController.java
 Updated SRC_MAIN_WEBAPP/WEB-INF/spring/webmvc-config.xml
 Created SRC_MAIN_JAVA/org/grassfield/userapp/web/ApplicationConversionServiceFactoryBean.java
 Created SRC_MAIN_WEBAPP/WEB-INF/views/logins
 Created SRC_MAIN_WEBAPP/WEB-INF/views/logins/views.xml
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/logins/views.xml
 Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
 Created SRC_MAIN_JAVA/org/grassfield/userapp/web/ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj
 Created SRC_MAIN_JAVA/org/grassfield/userapp/web/LoginController_Roo_Controller.aj
 Created SRC_MAIN_WEBAPP/WEB-INF/views/logins/list.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/logins/show.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/logins/create.jspx
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/logins/update.jspx

Run the Selenium tests

~.web roo> selenium test --controller ~.web.LoginController
Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
 Created SRC_MAIN_WEBAPP/selenium
 Created SRC_MAIN_WEBAPP/selenium/test-login.xhtml
 Created SRC_MAIN_WEBAPP/selenium/test-suite.xhtml
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
 Updated ROOT/pom.xml [added plugin org.codehaus.mojo:selenium-maven-plugin:2.1]
~.web roo> perform tests
 [INFO] Scanning for projects...
 [INFO]
 [INFO] ------------------------------------------------------------------------
 [INFO] Building userapp 0.1.0.BUILD-SNAPSHOT
 [INFO] ------------------------------------------------------------------------
 [INFO] oo>
 [INFO] --- aspectj-maven-plugin:1.2:compile (default) @ userapp ---
 [WARNING] advice defined in org.springframework.scheduling.aspectj.AbstractAsyncExecutionAspect has not been applied [Xlint:adviceDidNotMatch]
 [WARNING] advice defined in org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl has not been applied [Xlint:adviceDidNotMatch]
 [WARNING] advice defined in org.springframework.mock.staticmock.AbstractMethodMockingControl has not been applied [Xlint:adviceDidNotMatch]
 [INFO] oo>
 [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ userapp ---
 [debug] execute contextualize
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] Copying 4 resources
 [INFO] oo>
 [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ userapp ---
 [INFO] Nothing to compile - all classes are up to date
 [INFO] oo>
 [INFO] --- aspectj-maven-plugin:1.2:test-compile (default) @ userapp ---
 [WARNING] advice defined in org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect has not been applied [Xlint:adviceDidNotMatch]
 [WARNING] advice defined in org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl has not been applied [Xlint:adviceDidNotMatch]
 [WARNING] advice defined in org.springframework.mock.staticmock.AbstractMethodMockingControl has not been applied [Xlint:adviceDidNotMatch]
 [WARNING] advice defined in org.springframework.scheduling.aspectj.AbstractAsyncExecutionAspect has not been applied [Xlint:adviceDidNotMatch]
 [INFO] oo>
 [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ userapp ---
 [debug] execute contextualize
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] skip non existing resourceDirectory /home/pandian/apps/userapp/src/test/resources
 [INFO] oo>
 [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ userapp ---
 [INFO] Nothing to compile - all classes are up to date
 [INFO] oo>
 [INFO] --- maven-surefire-plugin:2.9:test (default-test) @ userapp ---
 [INFO] Surefire report directory: /home/pandian/apps/userapp/target/surefire-reports
 ~.web roo>
 -------------------------------------------------------
 T E S T S
 -------------------------------------------------------
 ~.web roo>
 Results :>
 ~.web roo>
 Tests run: 9, Failures: 0, Errors: 0, Skipped: 0
 ~.web roo>
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 6.317s
 [INFO] Finished at: Sun May 27 16:29:56 IST 2012
 [INFO] Final Memory: 9M/215M
 [INFO] ------------------------------------------------------------------------

Bundling

~.web roo> perform package
 [INFO] Scanning for projects...
 [INFO]
 [INFO] ------------------------------------------------------------------------
 [INFO] Building userapp 0.1.0.BUILD-SNAPSHOT
 [INFO] ------------------------------------------------------------------------
 [INFO] oo>
 [INFO] --- aspectj-maven-plugin:1.2:compile (default) @ userapp ---
 [INFO] oo>
 [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ userapp ---
 [debug] execute contextualize
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] Copying 4 resources
 [INFO] oo>
 [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ userapp ---
 [INFO] Nothing to compile - all classes are up to date
 [INFO] oo>
 [INFO] --- aspectj-maven-plugin:1.2:test-compile (default) @ userapp ---
 [INFO] oo>
 [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ userapp ---
 [debug] execute contextualize
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] skip non existing resourceDirectory /home/pandian/apps/userapp/src/test/resources
 [INFO] oo>
 [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ userapp ---
 [INFO] Nothing to compile - all classes are up to date
 [INFO] oo>
 [INFO] --- maven-surefire-plugin:2.9:test (default-test) @ userapp ---
 [INFO] Tests are skipped.
 [INFO] oo>
 [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ userapp ---
 [INFO] Packaging webapp
 [INFO] Assembling webapp [userapp] in [/home/pandian/apps/userapp/target/userapp-0.1.0.BUILD-SNAPSHOT]
 [INFO] Processing war project
 [INFO] Copying webapp resources [/home/pandian/apps/userapp/src/main/webapp]
 [INFO] Webapp assembled in [158 msecs]
 [INFO] Building war: /home/pandian/apps/userapp/target/userapp-0.1.0.BUILD-SNAPSHOT.war
 [INFO] WEB-INF/web.xml already added, skipping
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 3.314s
 [INFO] Finished at: Sun May 27 16:35:44 IST 2012
 [INFO] Final Memory: 8M/150M
 [INFO] ------------------------------------------------------------------------

Create Eclipse project

~.web roo> perform eclipse
 [INFO] Scanning for projects...
 [INFO]
 [INFO] ------------------------------------------------------------------------
 [INFO] Building userapp 0.1.0.BUILD-SNAPSHOT
 [INFO] ------------------------------------------------------------------------
 [INFO] oo>
 [INFO] --- maven-eclipse-plugin:2.7:clean (default-cli) @ userapp ---
 [INFO] Deleting file: .project
 [INFO] Deleting file: .classpath
 [INFO] Deleting file: .wtpmodules
 [INFO] Deleting file: .component
 [INFO] Deleting file: org.eclipse.wst.common.component
 [INFO] Deleting file: org.eclipse.wst.common.project.facet.core.xml
 [INFO] Deleting file: org.eclipse.jdt.core.prefs
 [INFO] Deleting file: org.eclipse.ajdt.ui.prefs
 [INFO] oo>
 [INFO] >>> maven-eclipse-plugin:2.7:eclipse (default-cli) @ userapp >>>
 [INFO] oo>
 [INFO] --- aspectj-maven-plugin:1.2:compile (default) @ userapp ---
 [WARNING] advice defined in org.springframework.scheduling.aspectj.AbstractAsyncExecutionAspect has not been applied [Xlint:adviceDidNotMatch]
 [WARNING] advice defined in org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl has not been applied [Xlint:adviceDidNotMatch]
 [WARNING] advice defined in org.springframework.mock.staticmock.AbstractMethodMockingControl has not been applied [Xlint:adviceDidNotMatch]
 [INFO] oo>
 [INFO] <<< maven-eclipse-plugin:2.7:eclipse (default-cli) @ userapp <<< [INFO] oo>
 [INFO] --- maven-eclipse-plugin:2.7:eclipse (default-cli) @ userapp ---
 [INFO] Adding support for WTP version 2.0.
 [INFO] Using Eclipse Workspace: null
 [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
 [INFO] Wrote settings to /home/pandian/apps/userapp/.settings/org.eclipse.jdt.core.prefs
 [INFO] Wrote Eclipse project for "userapp" to /home/pandian/apps/userapp.
 [INFO] oo>
 Javadoc for some artifacts is not available.
 Please run the same goal with the -DdownloadJavadocs=true parameter in order to check remote repositories for javadoc.
 List of artifacts without a javadoc archive:
 o cglib:cglib-nodep:2.2.2
 o commons-codec:commons-codec:1.5
 o commons-dbcp:commons-dbcp:1.3
 o commons-pool:commons-pool:1.5.6
 o commons-digester:commons-digester:2.1
 o commons-beanutils:commons-beanutils:1.8.3
 o commons-fileupload:commons-fileupload:1.2.2
 o javax.el:el-api:1.0
 o javax.servlet:servlet-api:2.5
 o javax.servlet.jsp:jsp-api:2.1
 o javax.servlet.jsp.jstl:jstl-api:1.2
 o javax.transaction:jta:1.1
 o javax.validation:validation-api:1.0.0.GA
 o joda-time:joda-time:1.6
 o junit:junit:4.10
 o org.hamcrest:hamcrest-core:1.1
 o log4j:log4j:1.2.16
 o mysql:mysql-connector-java:5.1.18
 o net.sf.flexjson:flexjson:2.1
 o org.apache.commons:commons-lang3:3.1
 o org.apache.tiles:tiles-jsp:2.2.2
 o org.apache.tiles:tiles-servlet:2.2.2
 o org.apache.tiles:tiles-core:2.2.2
 o org.apache.tiles:tiles-api:2.2.2
 o org.slf4j:jcl-over-slf4j:1.6.4
 o org.slf4j:slf4j-api:1.6.4
 o org.apache.tiles:tiles-template:2.2.2
 o org.aspectj:aspectjrt:1.6.12
 o org.aspectj:aspectjweaver:1.6.12
 o org.glassfish.web:jstl-impl:1.2
 o org.hibernate:hibernate-core:3.6.9.Final
 o antlr:antlr:2.7.6
 o commons-collections:commons-collections:3.1
 o dom4j:dom4j:1.6.1
 o org.hibernate:hibernate-commons-annotations:3.2.0.Final
 o org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final
 o org.hibernate:hibernate-entitymanager:3.6.9.Final
 o javassist:javassist:3.12.0.GA
 o org.hibernate:hibernate-validator:4.2.0.Final
 o org.slf4j:slf4j-log4j12:1.6.4
 o org.springframework:spring-aop:3.1.0.RELEASE
 o aopalliance:aopalliance:1.0
 o org.springframework:spring-asm:3.1.0.RELEASE
 o org.springframework:spring-beans:3.1.0.RELEASE
 o org.springframework:spring-core:3.1.0.RELEASE
 o org.springframework:spring-aspects:3.1.0.RELEASE
 o org.springframework:spring-context:3.1.0.RELEASE
 o org.springframework:spring-expression:3.1.0.RELEASE
 o org.springframework:spring-context-support:3.1.0.RELEASE
 o org.springframework:spring-test:3.1.0.RELEASE
 o org.springframework:spring-jdbc:3.1.0.RELEASE
 o org.springframework:spring-tx:3.1.0.RELEASE
 o org.springframework:spring-orm:3.1.0.RELEASE
 o org.springframework:spring-webmvc:3.1.0.RELEASE
 o org.springframework:spring-web:3.1.0.RELEASE
 o org.springframework.roo:org.springframework.roo.annotations:1.2.1.RELEASE
 o commons-io:commons-io:2.1
 o org.springframework.webflow:spring-js-resources:2.2.1.RELEASE
 ~.web roo>
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 3.696s
 [INFO] Finished at: Sun May 27 16:35:59 IST 2012
 [INFO] Final Memory: 12M/218M
 [INFO] ------------------------------------------------------------------------

Thats all!

~.web roo> quit

Web Preview

pandian@pandian-ubuntu:~/apps/userapp$ mvn tomcat:run
 [INFO] Scanning for projects...
 [INFO]
 [INFO] ------------------------------------------------------------------------
 [INFO] Building userapp 0.1.0.BUILD-SNAPSHOT
 [INFO] ------------------------------------------------------------------------
 [INFO]
 [INFO] >>> tomcat-maven-plugin:1.1:run (default-cli) @ userapp >>>
 [INFO]
 [INFO] --- aspectj-maven-plugin:1.2:compile (default) @ userapp ---
 [INFO]
 [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ userapp ---
 [debug] execute contextualize
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] Copying 4 resources
 [INFO]
 [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ userapp ---
 [INFO] Nothing to compile - all classes are up to date
 [INFO]
 [INFO] <<< tomcat-maven-plugin:1.1:run (default-cli) @ userapp <<<
 [INFO]
 [INFO] --- tomcat-maven-plugin:1.1:run (default-cli) @ userapp ---
 [INFO] Running war on http://localhost:8080/userapp
 [INFO] Creating Tomcat server configuration at /home/pandian/apps/userapp/target/tomcat
 May 27, 2012 4:36:26 PM org.apache.catalina.startup.Embedded start
 INFO: Starting tomcat server
 May 27, 2012 4:36:26 PM org.apache.catalina.core.StandardEngine start
 INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
 May 27, 2012 4:36:26 PM org.apache.catalina.core.ApplicationContext log
 INFO: Initializing Spring root WebApplicationContext
 May 27, 2012 4:36:28 PM org.apache.catalina.core.ApplicationContext log
 INFO: Initializing Spring FrameworkServlet 'userapp'
 May 27, 2012 4:36:28 PM org.apache.coyote.http11.Http11Protocol init
 INFO: Initializing Coyote HTTP/1.1 on http-8080
 May 27, 2012 4:36:28 PM org.apache.coyote.http11.Http11Protocol start
 INFO: Starting Coyote HTTP/1.1 on http-8080

Database

mysql> show tables;
 +-------------------+
 | Tables_in_userapp |
 +-------------------+
 | login |
 +-------------------+
 1 row in set (0.00 sec)
mysql> select * from login;
 +----+------------+------------+---------+
 | id | password | username | version |
 +----+------------+------------+---------+
 | 1 | pandian | pandian | 0 |
 | 2 | fieldgrass | grassfield | 0 |
 +----+------------+------------+---------+
 2 rows in set (0.00 sec)

 

Advertisements

Roo for rapid Spring development

Whenever we try to build a new project, we used to start from scratch with our favourite IDE. Spring is used for rapid development. In such case, initial setup of the project takes time. Everybody wants a template to begin with Spring. “Can I find a blank war for Struts?” is a frequently being asked question. Hence we introduce Roo from SpringTools

If you take any generic Spring development project, it contains the following environment setup activities. This is a recursive one for every project.

We can overcome this with Roo. It helps you to create and start a Spring Web in less than 10 minutes!

How to Use Roo?

Spring has some pre-requisites. You need to ensure JDK 1.6 is installed. You should have Maven and Roo. Download them and ensure it is in PATH. Usually I will create symlinks for the frequently used applications in /usr/bin. (This tutorial is created for Ubuntu or any Unix Platforms. I hope you are smart enough to not to ask me to write the same for Windows 🙂 )

$ sudo ln -s /home/pandian/Downloads/maven/apache-maven-3.0.4/bin/mvn /usr/bin/mvn

$ sudo ln -s /home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/roo /usr/bin/roo

Now this is the time to execute Roo

$ roo
roo>hint

You should see something like the following!

The project has been created. But we need persistence support!

roo> project --topLevelPackage org.grassfield.roo
 Created ROOT/pom.xml
 Created SRC_MAIN_RESOURCES
 Created SRC_MAIN_RESOURCES/log4j.properties
 Created SPRING_CONFIG_ROOT
 Created SPRING_CONFIG_ROOT/applicationContext.xml

I like to use MySQL with Hibernate

roo> jpa setup --provider HIBERNATE --database MYSQL
Created SPRING_CONFIG_ROOT/database.properties
Please update your database details in src/main/resources/META-INF/spring/database.properties.
Updated SPRING_CONFIG_ROOT/applicationContext.xml
Created SRC_MAIN_RESOURCES/META-INF/persistence.xml
Updated ROOT/pom.xml [added dependencies mysql:mysql-connector-java:5.1.18, org.hibernate:hibernate-core:3.6.9.Final, org.hibernate:hibernate-entitymanager:3.6.9.Final, org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final, org.hibernate:hibernate-validator:4.2.0.Final, javax.validation:validation-api:1.0.0.GA, cglib:cglib-nodep:2.2.2, javax.transaction:jta:1.1, org.springframework:spring-jdbc:${spring.version}, org.springframework:spring-orm:${spring.version}, commons-pool:commons-pool:1.5.6, commons-dbcp:commons-dbcp:1.3]

I am not updating the DB information in the database.properties located in

/home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/src/main/resources/META-INF/spring
$ cat database.properties
 #Updated at Sat Apr 28 11:47:56 IST 2012
 #Sat Apr 28 11:47:56 IST 2012
 database.driverClassName=com.mysql.jdbc.Driver
 database.url=jdbc\:mysql\://localhost\:3306
 database.username=root
 database.password=pandian

This is the time to setup the DB. Let create an entity Timer.

roo> entity jpa --class ~.Timer --testAutomatically
 Created SRC_MAIN_JAVA/org/grassfield/roo
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer.java
 Created SRC_TEST_JAVA/org/grassfield/roo
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand.java
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerIntegrationTest.java
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_Configurable.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_ToString.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_Jpa_Entity.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_Jpa_ActiveRecord.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerIntegrationTest_Roo_Configurable.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand_Roo_DataOnDemand.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerIntegrationTest_Roo_IntegrationTest.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand_Roo_Configurable.aj
 ~.Timer

Lets create a field to the Timer entity now

roo> field string --fieldName message --notNull
 Updated SRC_MAIN_JAVA/org/grassfield/roo/Timer.java
 Updated SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand_Roo_DataOnDemand.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_JavaBean.aj
 ~.Timer

Lets build the web project now with spring MVC

roo> hint web mvc

It will be useful to execute the hint command after every execution.

~.Timer roo> web mvc setup
 Created ROOT/src/main/webapp/WEB-INF/spring
 Created ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml
 Created ROOT/src/main/webapp/WEB-INF/web.xml
 Updated ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml
 Created ROOT/src/main/webapp/images
 Created ROOT/src/main/webapp/images/add.png
 Created ROOT/src/main/webapp/images/banner-graphic.png
 Created ROOT/src/main/webapp/images/create.png
 Created ROOT/src/main/webapp/images/delete.png
 Created ROOT/src/main/webapp/images/favicon.ico
 Created ROOT/src/main/webapp/images/list.png
 Created ROOT/src/main/webapp/images/resultset_first.png
 Created ROOT/src/main/webapp/images/resultset_last.png
 Created ROOT/src/main/webapp/images/resultset_next.png
 Created ROOT/src/main/webapp/images/resultset_previous.png
 Created ROOT/src/main/webapp/images/show.png
 Created ROOT/src/main/webapp/images/springsource-logo.png
 Created ROOT/src/main/webapp/images/update.png
 Created ROOT/src/main/webapp/styles
 Created ROOT/src/main/webapp/styles/alt.css
 Created ROOT/src/main/webapp/styles/standard.css
 Created ROOT/src/main/webapp/WEB-INF/classes
 Created ROOT/src/main/webapp/WEB-INF/classes/alt.properties
 Created ROOT/src/main/webapp/WEB-INF/classes/standard.properties
 Created ROOT/src/main/webapp/WEB-INF/layouts
 Created ROOT/src/main/webapp/WEB-INF/layouts/default.jspx
 Created ROOT/src/main/webapp/WEB-INF/layouts/layouts.xml
 Created ROOT/src/main/webapp/WEB-INF/views
 Created ROOT/src/main/webapp/WEB-INF/views/header.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/menu.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/footer.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/views.xml
 Created ROOT/src/main/webapp/WEB-INF/views/dataAccessFailure.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/index-template.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/index.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/resourceNotFound.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/uncaughtException.jspx
 Created ROOT/src/main/webapp/WEB-INF/tags/form
 Created ROOT/src/main/webapp/WEB-INF/tags/form/create.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/dependency.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/find.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/list.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/show.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/update.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/checkbox.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/column.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/datetime.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/display.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/editor.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/input.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/reference.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/select.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/simple.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/table.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/textarea.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/category.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/item.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/menu.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util
 Created ROOT/src/main/webapp/WEB-INF/tags/util/language.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/load-scripts.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/pagination.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/panel.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/placeholder.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/theme.tagx
 Created ROOT/src/main/webapp/WEB-INF/i18n
 Created ROOT/src/main/webapp/WEB-INF/i18n/messages.properties
 Created ROOT/src/main/webapp/images/en.png
 Updated ROOT/src/main/webapp/WEB-INF/i18n/application.properties
 Updated ROOT/src/main/webapp/WEB-INF/web.xml
 Updated 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 dependency org.apache.tiles:tiles-jsp:2.2.2]
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/footer.jspx

and next….

roo> web mvc all --package ~.web
 Created SRC_MAIN_JAVA/org/grassfield/roo/web
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/TimerController.java
 Updated SRC_MAIN_WEBAPP/WEB-INF/spring/webmvc-config.xml
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/ApplicationConversionServiceFactoryBean.java
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/views.xml
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/timers/views.xml
 Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/TimerController_Roo_Controller.aj
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/list.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/show.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/create.jspx
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/update.jspx
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj

Lets configure the tests!

roo> selenium test --controller ~.web.TimerController
 Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
 Created SRC_MAIN_WEBAPP/selenium
 Created SRC_MAIN_WEBAPP/selenium/test-timer.xhtml
 Created SRC_MAIN_WEBAPP/selenium/test-suite.xhtml
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
 Updated ROOT/pom.xml [added plugin org.codehaus.mojo:selenium-maven-plugin:2.1]
~.Timer

Lets run the tests now

~.web roo> perform tests
[INFO] ------------------------------------------------------------------------
 [INFO] BUILD FAILURE
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 3.818s
 [INFO] Finished at: Sat Apr 28 12:40:43 IST 2012
 [INFO] Final Memory: 7M/151M
 [INFO] ------------------------------------------------------------------------
 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.9:test (default-test) on project roo: There are test failures.
 [ERROR] o>
 [ERROR] Please refer to /home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/newproj/target/surefire-reports for the individual test results.
 [ERROR] -> [Help 1]
 [ERROR] o>
 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
 [ERROR] o>
 [ERROR] For more information about the errors and possible solutions, please read the following articles:
 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Unfortunate, lets see the test log

2012-04-28 12:40:42,920 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table timer (id bigint not null auto_increment, message varchar(255) not null, version integer, primary key (id)) ENGINE=InnoDB
2012-04-28 12:40:42,920 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - No database selected
2012-04-28 12:40:43,176 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,190 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,201 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,205 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,209 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,213 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,216 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,221 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected

We have forgot to give the database name in config files.

I am creating a new Database roodb

mysql> create database roodb;

Hence the db property file is changed as below

$ cat ./src/main/resources/META-INF/spring/database.properties
#Updated at Sat Apr 28 12:35:06 IST 2012
 #Sat Apr 28 12:35:06 IST 2012
 database.driverClassName=com.mysql.jdbc.Driver
 database.url=jdbc\:mysql\://localhost\:3306/roodb
 database.username=root
 database.password=pandian

Let’s run the test again!

~.web roo> perform test
[INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 4.520s
 [INFO] Finished at: Sat Apr 28 12:51:22 IST 2012
 [INFO] Final Memory: 7M/151M
 [INFO] ------------------------------------------------------------------------

Cheers!

lets package it now

~.web roo> perform package
[INFO] Webapp assembled in [177 msecs]
[INFO] Building war: /home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/newproj/target/roo-0.1.0.BUILD-SNAPSHOT.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:00.264s
[INFO] Finished at: Sat Apr 28 12:56:13 IST 2012
[INFO] Final Memory: 8M/151M
[INFO] ------------------------------------------------------------------------

I prefer eclipse build

.web roo> perform eclipse
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 8:21.388s
 [INFO] Finished at: Sat Apr 28 13:07:00 IST 2012
 [INFO] Final Memory: 13M/145M
 [INFO] ------------------------------------------------------------------------
~.web roo> quit
 $mvn tomcat:run

Cheers!

Lets see what happened at DB end.

mysql> show tables;
+-----------------+
| Tables_in_roodb |
+-----------------+
| timer           |
+-----------------+
1 row in set (0.00 sec)
mysql> desc timer;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| message | varchar(255) | NO   |     | NULL    |                |
| version | int(11)      | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from timer;
Empty set (0.00 sec)