我有 3 h:inputText :名称、起始编号和结束编号。
如 Bean 类所示,我有两个自定义验证函数 validateName 和 validateNumber。这些函数的调用如 show.xhtml 中所示。
其中 validateName() 成功运行,但在 validateNumber() 处生成错误,如下所示:
show.xhtml @83,93 验证器="#{myBean.validateNumber}": java.lang.NullPointerException
第 83 行是:
<h:inputText id="idstart" value="#{myBean.start}" validator="#{myBean.validateNumber}"/>
显示.xhtml:
<h:inputText id="name" required="true" requiredMessage="Name cannot be empty!" value="#{myBean.name}" validator="#{myBean.validateName}">
<rich:message for="name"/>
<h:inputText id="idstart" value="#{myBean.start}" validator="#{myBean.validateNumber}"/>
<rich:message for="id1"/>
<h:inputText id="id2" value="#{myBean.end}" />
SessionScoped bean:
import javax.faces.application.Application;
import javax.faces.validator.ValidatorException;
public class MyBean
{
private Integer start;
private Integer end;
private String name;
public void validateName(FacesContext context,UIComponent component,Object value)throws ValidatorException
{
String name=value.toString();
/*Code for having Unique name */
FacesMessage msg = new FacesMessage("Unique Validation failed","Name used is already existing");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
public void validateNumber(FacesContext context,UIComponent component,Object value)throws ValidatorException
{
if(start > end)
{
FacesMessage msg = new FacesMessage("Number Validation failed","Start Number Should be less the End Number");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}