【发布时间】:2017-10-27 16:22:12
【问题描述】:
我在 Spring 中进行验证时遇到问题。打开表单后出现以下错误:
java.lang.IllegalStateException: Invalid target for Validator [com.example.validator.UserValidator@6ac0a8f4]: com.example.web.forms.UserDTO@4d3b2379
我的 Validator 暂时,想先检查是否有任何工作:
@Component
public class UserValidator implements Validator {
@Autowired
ServiceUser serviceUser;
@Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
User user = (User) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "NotEmpty.userForm.name");
}
}
控制器:
@Controller
public class UserController {
@Autowired
UserValidator userValidator;
@InitBinder
public void initBinder(WebDataBinder binder) {
CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true, 10);
binder.registerCustomEditor(Date.class, editor);
binder.setValidator(userValidator);
// binder.addValidators(userValidator);
}
final static Logger logger = Logger.getLogger(UserController.class);
@Autowired
protected ServiceUserImpl service;
@RequestMapping("/lista")
public String showIndex(Model model) {
User contest = new User();
model.addAttribute("element", contest);
model.addAttribute("collection", service.findAll());
return "lista";
}
@RequestMapping("/dodaj")
public String showFormPublication(HttpServletRequest request, @ModelAttribute("userDto") @Valid UserDTO userDTO, BindingResult result) {
if (request.getMethod().equalsIgnoreCase("post") && !result.hasErrors()) {
if (result.hasErrors()) {
return "forms/contest";
} else {
User user = new User();
user.setId(userDTO.getId());
user.setName(userDTO.getName());
user.setSurname(userDTO.getSurname());
user.setDateOfBirth(userDTO.getDateOfBirth());
user.setIndexNumber(userDTO.getIndexNumber());
user.setEmail(userDTO.getEmail());
service.save(user);
return "redirect:/lista";
}
}
return "dodaj";
}
}
.jsp 中的表单:
<form:form action="dodaj" method="POST" modelAttribute="userDto">
<table border="1">
<tbody>
<tr>
<th>Imię</th>
<td>
<form:input type="text" path="name" />
<c:if test="${pageContext.request.method=='POST'}"><form:errors path="name" /></c:if>
</td>
</tr>
<tr>
<th>Nazwisko</th>
<td>
<form:input type="text" path="surname" />
<c:if test="${pageContext.request.method=='POST'}"><form:errors path="surname" /></c:if>
</td>
</tr>
<tr>
<th>DataUrodzenia</th>
<td>
<form:input type="date" path="dateOfBirth" />
<c:if test="${pageContext.request.method=='POST'}"><form:errors path="dateOfBirth" /></c:if>
</td>
</tr>
<tr>
<th>NumerIndeksu</th>
<td>
<form:input type="number" path="indexNumber" />
<c:if test="${pageContext.request.method=='POST'}"><form:errors path="indexNumber" /></c:if>
</td>
</tr>
<tr>
<th>Email</th>
<td>
<form:input type="text" path="email" />
<c:if test="${pageContext.request.method=='POST'}"><form:errors path="email" /></c:if>
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Dodaj!" /></td>
</tr>
</tbody>
</table>
</form:form>
我尝试在 @InitBinder 旁边添加 ("userDto"),但不幸的是它没有帮助。在适用的解决方案方面还没有找到更多。如果还有什么我应该在这里发布的,请告诉我。如果有人急于尝试运行它,我还可以提供存储库的链接。
【问题讨论】:
标签: java spring validation spring-mvc