我正在使用 JSF 2.0 创建 Web 应用程序,并在其中验证全名。
<h:inputText value="#{PersonalInformationDataBean.fullName}" size="75" id="fullName" >
<f:validator validatorId="fullNameValidator" />
</h:inputText>
<font color="red"><br /><h:message for="fullName"/></font>
在下面的java中是我所拥有的
public class FullNameValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
String enteredName = (String) value;
// Pattern p = Pattern.compile("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
Pattern p = Pattern.compile("([a-zA-Z\\s]+)");
Matcher m = p.matcher(enteredName.trim());
System.out.println("trimmed data is " + enteredName.trim());
boolean matchFound = m.matches();
if (enteredName.trim().length() == 0) {
FacesMessage message = new FacesMessage();
message.setSummary("Please enter name.");
throw new ValidatorException(message);
}
if (enteredName.trim().length() < 10) {
FacesMessage message = new FacesMessage();
message.setSummary("Name should be atleast 10 characters.");
throw new ValidatorException(message);
}
if (!matchFound) {
FacesMessage message = new FacesMessage();
message.setSummary("Invalid Name.");
throw new ValidatorException(message);
}
// FacesMessage message = new FacesMessage();
// message.setSummary("");
// throw new ValidatorException(message);
}
}
当我在本地运行项目时,它运行完美。
当我把这个项目放到网上时,我遇到了问题。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ fullName data as + Error Message +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 123343543534545 + Invalid Name +
+ fahim + Full name should be 10 characters +
+ null (blank) + NO MESSAGE, here I was expecting +
+ + result as Please enter name +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
我不明白当我没有传递任何值时,为什么我没有收到错误消息说请输入名称。知道我在这里缺少什么吗?
笔记:
我什至没有trimmed data is
在catalina.out
文件中获取我打印的所有消息System.out.println
问题是当我当时传递数据时只调用验证。其他验证没有发生。请让我知道我在这里缺少什么。