Saturday, June 25, 2011

Set up JSF 2 web project (Maven+ Eclipse)

In this entry we show you how to configure your project with Maven and eclipse to support JSF capabilities.

Set up web project with Maven

JSF Dependency

  • For Java EE Application

In most "Java EE 6" application servers, it has "build-in support for JSF 2.0", so you only need to download one JSF API for development purpose.


com.sun.faces
jsf-api
2.1.0-b03
provided


  • For simple servlet container like Tomcat

You may need to download the following dependencies:




javax.servlet
jstl
1.2
runtime



javax.servlet
servlet-api
2.5
provided



javax.servlet.jsp
jsp-api
2.1
provided



javax.el
el-api
provided
1.0



com.sun.faces
jsf-api
2.1.0-b03



com.sun.faces
jsf-impl
2.1.0-b03



JSF 2.0 Servlet Configuration

Just like any other standard web frameworks, you are required to configure the JSF stuff in web.xml file as bellow




Set up web project


index.jsp



Faces Servlet
javax.faces.webapp.FacesServlet
1



Faces Servlet
/faces/*



javax.faces.DEFAULT_SUFFIX
.xhtml



javax.faces.PROJECT_STAGE
Development



Explanation for web.xml content
  1. Define the version 2.5 for your web module "web-app_2_5", it's so important to do that befor executing your first Maven install to keep your project compliant with Dynamic Web Module 2.5 (servlet specification 2.5) and thereafter add the JSF capabilities to your eclipse project (as shown in the next chapter)

  2. Add the JSF servlet in the web.xml, define a “javax.faces.webapp.FacesServlet” mapping to "/faces/*" ( you can also add the extension (*.jsf, *.xhtml,*.faces) to your mapping.

  3. Define a default mapping "javax.faces.DEFAULT_SUFFIX" with .xhtml file extension (not mandatory)

  4. In JSF 2.0 development, it’s recommended (not mandatory) to set the “javax.faces.PROJECT_STAGE” to “Development“, it will provide many useful debugging information to let you track the bugs easily (for deployment, just change it to “Production“)

Set up web project in eclipse

After importing your maven web project into eclipse, everything looks fine except the code assist doesn't work! ie : when you presse on the “Ctrl + Space” combination keys it prompts nothing?

To resolve that: in Eclipse project, you have to make sure the project is supported the WTP and JSF capabilities. so Right click on the project, choose properties, select “Project Facets“, make sure the “JavaServer Faces” is checked.
This allow you also to open your page with "web page editor" and drag and drop JSF component ...

No comments:

Post a Comment