我正在使用 JSF2.0、Spring 3 和 Hibernate 4。
在 jsf 中是否可以在 inputText 中以大写或小写形式输入文本或字符?我的意思是即使用户输入大写,inputText 也应该始终接受小写字符。
这在 JSF 2 中可能吗?
谢谢
您可以实现自己的验证器来验证文本大小写。检查本教程。但这不是客户端验证 - 请求将被发送到服务器,如果它不是有效的页面将被重新加载并显示验证错误消息。
您可以使用Converter自动转换输入;有关更多信息,请参阅 Java EE 6教程。
@FacesConverter("lowerConverter")
public class LowerConverter implements Converter {
@Override
public Object getAsObject(FacesContext cx, UIComponent component,
String value) throws ConverterException {
if(value == null) {
return null;
}
Locale locale = cx.getExternalContext().getRequestLocale();
return value.toLowerCase(locale);
}
@Override
public String getAsString(FacesContext cx, UIComponent component,
Object value) throws ConverterException {
if(value == null) {
return null;
}
return value.toString();
}
}