Announcement Announcement Module
Collapse
No announcement yet.
Add Validator to Annotation defined controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add Validator to Annotation defined controller

    I am trying to use annotation and have a question about validating my form,
    I have defined
    Code:
    <annotation-driven />
    in my xml file
    and my controller is as below, if i want to validate my form so that name cannot be blank

    My Jsp page
    Code:
    <f:form method="post" action="displayusermessage" commandName="usermessage">
    </f:form>
    UserMessage bean
    Code:
    public class UserMessage {
    	    
    	    private String name;
    	    private String message;
    	 
    	    public String getName() {
    	        return name;
    	    }
    	    public void setName(String name) {
    	        this.name = name;
    	    }
    	    public String getMessage() {
    	        return message;
    	    }
    	    public void setMessage(String message) {
    	        this.message = message;
    	    }
    	}
    Code:
    @Controller
    public class HomeController {
    @RequestMapping(value = "/displayusermessage", method = RequestMethod.POST)
        public String display(@ModelAttribute("usermessage") UserMessage usermessage, Model model) {
           //validate usermessage, if name is blank then send back to same page, or else send to success page
    //how do i add validation here
           
            model.addAttribute("usermessage", usermessage);
            return "success"
        }
    }

  • #2
    Autowire Validator ...

    Ash,

    I haven't tried this recommended solution so it may be incorrect... However, this is where I'd start if I were in your shoes. This recommended solution started HERE

    You UserMessage Beans:
    Code:
      
    import org.springmodules.validation.bean.conf.loader.annotation.handler.Length;  
    import org.springmodules.validation.bean.conf.loader.annotation.handler.NotBlank;
    
    public final class UserMessage {  
          
        @NotBlank  
        @Length(max = 80)  
        private String name; 
    
        public String getName() {  
            return name;  
        }  
    
        public void setName(String name) {  
            this.name = name;  
        }
    }
    Your Controller:
    Code:
    @Controller  
    public final class HomeController {
        @Autowired  
        private Validator validator;  
    
        @RequestMapping(value = "/displayusermessage", method = RequestMethod.POST)      
        public String post(@ModelAttribute("userMessage") UserMessage userMsg, BindingResult result) {  
            validator.validate(userMsg, result);
    
            if (result.hasErrors()) { return "form"; }
    
            // Use the redirect-after-post pattern to reduce double-submits.  
            return "redirect:thanks";
        }
    }
    Good luck!

    Comment

    Working...
    X