【发布时间】:2011-07-08 22:30:32
【问题描述】:
我想在我的托管 bean 中验证对我的 JSF 页面的输入,但由于某种原因它不起作用?
@ManagedBean
@RequestScoped
public class RegistrationController {
//values passed from the JSF page
private String name;
...
public void validateName(FacesContext context, UIComponent validate,
Object value) {
String inputFromField = (String) value;
String simpleTextPatternText = "^[a-zA-Z0-9]+$";
Pattern textPattern = null;
Matcher nameMatcher = null;
textPattern = Pattern.compile(simpleTextPatternText);
nameMatcher = textPattern.matcher(getName());
if (!nameMatcher.matches()) {
((UIInput) validate).setValid(false);
FacesMessage msg = new FacesMessage(
"your name cant contain special characters");
context.addMessage(validate.getClientId(), msg);
}
}
这是输入组件的样子(在表单中):
<h:inputText value="#{registrationController.name}" validator="#{registrationController.validateName}" required="true">
<h:message for="nameInput"/>
当我输入错误的输入时,我看不到验证消息,在控制台中我看到:
INFO:实例化了 org.hibernate.validator.engine.resolver.JPATraversableResolver 的一个实例。 信息:警告:FacesMessage(s) 已入队,但可能尚未显示。 sourceId=bRegForm:j_idt7[severity=(ERROR 2), summary=(bRegForm:j_idt7: Validation Error: Value is required.), detail=(bRegForm:j_idt7: Validation Error: Value is required.)]
可能是什么?我忘记了什么吗?我是否必须在我的配置文件中添加一些内容...?
【问题讨论】:
标签: java validation jsf jsf-2 java-ee-6