Create a custom converter which accepts multiple patterns by <f:attribute>
on the component.
Here's how you'd like to have the view to look like:
<h:inputText id="input" value="#{bean.date}">
<f:converter converterId="multiDateConverter" />
<f:attribute name="pattern1" value="MM/dd/yyyy" />
<f:attribute name="pattern2" value="MM-dd-yyyy" />
</h:inputText>
And here's how the converter can look like (for JSF 1.x, register it as
<converter-id>multiDateConverter</converter-id>
in faces-config.xml
instead)
@FacesConverter(value="multiDateConverter")
public class MultiDateConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
List<String> patterns = getPatterns(component);
Date date = null;
for (String pattern : patterns) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setLenient(false); // Don't parse dates like 33-33-3333.
try {
date = sdf.parse(value);
break;
} catch (ParseException ignore) {
//
}
}
if (date == null) {
throw new ConverterException(new FacesMessage("Invalid date format, must match either of " + patterns));
}
return date;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
return new SimpleDateFormat(getPatterns(component).get(0)).format((Date) value);
}
private static List<String> getPatterns(UIComponent component) {
List<String> patterns = new ArrayList<String>();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
String pattern = (String) component.getAttributes().get("pattern" + i);
if (pattern != null) {
patterns.add(pattern);
} else {
break;
}
}
if (patterns.isEmpty()) {
throw new IllegalArgumentException("Please provide <f:attribute name="patternX"> where X is the order number");
}
return patterns;
}
}
Note that it only picks the first (default) pattern to redisplay the value. So in the above example, if you enter 05-10-2011
, then it get redisplayed as 05/10/2011
.
Unrelated to the concrete problem, the pattern MM-dd-yyyy
isn't very common. Didn't you mean to use dd-MM-yyyy
?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…