Why can't I use Valid parameter along with RequestParam in Spring MVC?

前端 未结 3 1524
北海茫月
北海茫月 2020-12-16 19:06

Example:

public String getStudentResult(@RequestParam(value = \"regNo\", required = true) String regNo, ModelMap model){

How can I use @v

3条回答
  •  暖寄归人
    2020-12-16 19:35

    one way to do it is to write a Wrapper Bean like the following :

     public class RegWrapperBean{
    
            @NotNull
            String regNo ; 
    
            public String getRegNo(){
                 return regNo ;
            }
    
            public void setRegNo(String str){
                 this.regNo=str;
            }
    
    }
    

    and your handler method will be like the following :

      @RequestMapping(value="/getStudentResult", method=RequestMethod.POST)
        public String getStudentResult(@Valid @ModelAttribute RegWrapperBean bean,
                BindingResult validationResult, Model model) {
        }
    

    and please refer to these answers here and here .

    Hope that Helps .

提交回复
热议问题