Lajos is a java developer and occasionally a trainer at EPAM System. He's highly interested in developer productivity, web frameworks, and cloud computing. He is a passionate father and rock climber. Lajos has posted 2 posts at DZone. View Full User Profile

JSF 2.0 with Spring Roo

11.01.2011
| 27145 views |
  • submit to reddit

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 [8080]
[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 yourname@gmail.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
I was even able to add a theme switcher in a couple of minutes (probably a good canditate to be included into the JSF addon, or maybe a new JSF dependent add-on). Lets start create a JSF 2.0 webapp today!

 

Keep up with the good work Alan Steward and the entire Spring Roo Team!

AttachmentSize
spring-roo-jsf-webapp-small.png48.71 KB
spring-roo-jsf-webapp.png85.95 KB
Published at DZone with permission of its author, Lajos Papp.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Juan Macias replied on Wed, 2011/11/02 - 3:04am

Very nice indeed. But languages are not working ;)

Ennius replied on Wed, 2011/11/02 - 5:10am

Nice, but why 'web jsf setup --implementation APACHE_MYFACES --theme EGGPLANT' is not working ? i get : Command 'web jsf setup --implementation APACHE_MYFACES --theme EGGPLANT' was found but is not currently available (type 'help' then ENTER to learn about this command)

Charlie Mordant replied on Wed, 2011/11/02 - 5:28am

Is that working with OSGI and REST/WS? i'm trying to implement that combo.

Lajos Papp replied on Wed, 2011/11/02 - 8:54am in response to: Ennius

Please note, that you have to use 1.2.0.BUILD-SNAPSHOT version, i checked it again with the latest one [rev 765f5e7], and the 2 listing is working just fine. please copy your 'log.roo' file and i might figure out the reason.

Ali2 Ebrahimi replied on Wed, 2011/11/02 - 10:06am

I get following error with windows xp latest build.
roo> project --topLevelPackage com.organbay --projectName rooj-jsf-app
Created ROOT\pom.xml
Undo create ROOT\pom.xml
Unknown character property name {r} near index 8
F:\ali\projects\spring-roo\tests\rooj-jsf-app\
^

Ansar Basha replied on Wed, 2011/11/02 - 10:30am

I downloaded 1.2.0.BUILD-SNAPSHOT [rev 765f5e7] still giving error line Ali2 Ebrahimi mentioned. roo> project --topLevelPackage com.organbay --projectName rooj-jsf-app Created ROOT\pom.xml Undo create ROOT\pom.xml Unexpected internal error near index 39 C:\spring-roo-1.2.0.BUILD-SNAPSHOT\bin\ ^ Thanks

Ansar Basha replied on Wed, 2011/11/02 - 10:32am

Here is the contents of the log.roo file

 

// Spring Roo 1.2.0.BUILD-SNAPSHOT [rev 765f5e7] log opened at 2011-11-02 15:26:35
// [failed] project --topLevelPackage com.organbay --projectName rooj-jsf-app
 

Lajos Papp replied on Wed, 2011/11/02 - 12:41pm in response to: Ansar Basha

Uppps, that happens when you work on snapshots .... looks like you just hit a bug: https://jira.springsource.org/browse/ROO-2867 I'm sure it wont make you happy, but it works on anything, but windows ;)

Lajos Papp replied on Thu, 2011/11/03 - 3:07am in response to: Lajos Papp

you can quickly fix this windows issue, by:

Lajos Papp replied on Thu, 2011/11/03 - 4:22am in response to: Lajos Papp

ROO-2867 is fixed, you can use the latest http://static.springsource.org/downloads/nightly/snapshot-download.php?project=ROO without any modification.

Roger Itai replied on Thu, 2011/11/03 - 2:03pm

Hi,

 I got following error when ran Maven:

[ERROR] Failed to execute goal on project telepizza: Could not resolve dependenc
ies for project br.com.alvorada.telepizza:telepizza:war:0.1.0.BUILD-SNAPSHOT: Fa
ilure to find org.springframework.roo:org.springframework.roo.annotations:jar:1.
2.0.BUILD-SNAPSHOT in http://maven.springframework.org/release was cached in the
 local repository, resolution will not be reattempted until the update interval
of spring-maven-release has elapsed or updates are forced -> [Help 1]

Thanks in advance for any information.

Lajos Papp replied on Thu, 2011/11/03 - 4:49pm in response to: Roger Itai

Hi Roger, The annotations.jar with version 1.2.0.BUILD-SNAPSHOT is missing from you local maven repo. It is part of the dowloaded spring-roo-1.2.0.BUILD-SNAPSHOT_XXX.zip, so you can install it with the following command:
mvn install:install-file -DgroupId=org.springframework.roo -DartifactId=org.springframework.roo.annotations -Dversion=1.2.0.BUILD-SNAPSHOT -Dpackaging=jar -Dfile=spring-roo-1.2.0.BUILD-SNAPSHOT/annotations/org.springframework.roo.annotations-1.2.0.BUILD-SNAPSHOT.jar

Alan Stewart replied on Fri, 2011/11/04 - 6:57pm

Thanks Lajos for exercising the JSF/PrimeFaces add-on before its release in Roo 1.2 RC1. If you find any issues please log Jira tickets so I can fix them before we release 1.2. I'm constantly making changes and tweaks, so always use the latest Roo snapshot code.

Cheers
Alan Stewart, Spring Roo Project Lead

Prince Benin replied on Mon, 2011/11/07 - 3:55pm

Hi Lajos,

I got following error when i want to deploy my application:

roo> cloud foundry deploy --appName rooException in thread "Spring Roo JLine Shell" java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.roo.shell.Completion
        at org.springframework.roo.shell.SimpleParser.complete(SimpleParser.java:694)
        at org.springframework.roo.shell.jline.JLineCompletorAdapter.complete(JLineCompletorAdapter.java:35)
        at jline.ConsoleReader.complete(ConsoleReader.java:936)
        at jline.ConsoleReader.readLine(ConsoleReader.java:603)
        at jline.ConsoleReader.readLine(ConsoleReader.java:457)
        at jline.ConsoleReader.readLine(ConsoleReader.java:283)
        at org.springframework.roo.shell.jline.JLineShell.promptLoop(JLineShell.java:395)
        at org.springframework.roo.shell.jline.JLineShell.run(JLineShell.java:158)
        at java.lang.Thread.run(Unknown Source)

 Can you help me please?

 

Paap Joop replied on Mon, 2011/12/12 - 5:46am

Hi Lajos, nice blog

when i execute "mvn package ....." , i get error that could not resolve dependencies with annotations.jar  which i have installed by mvn install:install-file -DgroupId=org.springframework.roo -DartifactId=org.springframework.roo.annotations -Dversion=1.2.0.BUILD-SNAPSHOT -Dpackaging=jar -Dfile=spring-roo-1.2.0.BUILD-SNAPSHOT/annotations/org.springframework.roo.annotations-1.2.0.BUILD-SNAPSHOT.jar

Thank you in advance.

Paap Joop replied on Mon, 2011/12/12 - 6:48am in response to: Paap Joop

it's working now! i've done error when installing annotations. i did'nt specify the good path -Dfile=d:/spring-roo-1.2.0.BUILD-SNAPSHOT/annotations.

thanks

Hadrien Boye replied on Tue, 2011/12/20 - 5:14am

Hi, thankyou for this great article which makes concrete spring roo come blazing fast !

i tried to deloy my app on cloud foundry (http://organstore.cloudfoundry.com), and i get this error when i try accessing a list or create page :

 exception

javax.servlet.ServletException: Failed to parse the expression [#{applicationBean.getColumnName(column)}]
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:229)
	org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
	org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

javax.el.ELException: Failed to parse the expression [#{applicationBean.getColumnName(column)}]

[...]

 is it something related to JSF version supported by cloud foundry ? any hint ?

edit :  potential answer : support of passing arguments to methods call in Expression Language was introduced in JSP 2.2. Which is not suported by Tomcat before v7... (Cloud foundry runs Tomcat 6).

 > wich container do you use for http://roojsfpreview.cloudfoundry.com/ ?

Balasubramanian... replied on Wed, 2011/12/21 - 3:52pm

I do not like to generate my gui from managed bean, i prefer to use xhtml only. How i do remove generating gui code in managed bean

Anabelle Trinidad replied on Tue, 2011/12/27 - 8:45pm

Hi Lajos..great article. Just wanna ask, how were you able to add the theme switcher in the footer? I'm using SPRING ROO 1.2.0.RC1. thanks!

Rudi Gruteke replied on Sat, 2012/01/07 - 2:57am in response to: Hadrien Boye

I tried with roo 1.2 GA. Exactly the same problem. It works on Tomcat 7.0.23 as local install, but not on Tomcat 6.0.35 local and not in cloud foundry. Is there any bypass?

Carla Brian replied on Thu, 2012/06/14 - 6:10pm

This is nice. I think this is a good software. I will install this application now. - Joe Aldeguer

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.