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 ;-)

1 comment:

  1. Hello badr i am mohamed sabra from egypt.i missed you so much.can u help me in this issue that include when you use the lazy load data table the error messages and other messages type dropped from managed bean not appear on the page.

    ReplyDelete