Announcement Announcement Module
Collapse
No announcement yet.
@ModelAttribute??? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ModelAttribute???

    I have running example with @ModelAttribute in method param as follows
    Code:
    @Controller  
    @RequestMapping("login")  
    public class LoginControllerTest {  
        //------------------------------ Working fine -----------------------------------  
        @RequestMapping(value = "dologin.htm", method = RequestMethod.GET)  
        public String getLoginpage(@ModelAttribute LoginForm loginForm,  
                BindingResult result) {  
            return "loginform1";  
        }  
        @RequestMapping(value = "dologin.htm", method = RequestMethod.POST)  
        public String doLoginpage(@Valid @ModelAttribute LoginForm loginForm,  
                BindingResult result, ModelMap model) {  
            if(result.hasErrors()){  
                return getLoginpage(loginForm, result);  
            }  
            model.addAttribute("message", loginForm.getUserName() + "-" + loginForm.getPassword());  
            return "helloworld";  
        }  
    }
    I have just created one more class LoginFormDto as same like LoginForm (you can say duplicate class) and add 2 more method in same controller .

    Code:
        @RequestMapping(value = "dologin1.htm", method = RequestMethod.GET)  
        public String getLoginpage1(@ModelAttribute LoginFormDto loginForm,  
                BindingResult result) {  
            return "loginform1";  
        }  
        @RequestMapping(value = "dologin1.htm", method = RequestMethod.POST)  
        public String doLoginpage1(@Valid @ModelAttribute LoginFormDto loginForm,  
                BindingResult result, ModelMap model) {  
            if(result.hasErrors()){  
                return getLoginpage1(loginForm, result);  
            }  
            model.addAttribute("message", loginForm.getUserName() + "-" + loginForm.getPassword());  
            return "helloworld";  
        }
    When I hit url for dologin1.htm (GET) i am getting following error
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'loginForm' available as request attribute

    Everything work fine for url dologin.htm

    I am not getting what exactly i am missing?

    Can anyone please point me...

  • #2
    Your second controller "GET" method getLoginpage1 is returning to the same view (loginform1) as your first method.
    You have already bound the LoginForm to that jsp.

    I assume that's a typo.

    Comment


    • #3
      Thanks,

      Now I got it why it was not working.
      Following is my understanding about the above example.
      I did not specified name for ModelAttribute ie(something like @ModelAttribute("loginForm")) . So the container assume name as a Class Name ( first character small)
      for example
      @ModelAttribute LoginForm => @ModelAttribute("loginForm")

      In my second example i used @ModelAttribute LoginFormDto so the container by default looking for
      attribute "loginFormDto" not for "loginForm"

      If I give explicitly attribute name with ModelAttribute it works without any issue.

      I dont know if this is documented somewhere.

      Comment

      Working...
      X