我正在尝试在Spring中使用验证组,但它似乎不想工作。
使用@Valid用户成员时,它可以正常工作。当我使用@Validated(FormValidationGroup.class)时,它没有。这是方法签名。
代码:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String doRegister(Model model, @Valid User member, BindingResult result) {
logger.info("Showing Registration Page....");
if (result.hasErrors()) {
return "createmembers";
}
if (userService.exists(member.getUsername())) {
result.rejectValue("username", "Duplicate Key",
"This email address has already been used");
return "createmembers";
}
else {
userService.create(member);
return "registerSuccess";
}
}
其他方法
代码:
@RequestMapping("/createmembers")
public String createMembers(Model model) {
logger.info("Showing Create Members Page....");
model.addAttribute("member", new User());
return "createmembers";
}
我突出强调的部分就是这一切。
跟踪中的相关错误似乎是
java.lang.IllegalStateException:BindingResult和bean名称'member'的普通目标对象都不可用作请求属性
JSP页面如下
代码:
<sf:form id="details" method="post" action="${pageContext.request.contextPath}/register" commandName="member">
<table class="formtable">
<tr><td>Name</td><td><sf:input name = "name" path="name" type="text"/><br/><sf:errors path="name" cssClass="error"></sf:errors></td></tr>
<tr><td>Password</td><td><sf:input id="password" name = "password" path="password" type="password"/><br/><sf:errors path="password" cssClass="error"></sf:errors></td></tr>
<tr><td>Confirm Password</td><td><input id = "con_password" name = "con_password" type="password"/><div id="matchpass"></div></td></tr>
<tr><td>Email</td><td><sf:input name = "username" path="username" type="text"/><br/><sf:errors path="username" cssClass="error"></sf:errors></td></tr>
<tr><td>Gender</td><td><sf:select name="gender" path="gender" class="select"><option value = "M">Male</option><option value = "F">Female</option></sf:select></td></tr>
<tr><td>Membership Type</td><td><sf:select name="member_type" path="member_type"><option value = "Senior">Senior</option><option value = "Junior">Junior</option><option value = "Student">Student</option></sf:select></td></tr>
<tr><td>Grade</td><td><sf:select name="grade" path="grade"><option value = "Graded">Graded</option><option value = "Intermediate">Intermediate</option><option value = "Beginner">Beginner</option></sf:select></td></tr>
<tr><td>Address Line 1</td><td><sf:input name = "ad_line1" path="ad_line1" type="text"/><br/><sf:errors path="ad_line1" cssClass="error"></sf:errors></td></tr>
<tr><td>Address Line 2</td><td><sf:input name = "ad_line2" path="ad_line2" type="text"/><br/><sf:errors path="ad_line2" cssClass="error"></sf:errors></td></tr>
<tr><td>City/Town</td><td><sf:input name = "ad_city" path="ad_city" type="text"/><br/><sf:errors path="ad_city" cssClass="error"></sf:errors></td></tr>
<tr><td>County</td><td><sf:input name = "ad_county" path="ad_county" type="text"/><br/><sf:errors path="ad_county" cssClass="error"></sf:errors></td></tr>
<tr><td>Contact Number</td><td><sf:input name = "contact_num" path="contact_num" type="text"/><br/><sf:errors path="contact_num" cssClass="error"></sf:errors></td></tr>
<tr><td>Emergency Contact Name</td><td><sf:input name = "em_con_name" path="em_con_name" type="text"/><br/><sf:errors path="em_con_name" cssClass="error"></sf:errors></td></tr>
<tr><td>Emergency Contact Number</td><td><sf:input name = "em_con_num" path="em_con_num" type="text"/><br/><sf:errors path="em_con_num" cssClass="error"></sf:errors></td></tr>
<tr><td><input value="Register" type="submit"/></td></tr>
</table>
</sf:form>
这只是一个部分页面,因为我正在使用Apache Tiles,而且我省略了检查密码的脚本。
正如我所说,这对@Valid注释很好用,但是由于我从jdbc切换到hibernate,我正在失去验证。我希望用户的密码字段在5-15之间,但由于我正在对它们进行加密,因此Hibernate在将它们写入数据库时不会让它们通过验证。
我在用户点击Register时的理解,这映射到createmembers.jsp(实际上是使用header.jsp,footer.jsp和createmembers.jsp的Apache tile) - 这个jsp包含一个连接到寄存器映射的表单在我的控制器中,将用户对象添加到模型中。然后,寄存器方法读取该对象,进行验证,如果一切正常,则写入数据库。如果不好,它会返回到createmembers页面,否则返回registerSuccess页面。就像我说的那样,我确实使用@ Valid annotation和JDBC进行验证,但是在Spring和Hibernate验证的情况下,我需要修复它。
答案 0 :(得分:0)
修正了它。我必须明确地将用户注释为模型属性。这是新方法签名。
public String doRegister(Model model, @Validated(FormValidationGroup.class) @ModelAttribute("member") User member, BindingResult result)