If you have a custom converter or validator you may provide these with the option of letting developers specify attributes. For example, let's say that we are creating a converter that will allow the formatting of a Calendar
object. Note, that the <f:convertDateTime />
is for Date
objects (which can be easily retrieved from a Calendar
, but that is beside the point). So the custom converter I'll create here is for converting/formatting Calendar
objects.
package com.blogspot.bigallan.converters;
import java.text.*;
import java.util.*;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
public class CalendarConverter implements Converter {
/**
* Not implemented as the converter is one-way only.
*/
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
return null;
}
public String getAsString(FacesContext ctx, UIComponent component, Object value) {
// Get the Calendar to convert
Calendar calendar = (Calendar) value;
String pattern = "";
// Obtain attributes specified along with the converter
Map<String, Object> attributes = component.getAttributes();
// Check if the pattern attribute was specified
if (!attributes.containsKey("pattern")) {
System.out.println("pattern was not specified");
return "";
} else {
pattern = (String)attributes.get("pattern");
}
// Create a DateFormatter for the pattern
DateFormat outputFormat = new SimpleDateFormat(pattern);
// Format the calendar
try {
Date date = originalFormat.parse(calendar.getTime());
return outputFormat.format(originalDate));
} catch (ParseException ex) {
ex.printStackTrace()
return "";
}
}
}
In
faces-config.xml
you would declare the converter like this:
<converter>
<converter-id>calendarConverter</converter-id>
<converter-class>com.blogspot.bigallan.converters.CalendarConverter</converter-class>
</converter>
We can now use the converter in a view:
<h:outputText value="#{myBean.calendarObject}">
<f:converter converterId="calendarConverter" />
<f:attribute name="pattern" value="d. MMMM yyyy" />
</h:outputText>
When the code is rendered in the browser you'll get the calendar converter to something like this 24. November 2008. So far so good. However, what if we didn't specify the date pattern as static text, but rather bind it to a value. This technique is efficient when you reuse the pattern in many places and want to keep the pattern in a central place. So say that we had a managed-bean called
common
with a method getDateFormat() : String
that would provide the pattern. The code would look like this:
<h:outputText value="#{myBean.calendarObject}">
<f:converter converterId="calendarConverter" />
<f:attribute name="pattern" value="#{common.dateFormat}" />
</h:outputText>
When the code is rendered in the browser you'll get a blank response and see the following in the log, "
pattern was not specified
". The log statement is generated from out code above where we check for the attribute named "pattern". This is because of a requirement in the JSF 1.2 specification (JSR-252) that states (Section 9.4.2) that literal (static) text should be stored in the attribute map of the component and non-literal texts should be stored in the value expression map of the component. So to get the pattern from the value expression map we modify the getAsString
method as follows:
public String getAsString(FacesContext ctx, UIComponent component, Object value) {
// Get the Calendar to convert
Calendar calendar = (Calendar) value;
String pattern = "";
// Obtain attributes specified along with the converter
Map<String, Object> attributes = component.getAttributes();
// Check if the pattern attribute was specified
if (!attributes.containsKey("pattern")) {
ValueExpression ve = component.getValueExpression("pattern");
if (ve == null) {
System.out.println("pattern was not specified");
return "";
} else {
pattern = (String) ve.getValue(ctx.getELContext());
}
} else {
pattern = attributes.get("pattern");
}
// Create a DateFormatter for the pattern
DateFormat outputFormat = new SimpleDateFormat(pattern);
// Format the calendar
try {
Date date = originalFormat.parse(calendar.getTime());
return outputFormat.format(originalDate));
} catch (ParseException ex) {
ex.printStackTrace()
return "";
}
}
The difference is that we now first check to see if there is a literal value specified. If not, we check if there is a value expression, and if there is no value expression either we will fail. If there is a value express we evaluate it and use it as our pattern.