Sunday, June 26, 2011

Getting Started with JSF 2 and Primefaces

In this entry we show you how you can start using primefaces with the JSF Mojara reference implementation !

So After setting up your web project with maven and eclipse you just need to import the JSF and Primefaces namespaces into your pages to test the first integration and display some components.

PS:You also need to add primefaces maven dependencies ...

JSF Namespace
JSF namespace is necessary to add JSF tags and components to your pages.

xmlns:f="http://java.sun.com/jsf/core"

xmlns:h="http://java.sun.com/jsf/html"

Primefaces Namespace
PrimeFaces namespace is necessary to add PrimeFaces components to your pages.

xmlns:p="http://primefaces.prime.com.tr/ui"

Test Run

That's it, now you can add a test for JSF and Primefaces component to see if everything is ok.











Congratulation !! executing the application and getting the correct display means JSF and Primefaces are working fine in your web application ...

If you want to deepen your JSF integration test, you can add a JSF managed bean and call it from your page !

Create a managed bean

Briefly a managed bean is a regular Java bean (simple pojo) with a fancy name. When we register the bean with JSF it becomes a managed bean, in other words, it’s now managed by the framework (JSF). For further information see learning-jsf2-managed-beans and whats-new-in-jsf-2

Now, let's create HelloMyBlog our own managed bean !

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "helloMyBlog")//or just @Named only from CDI
@RequestScoped
public class HelloMyBlog {
private String webFramework;

public HelloMyBlog(){
webFramework = "JSF 2 and Primefaces";
}

public String getWebFramework() {
return webFramework;
}

public void setWebFramework(String webFramework) {
this.webFramework = webFramework;
}
}

After, we create our view helloMyBlog.xhtml witch use the managed bean HelloMyBlog by name "helloMyBlog" ( @ManagedBean(name = "helloMyBlog"), or just @Named from CDI )












Finaly, Run your application ...

Your web browser should now display the same result as our first test !

Conclusion

We covered in this article the basic test and integration for a simple JSF application with Primefaces component libraries.

Befor going ahead with JSF developpement it's highly recomended to understand the life cycle of a JSF application see JSF application lifecycle

In general JSF is great if you have time to learn it before you have to use it as your main venue for a major project ;-)

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 ...