The h-namespace library corresponds to the HTML components. For example, the
·
<h:body> tag corresponds to the HTML <BODY> tag.
The f-namespace corresponds to the JSF-specific features. For example, the
·
<f:view> tag defines the JSF view. Within this tag, all the UI components,
including their hierarchy and state, will be stored by the JSF runtime environment.
The ui-namespace is used by Facelets to define the elements within the template.
·
For example, the <ui:insert> tag specifies that content should be inserted here.
The views using this template should provide the content for this template to
include.
Implementing a Custom Converter
For a contact's date of birth attribute, we use JodaTime. However, JSF and PrimeFaces don't support the
display of JodaTime natively. As a result, we need to implement a custom JSF converter to serve the
purpose of displaying the date of birth of a contact in the frontend.
In JSF, implementing a custom converter is quite easy. All we need to do is to implement the
javax.faces.convert.Converter interface. Listing 18-7 shows the class content.
Listing 18-7. A JSF Custom Converter
package com.apress.prospring3.ch18.web.converter;
import
javax.faces.component.UIComponent;
import
javax.faces.context.FacesContext;
import
javax.faces.convert.Converter;
import
javax.faces.convert.FacesConverter;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
@FacesConverter("jodaDataTimeConverter")
public class JodaDateTimeConverter implements Converter {
private static final String PATTERN = "yyyy-MM-dd";
@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
return DateTimeFormat.forPattern(PATTERN).parseDateTime(value);
}
@Override
public String getAsString(FacesContext ctx, UIComponent component, Object value) {
DateTime dateTime = (DateTime) value;
return DateTimeFormat.forPattern(PATTERN).print(dateTime);
}
}
In Listing 18-7, the JodaDateTimeConverter class implements JSF's Converter interface and overrides
the two methods getAsObject() (converts from String to JodaTime) and getAsString() (converts from
JodaTime to String). Also note that the class is annotated with @FacesConverter to indicate to the JSF
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home