|
A Java Server Faces application can be associated with the Java Bean known as backing bean, which we have just seen in the registration example. An application gets and sets the object data for a component by calling the appropriate object properties for that component. When a component is bound to an object, the application has two views i.e. Model View which represents the bean properties and the presentation view which actually displayed on the UI. The most of the development time and effort is wasted to show and maintain the data in both the layers in appropriate format. For example:- If with in the same application user want to display the date in different format then it is very cumbersome as he needs to code the different styles for different pages. The JSF conversion model implicitly convert the different data automatically.
The coming example will show the usefulness of the conversion model of the JSF. We take an example that a java.util.Date is coming from the backing bean and UI will display the dates in various formats. The tag <f:convertDateTime \ > is used to convert the date in various properties by just setting the attribute dateStyle with values. The following formats are available in the JSF:-
- default:- Jan 07, 2008 2:35:45 PM ( Default Value)
- short:- 01/07/08 22:55:42 PM
- medium:- Jan 07, 2008 2:42:36 PM
- long:- Jan 07, 2008 2:41:08 PM
- full:- Monday, Jan 07, 2008 2:39:56 PM
Example:-
Step 1:-We need to create the seperate jsp i.e. dateFormat JSP for the example. The code for the jsp is as follows :-
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- This file is an entry point for JavaServer Faces application. --%>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <f:view> <h:outputText value="#{MyBean.today}"> <f:convertDateTime type="both" dateStyle="full"/> </h:outputText> </f:view> </body> </html>
|
Step 2:- We need to create a backing bean to get the date from the bean to the UI. The code is as follows:-
package com.visualbuilder;
import java.util.Calendar; import java.util.Date; import java.util.Locale;
public class MyBean {
public Date getToday() { Calendar cal = java.util.Calendar.getInstance(Locale.US); return cal.getTime(); }
}
|
|