我有这样的情景。
我有jsp文件,有6个字段:userId,userName,lastName,dob,emailId,password。
<form:form method="POST" modelAttribute="keyUserRegistration" action="checkRegistration.do">
<table width="100%" border="0">
<tr>
<td><div class="form-control">
<form:errors path="userId" ></form:errors>
<label>
<p>
User ID<small class="required"></small>
</p>
<form:input path="userId" id="userId"/>
</label> </div>
</td>
</tr>
</table>
</form:form>
以类似的方式我写了所有其他属性。
我有一个Controller来处理来自我页面的请求,如下所示:
@RequestMapping(value = "/checkRegistration", method =
RequestMethod.POST)
public String submitLogin( @ModelAttribute("keyUserRegistration")
UserRegistration userR, BindingResult result, Model model,
) {
if (userR != null && !userR.equals("null")) {
registrationValidator.validate(userR, result);
if (result.hasErrors()) {
System.out.println("controller result has errors");
return "registration";
}
}
我有Validator类来验证表单:
@Component
public class RegistrationValidator implements Validator{
public boolean supports(Class<?> clazz) {
return UserRegistration.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
UserRegistration registration = (UserRegistration) target;
ValidationUtils.rejectIfEmpty(errors, "userId", "Please enter User Id");
ValidationUtils.rejectIfEmpty(errors, "emailId", "Please enter Email Id");
ValidationUtils.rejectIfEmpty(errors, "password", "Please enter Password");
ValidationUtils.rejectIfEmpty(errors, "lastName", "Please enter Last Name");
ValidationUtils.rejectIfEmpty(errors, "dob", "Please enter Date Of Birth");
ValidationUtils.rejectIfEmpty(errors, "userName", "Please enter User Name");
}
}
我总是遇到例外:
org.springframework.context。 NoSuchMessageException :代码“请输入用户ID.keyUserRegistration.userId”代码 locale'en_US “
我的问题:
有没有办法使用spring验证器在jsp上实现错误消息,但是没有声明消息属性文件和资源包?
答案 0 :(得分:0)
试试这个方法:
UserRegistration registration = (UserRegistration) target;
System.out.println(registration.getUserId());
ValidationUtils.rejectIfEmpty(errors, "userId","error.userId", "Please enter User Id");
答案 1 :(得分:0)
尝试:
ValidationUtils.rejectIfEmpty(errors, "userId","error.userId", "Please enter User Id");
答案 2 :(得分:0)
尝试此方法 -
ValidationUtils.rejectIfEmpty(错误,“您的属性”,“error.your属性”,“错误消息”)