As we all know web application development using JAVA/JEE requires lot of the efforts, and the development is relatively very slow as compared to other technologies. But, because of goodness of java we have to stick with it. Now days, Spring is widely used open source web development framework. Spring provides a tool Spring Roo that makes development hundreds times faster than previously. I am here to explain steps to develop web applications using Spring Roo.
What is Sping Roo?
Spring Roo is a web development tool using JAVA/JEE technologies. It allows you to build high-quality, high-performance, lock-in-free enterprise applications in just minutes.
Why Spring Roo?
1. High Productivity
2. Uses Standard Java
3. Easy Removal(At runtime Roo can be simply removed from your project and it will look like simple Spring project).
4. High performance
5. High Quality
Prerequisite:
Java 5 and above
Maven 2.0.9 and above
Tomcat
IDE is not mandatory(eclipse or STS is good)
Here I am using Java 1.7.0_51, maven 3.1.0 and tomcat 7.0.40.
Step1:
Download Spring Roo from http://spring-roo-repository.springsource.org/spring-roo-1.2.5.RELEASE.zip. Unzip the the
downloaded file.
Set path variable:
ROO_HOME=/home/kuldeep/java/SpringRoo/spring-roo-1.2.2.RELEASE
export ROO_HOME
PATH=$PATH:ROO_HOME/bin
export PATH
Create link:
open command prompt and run this command sudo ln -s $ROO_HOME/ bin/roo.sh /usr/bin/roo. Now you are ready to play with Spring Roo.
Step2: Open Terminal run following command
mkdir rooExample
cd rooExample
roo
Step3: Create project: Roo provides lots of the commands. To create new project launch
project –topLevelPackage com.kuldeep.rootest
Output:
Created ROOT/pom.xml
Created SRC_MAIN_RESOURCES
Created SRC_MAIN_RESOURCES/log4j.properties
Created SPRING_CONFIG_ROOT
Created SPRING_CONFIG_ROOT/applicationContext.xml
You can see here, some configuration files has been generated.
Step4: Generate Database configuration
jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
Output:
Created SPRING_CONFIG_ROOT/database.properties
Updated SPRING_CONFIG_ROOT/applicationContext.xml
Created SRC_MAIN_RESOURCES/META-INF/persistence.xml
Updated ROOT/pom.xml [added dependencies org.hsqldb:hsqldb:2.2.9, org.hibernate:hibernate-core:4.2.2.Final, org.hibernate:hibernate-entitymanager:4.2.2.Final, org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final, commons-collections:commons-collections:3.2.1, org.hibernate:hibernate-validator:4.3.1.Final, javax.validation:validation-api:1.0.0.GA, 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.4]
This will generate database configuration files using Hibernate and hsqldb.
Step5: Generate entity classes
entity jpa --class ~.models.Employee –testAutomatically
Output:
Created SRC_MAIN_JAVA/com/kuldeep/rootest/models
Created SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee.java
Created SRC_TEST_JAVA/com/kuldeep/rootest/models
Created SRC_TEST_JAVA/com/kuldeep/rootest/models/EmployeeDataOnDemand.java
Created SRC_TEST_JAVA/com/kuldeep/rootest/models/EmployeeIntegrationTest.java
Created SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee_Roo_Configurable.aj
Created SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee_Roo_Jpa_Entity.aj
Created SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee_Roo_Jpa_ActiveRecord.aj
Created SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee_Roo_ToString.aj
Created SRC_TEST_JAVA/com/kuldeep/rootest/models/EmployeeDataOnDemand_Roo_Configurable. aj
Created SRC_TEST_JAVA/com/kuldeep/rootest/models/EmployeeDataOnDemand_Roo_DataOnDema nd.aj
Created SRC_TEST_JAVA/com/kuldeep/rootest/models/EmployeeIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA/com/kuldeep/rootest/models/EmployeeIntegrationTest_Roo_IntegrationTest. aj.
This command generate jpa entity and some roo specific files as well as test integration file. .aj files represent aspectJ file. When project compiles, .aj files used with .java files and generate bytecode.
Step6: Add fields to entity
field string --fieldName empName --notNull
we can specify all database constraints to the fields as we do manually using annotation etc.
Output:
Updated SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee.java
Updated SRC_TEST_JAVA/com/kuldeep/rootest/models/EmployeeDataOnDemand_Roo_DataOnDema nd.aj
Updated SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee_Roo_Jpa_ActiveRecord.aj
Created SRC_MAIN_JAVA/com/kuldeep/rootest/models/Employee_Roo_JavaBean.aj
You can see here it updated some .java and .aj files and also created .aj file (Employee_Roo_JavaBean.aj) this is bean representation with getter setter.
Step7: Generate web related configuration
web mvc setup
This command created lots of the directory and files(a common structure for a web application).
Step8: Generate Controller and configuration for controller
web mvc all --package ~.web
Output:
Created SRC_MAIN_JAVA/com/kuldeep/rootest/web
Created SRC_MAIN_JAVA/com/kuldeep/rootest/web/EmployeeController.java
Updated SRC_MAIN_WEBAPP/WEB-INF/spring/webmvc-config.xml
Created SRC_MAIN_JAVA/com/kuldeep/rootest/web/ApplicationConversionServiceFactoryBean.java
Created SRC_MAIN_WEBAPP/WEB-INF/views/employees
Created SRC_MAIN_WEBAPP/WEB-INF/views/employees/views.xml
Updated SRC_MAIN_WEBAPP/WEB-INF/views/employees/views.xml
Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
Created SRC_MAIN_JAVA/com/kuldeep/rootest/web/ApplicationConversionServiceFactoryBean_Roo _ConversionService.aj
Created SRC_MAIN_JAVA/com/kuldeep/rootest/web/EmployeeController_Roo_Controller.aj
Created SRC_MAIN_WEBAPP/WEB-INF/views/employees/list.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/employees/show.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/employees/create.jspx
Updated SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/employees/update.jspx
step9: Generate Integration test for controller
selenium test --controller ~.web.EmployeeController
Output:
Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
Created SRC_MAIN_WEBAPP/selenium
Created SRC_MAIN_WEBAPP/selenium/test-employee.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.3]
Step10: Now you are almost done with you web project using Spring Roo. You can execute following command to perform test and make project eclipse compatible so you can import project into eclipse
perform tests
perform package
perform eclipse
and finally quit from roo console.
Final Step: Deploy above project on tomcat.
No comments:
Post a Comment