我试图验证表单的字段,但我收到的错误是我无法解决的。
myForm 是一个包含名为 filtro 的对象(过滤器)的表单,该对象扩展了另一个过滤器(基本过滤器)。而且,我正在尝试验证myFilter的继承字段。
HTML
<form th:object="${myForm}" method="post">
<div style="padding-top: 15px;">
<input type="text" th:field="${myForm.filtro.ricercaBaseCup.descrizione}" th:errorclass="fieldError"/>
</div>
<div padding-top: 15px;">
<input type="text" th:field="${myForm.filtro.ricercaBaseGeografico.descrizione}" th:errorclass="fieldError"/>
</div>
<!--error messages-->
<ul th:if="${#fields.hasErrors('*')}" style="padding:0px; color:red">
<li class='errorClass' th:each="err : ${#fields.errors('*')}" th:text="${err}"></li>
</ul>
</form>
CONTROLLER
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String search(@Valid @ModelAttribute("myForm") MyForm form,
BindingResult bindingResult,
Model model){
//Validation
new MyFormValidator().validate(new Object[]{form}, bindingResult);
if(bindingResult.hasErrors()){
bindingResult.getFieldError();
return rootPath;
}
VALIDATOR
public class MyFormValidator implements Validator{
@Override
public boolean supports(Class<?> clazz) {
return MyForm .class.equals(clazz);
}
@Override
public void validate(Object obj, Errors errors) {
Object[] obj = (Object[])obj;
MyForm form = (MyForm)obj[0];
if(form.getFiltroBase().getRicercaBaseCup().getDescrizione() != null && form.getFiltroBase().getRicercaBaseCup().getDescrizione().length() < 15){
errors.rejectValue("filtro.ricercaBaseCup.descrizione", "gestOperativita.codCup.nonValido");
}else if(form.getFiltroBase().getRicercaBaseCup().getDescrizione() == null){
errors.rejectValue("filtro.ricercaBaseCup.descrizione", "gestOperativita.codCup.isEmpty");
}
if(form.getFiltroBase().getRicercaBaseGeografico().getDescrizione() == null){
errors.rejectValue("filtro.ricercaBaseGeografico.descrizione", "gestOperativita.Ente.isEmpty");
}
}
}
在return rootPath;
指令后,我刷新包含错误消息的html片段,但是..
当我尝试时,我得到了这个:
评估SpringEL表达式的异常:&#34;#fields.hasErrors(&#39; *&#39;)&#34;
(/ controllaDocFinanziaria / gestioneOperativita:86)23:23:58,603错误 [org.apache.catalina.core.ContainerBase [jboss.web] [缺省主机]。[/ MutuiBEI]。[调度]] (http-localhost / 127.0.0.1:8080-1)JBWEB000236:Servlet.service()for servlet调度程序抛出异常:java.lang.NullPointerException