JSF 2.0 with Spring Roo
Join the DZone community and get the full member experience.Join For Free
If you want to create a JSF 2.0 web application in 10 minutes here comes a short how-to.
I never really gave a chance to JSF. With JSF 1.X there was limited tool support, components were quite basic. it didn't really got my attention. I know JSF 2.0 was released more than 2 years ago, so it was long on my todo list.
Lately I am working with spring roo a lot, and as Spring Roo 1.2.0.M1 was released, I read Alan Steward's blog entry which mentioned that the next 1.2 release will include JSF/PrimeFaces support.
I couldn't wait any longer so i went on with the snapshot version, and created a JSF 2.0 webapp. It's pretty impressive, if you want to see the result, i deployed it to CloudFoundry: http://roojsfpreview.cloudfoundry.com/ (if you dont't already have a cloudfoundry account, you should definitely register for a free account)Screenshot of the deployed JSF 2.0 sample app click here to see it in full size
How to sell your brain?
The other day i was doing a spring roo training and the participants just said: please no more pizzashop examples! So they come up with the idee to create a special ebay for selling organs.
Sure, its probably politically incorrect but caught their attention for the rest of the training. So here we create an 'Organ-bay' in 10 minutes:
- Download the latest snapshot from springsource CI: http://static.springsource.org/downloads/nightly/snapshot-download.php?project=ROO
unzip /path/to/spring-roo-1.2.0.BUILD-SNAPSHOT_201110XX.XXXXXXXXXXX.zip ROO_HOME=$(readlink -f spring-roo-1.2.0.BUILD-SNAPSHOT) PATH=$ROO_HOME/bin:$PATH roo.sh ____ ____ ____ / __ \/ __ \/ __ \ / /_/ / / / / / / / / _, _/ /_/ / /_/ / /_/ |_|\____/\____/ 1.2.0.BUILD-SNAPSHOT [rev 1195a76] Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER.
Once you got the roo shell prompt enter the following commands:
project --topLevelPackage com.organbay --projectName rooj-jsf-app persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY enum type --class ~.domain.BloodType enum constant --name Group_A enum constant --name Group_B enum constant --name Group_AB enum constant --name Group_0 entity jpa --class ~.domain.Organ field number --fieldName price --type java.lang.Integer --notNull --min 5 field string --fieldName name --notNull field date --fieldName bestBefore --type java.util.Date --dateFormat SHORT --notNull field string --fieldName description entity jpa --class ~.domain.Donor field string --fieldName name --sizeMax 25 --notNull field enum --fieldName bloodType --type ~.domain.BloodType --notNull field date --fieldName birthDate --type java.util.Date --past field string --fieldName email --regexp "[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+" --notNull field set --fieldName organs --type ~.domain.Organ --mappedBy donor --notNull false --cardinality ONE_TO_MANY field reference --fieldName donor --class ~.domain.Organ --type ~.domain.Donor --notNull web jsf setup --implementation APACHE_MYFACES --theme EGGPLANT web jsf all --package ~.jsf quit
Now the application is ready to be deployed. The tomcat:run maven goal would be ideal for this purpose. To be able to use JSF 2.0 you would need tomcat 7.x but unfortunately the tomcat maven plugin version which supports tomcat 7 is under development, so i found that its easier to use the cargo plugin:
mvn package org.codehaus.cargo:cargo-maven2-plugin:run -Dcargo.maven.containerId=tomcat7x -Dcargo.maven.containerUrl=http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.22/bin/apache-tomcat-7.0.22.zip ... [WARNING] [talledLocalContainer] Oct 26, 2011 5:06:08 PM org.apache.catalina.startup.Catalina start [WARNING] [talledLocalContainer] INFO: Server startup in 31856 ms [INFO] [talledLocalContainer] Tomcat 7.x started on port  [INFO] Press Ctrl-C to stop the container...
now you can reach the webapp in your browser: http://localhost:8080/rooj-jsf-app-0.1.0.BUILD-SNAPSHOT/pages/donor.jsf
Deploy it to the cloud
if you want to share your new shiny webapp on the cloud, you would have several choises: amazon ec2, amazon beanstalk, heroku, cloudfoundry, you name it.
I just choose cloudfoundry, as it has a roo plugin, and it was just 3 lines to deploy it:
cloud foundry login --email email@example.com --password idonttellya cloud foundry deploy --appName roojsfpreview --path /target/rooj-jsf-app-0.1.0.BUILD-SNAPSHOT.war cloud foundry start app --appName roojsfpreview
So there is no more excuse not to give JSF 2.0 a go. I was quite impressed with the generated primefaces app, in terms of eye candy, and features such as:
- Client side validation based on the JSR-303 annotation attached to the entities
- AJAX pagination
- Modal dialog boxes
- Calendar widget
- Auto populated dropp-down list for 1:N relation
- Auto populated dropp-down list for enumartions
- growl type autohiding popup messages
Keep up with the good work Alan Steward and the entire Spring Roo Team!
Opinions expressed by DZone contributors are their own.