Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Spring Validation Group Error

  • 21-01-2014 4:02pm
    #1
    Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭


    I'm trying to use a validation group in Spring, but it doesn't seem to want to work.

    When using @Valid User member, it works fine. When I used @Validated(FormValidationGroup.class), it doesn't. This is the method signature.
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    	public String doRegister(Model model, [B]@Valid[/B] 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";
    		}
    	}
    

    Other method
    @RequestMapping("/createmembers")
    	public String createMembers(Model model) {
    		logger.info("Showing Create Members Page....");
    		model.addAttribute("member", new User());
    		return "createmembers";
    	}
    

    The part I've highlighted is all that changes.

    The relevant error from the trace seems to be

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'member' available as request attribute


    The JSP page is as follows
    <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>
    

    It's only a partial page as I'm using Apache Tiles, and I've left out the script for checking the password.

    As I said, this works fine with the @Valid annotation, but since I'm switching from jdbc to hibernate, I'm losing validation. I want the password field to be between 5-15 for users, but since I am encrypting them, Hibernate won't let them pass validation when writing them to the database.

    My understanding in the user clicks on Register, this maps to the createmembers.jsp (which is actually Apache tiles using header.jsp, footer.jsp and createmembers.jsp) - this jsp contains a form which connects to the register mapping in my controller and adds the user object to the model. The register method then reads that objects, does validation and if it's all good, writes to database. If bad, it goes back to the createmembers page, otherwise registerSuccess page. Like I said, I did have Validation working with the@Valid annotation and with JDBC, but with both Spring and Hibernate validating, I need to fix it up a bit.


Comments

  • Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭chrislad


    Fixed it. Had to explicitly annotate the User object as a model attribute. Must be done implicitly by the @Valid annotation.
    public String doRegister(Model model, @Validated(FormValidationGroup.class) @ModelAttribute("member") User member, BindingResult result)
    


Advertisement