我想验证数据库中是否存在社会安全号码。这是我的验证者。
@RequestScoped
@FacesValidator("personalIdValidator")
public class PersonalIdValidator implements Validator {
@EJB
private UserDaoLocal userDao;
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
boolean ok;
String personalId = value.toString();
String regex = "[0-9]{11}";
ok = Pattern.matches(regex, personalId);
if (!ok || userDao.findUserExist(personalId)) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Błędny pesel", "Błędny pesel"));
}
}
}
在Validator中,我有一个@EJB userDao,我做了一个方法userDao.findUserExist(personalId)
这个方法:
@Override
public boolean findUserExist(String personalId) {
try {
Query q = em.createQuery("SELECT u FROM User u WHERE u.personalId = :personalId");
q.setParameter("personalId", personalId);
user = (User) q.getSingleResult();
if(user == null) {
return false;
} else {
return true;
}
} catch (Exception e) {
System.out.println("Błąd" + e.getMessage());
return false;
}
}
但是当我这样做时,我有一个错误:
警告:StandardWrapperValve [Faces Servlet]:PWC1406: servlet Faces Servlet的Servlet.service()引发了异常 java.lang.NullPointerException at pl.ePrzychodnia.validators.PersonalIdValidator.validate(PersonalIdValidator.java:41) 在javax.faces.component.UIInput.validateValue(UIInput.java:1149)at javax.faces.component.UIInput.validate(UIInput.java:967)at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)at at javax.faces.component.UIInput.processValidators(UIInput.java:698)at at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 在 javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 在 org.primefaces.component.panel.Panel.processValidators(Panel.java:297) 在javax.faces.component.UIForm.processValidators(UIForm.java:253) 在 javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 在 javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 在 javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172) 在 com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 在 org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 在 org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 在 org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 在 com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 在 com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) 在 com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 在 com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 在 com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) 在 com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 在 com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 在 com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 在 com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 在 com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 在 com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 在com.sun.grizzly.ContextTask.run(ContextTask.java:71)at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532) 在 com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513) 在java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:0)
第一个:
替换
String personalId = value.toString();
通过
String confirm = (String) component.getAttributes().get("theInputFromYourPage");
你也可以试试这个:
private static final String regex = "[0-9]{11}";
private Pattern pattern;
private Matcher matcher;
public PersonalIdValidator() {
pattern = Pattern.compile(regex);
}
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
// TODO Auto-generated method stub
matcher = pattern.matcher(value.toString());
if (!matcher.matches()) {
// DO YOUR STUFF QUERY
} else {
// FacesMessages, invite the user to respect regex [0-9]{11}
}
}