大家好,每当我在春天尝试调用验证器时,我都会收到以下错误
Servlet.service() for servlet spring threw exception: java.lang.IllegalStateException: Invalid target for Validator
请查看并帮助我解决此错误,以前我使用了登录页面的验证,它工作正常,但现在无法正常工作。
这是我的代码片段。
控制器
@Controller
public class NewUserRegistration
{
@Autowired
private UserService userService;
@Autowired
private NewUserValidator newUserValidator;
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.setValidator(newUserValidator);
}
@RequestMapping(value="/newUserAdd", method=RequestMethod.POST)
public String addUser(@ModelAttribute("user")@Valid User user,BindingResult result, Model model)
{
return "NewUser";
}
}
验证器
@Component
public class NewUserValidator implements Validator
{
@Override
public boolean supports(Class<?> classz)
{
return NewUserRegistration.class.equals(classz);
}
@Override
public void validate(Object obj, Errors error)
{
//Validation login for fields
}
}
JSP 页面
<form:form action="newUserAdd" method="POST" modelAttribute="user">
<center>
<table>
<tr><td>User Id:</td><td><input name="userId" type="text" /></td><td><font color="red"><c:out value="${userIdError}" /></font> </td></tr>
<tr><td>Password:</td><td><input name="userPassword" type="password"/></td><td><font color="red"><c:out value="${userPasswordError}" /></font></td></tr>
<tr><td>Confirm Password:</td><td><input name="userConfirmPassword" type="password"/></td><td><font color="red"><c:out value="${userPasswordError}" /></font></td></tr>
<tr><td>Name:</td><td><input name="userName" type="text"/></td><td><font color="red"><c:out value="${userPasswordError}" /></font></td></tr>
<tr><td></td><td><input type="submit" value="Create"/></td></tr>
</table>
</center>
</form:form>