background preloader

JSF

Facebook Twitter

Primefaces

Arun Gupta, Miles to go ...: TOTD #147: Java Server Faces 2.0 Composite Components using NetBeans - DRY your code. The Java Server Faces 2.0 uses Facelets instead of JSP as the view declaration language. This allows "view" part of MVC to be completely written using XHTML and CSS only and all the business logic resides in the backing bean. This enables a cleaner separation of views with model and controller and thus follows the MVC design pattern in a more intuitive way. JSF 2 also defines how resources can be packaged, located, and rendered by JSF runtime within a web application. Using these two features of Facelets and Resource Handling, JSF2 defines a composite component as a component that consists of one or more JSF components defined in a Facelet markup file that resides inside of a resource library.

A composite component can be defined using JSF 1.2 as well but it requires a much deeper understanding of JSF lifecycle and also authoring multiple files. Code is king! Lets say a Facelet (index.xhtml) has the following code fragment: and click on "Finish". and then replace the code fragment with: JSF Corner. Composite Components in JSF 2.0.

Since JSF 2.0, it’s very easy to create a reusable component, known as composite components. In this tutorial, we show you how to create a simple composite components (stored as “register.xhtml“), which is an user registration form, includes name and email text fields (h:inputText) and a submit button (h:commandButton). In addition, we also show you how to use it. Create a Composite Components Here’s the steps to create a composite components : 1. Create a .xhtml file, and declared the composite namespace. 2. Uses composite tags composite:interface, composite:attribute and composite:implementation, to define content of the composite component. <html xmlns=" //... xmlns:composite=" name="anything" /></composite:interface><composite:implementation> #{cc.attrs.anything} </composite:implementation></html> 3.

Put composite components (“.xhtml” file) into JSF’s resources folder, see figure 1: JSF Page Navigation. Navigation rules are those rules provided by JSF Framework which describe which view is to be shown when a button or link is clicked. Navigation rules can be defined in JSF configuration file named faces-config.xml.Navigation rules can be defined in managed beans.Navigation rules can contain conditions based on which resulted view can be shown.JSF 2.0 provides implicit navigation as well in which there is no need to define navigation rules as such. Implicit Navigation JSF 2.0 provides auto view page resolver mechanism named implicit navigation.In this case you only need to put view name in action attribute and JSF will search the correct view page automatically in the deployed application.

Auto navigation in JSF page Set view name in action attribute of any JSF UI Component. <h:form><h3>Using JSF outcome</h3><h:commandButton action="page2" value="Page2" /></h:form> Auto navigation in Managed Bean Define a method in managed bean to return a view name. Conditional Navigation Forward vs Redirect <? Fluent Navigation in JSF 2. We Recommend These Resources In this article, the third in a series covering JavaServer Faces (JSF) 2.0 features contributed by Red Hat, or which Red Hat participated in extensively, you'll discover that getting around in a JSF 2 application is much simpler and requires less typing. With improved support for GET requests and bookmarkability, which the previous article covered, JSF 2 is decidely more nimble.

But not at the cost of good design. JSF no longer has to encroach on your business objects by requiring action methods to return navigation outcomes, but can instead reflect on the state of the system when selecting a navigation case. This article should give you an appreciation for how intelligent the navigation system has become in JSF 2. Three new navigation variants are going to be thrown at you in this article: implicit, conditional and preemptive. Implicit navigation is particularly useful for developing application prototypes, where navigation rules just get in the way. 1. 2. 8. PrimeFaces Tutorial (Prime Faces for JSF 2) with Eclipse. PrimeFaces. JSF 2.0 + Spring + Hibernate integration example. Here’s a long article to show you how to integrate JSF 2.0, Spring and Hibernate together. At the end of the article, you will create a page which display a list of the existing customer from database and a “add customer” function to allow user to add a new customer into database.

P.S In this example, we are using MySQL database and deploy to Tomcat 6 web container. 1. Project Structure Directory structure of this example 2. Create a customer table and insert 2 dummy records. DROP TABLE IF EXISTS `mkyongdb`. 3. A model class and Hibernate mapping file for customer table. File : Customer.java package com.mkyong.customer.model; import java.util.Date; public class Customer{ public long customerId; public String name; public String address; public Date createdDate; //getter and setter methods } File : Customer.hbm.xml 4. Spring’s BO and DAO classes for business logic and database interaction. File : CustomerBo.java File : CustomerBoImpl.java File : CustomerDao.java File : CustomerDaoImpl.java 5. 6. <? 8. Injecting Managed beans in JSF 2.0. In JSF 2.0, a new @ManagedProperty annotation is used to dependency injection (DI) a managed bean into the property of another managed bean.

Let see a @ManagedProperty example : MessageBean.java – A managed bean named “message“. import java.io.Serializable;import javax.faces.bean.ManagedBean;import javax.faces.bean.SessionScoped; @ManagedBean(name="message") @SessionScoped public class MessageBean implements Serializable { //business logic and whatever methods... } HelloBean.java – Inject the “message” bean into the “messageBean” property. import java.io.Serializable;import javax.faces.bean.ManagedBean;import javax.faces.bean.ManagedProperty;import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class HelloBean implements Serializable { @ManagedProperty(value="#{message}") private MessageBean messageBean; //must povide the setter method public void setMessageBean(MessageBean messageBean) { this.messageBean = messageBean; } //...}

Download Source Code Reference. JSF 2.0 and Resource Bundles example. In this tutorial, we demonstrate the use of resource bundle to display messages in JSF 2.0. For maintainability concern, it’s always recommended to put all the messages in properties file, instead of hard-code the message in page directly. 1. Properties File Create a properties file, contains message for the page, and put it into the project’s resource folder, see figure below messages.properties Project folder structure. 2. There are two ways to load the properties file into JSF 2.0. 1. Faces-config.xml <? 2. 3. In this case, the messages.properties file is given a name of “msg“, to access the message, just use “msg.key“. hello.xhtml 4.

Example 1 A normal way to access the message. <h:outputText value="#{msg.message}" /> //properties file message = This is "message" Example 2 For a key that has a dot “.” as name, you can’t use the normal way #{msg.message.test1}, it will not work. <h:outputText value="#{msg['message.test1']}" /> //properties file message.test1 = This is "message.test1" 5. JSF 2.0 Tutorial (JavaServer Faces 2 Tutorial) with Eclipse.