Bootstrapping a JSF 2 Project

I needed a break this afternoon, so I thought I'd see how easy it is to bootstrap a JSF 2 project. One of the biggest complaints about JSF 1.x is all that XML, so JSF 2 is aiming to fix that. How have we done so far? Based on this quick look (which is my first from-scratch JSF 2 app), really, really well. Here are the steps I took:
  • Create a web app project using the Maven archetype (because I'm lazy that way : ) :
    mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp \\
        -DarchetypeArtifactId=maven-archetype-webapp
  • Add FacesServlet to web.xml:
        
            Faces Servlet
            javax.faces.webapp.FacesServlet
        
        
            Faces Servlet
            \*.jsf
        
    
  • Add the JSF 2 jars (and their repo) to pom.xml:
        
            
                maven2-repository.dev.java.net
                Java.net Repository for Maven
                http://download.java.net/maven/2
            
        
    
        
            
                com.sun.faces
                jsf-api
                2.0.0-b08
                compile
            
            
                com.sun.faces
                jsf-impl
                2.0.0-b08
                compile
            
        
    
  • Create a managed bean:
    @ManagedBean(name="main", eager=true)
    public class MainBean {
        public MainBean() {
            System.err.println ("MainBean starting up!");
        }
    
        public String getText() {
            return "Here is some text!";
        }
    }
  • Create the view:
    
        
            #{main.text}
        
    
    
  • Build, deploy, browse
Your web browser should now say "Here is some text!" Did you notice that all but two of those steps are things you would do for any Java web app? As soon as someone creates a JSF 2 Maven archetype, even that can be simplified. Also notice that there was no mention of faces-config.xml. The application doesn't have one. Granted, this is an insanely simple application, so the need for faces-config.xml may still arise (navigation comes to mind, but I think we're working on a simplification for that too). The rule of thumb seems to be that JSF is not well-suited for smaller, simpler apps where things like Grails, RoR, etc. are a better choice. I think we'll see that JSF 2 changes that. [tags]jsf2[/tags]
Comments:

Post a Comment:
Comments are closed for this entry.
About

Jason Lee

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today