1
我需要幫助。我想在Spring 3 MVC中使用自定義驗證器驗證表單輸入。 每一件事情是工作正常,但在錯誤的情況下不被顯示在表格錯誤在spring3中顯示驗證錯誤MVC
這是控制器
public class ContactController {
@ModelAttribute("Contact")
public Contact getContactObject() {
return new Contact();
}
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String processValidatinForm(@Valid Contact contact, BindingResult result, Map model,Errors e) {
UserValidator validator = new UserValidator();
validator.validate(contact, result);
if (result.hasErrors()) {
System.out.println("found Errors");
return "contact";
}
// Add the saved validationForm to the model
model.put("Contact", contact);
return "successs";
}
@RequestMapping("/contacts")
public String showValidatinForm(Map model) {
Contact contact = new Contact();
model.put("Contact", contact);
return "contact";
}
} 用戶驗證
public class UserValidator implements Validator {
@Override
public boolean supports(Class clazz) {
return Contact.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "Enter user name");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "age", "Enter Age");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "Enter password.");
}
}
,這是形式的JSP頁面
<form:form method="post" action="addContact.html" commandName="Contact">
<table>
<tr>
<td>User Name:<font color="red"><form:errors path="userName" /></font></td>
<tr>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Age:<font color="red"><form:errors path="age" /></font></td>
</tr>
<tr>
<td><form:input path="age" /></td>
</tr>
<tr>
<td>Password:<font color="red"><form:errors path="password" /></font></td>
</tr>
<tr>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
怎麼做對不對?
感謝您的回覆。
可能重複的[Spring表單驗證問題](http://stackoverflow.com/questions/4896367/spring-form-validation-problems) – axtavt 2012-08-06 08:23:56