reset password
Author Message
hhuang30
Posts: 40
Posted 14:53 Apr 29, 2014 |

When the registeration page is loaded, it throws me an excepiton:

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

 

However,  the "userhaha" in setup in the controller side

models.put( "userhaha", new User() );

And, the model is also set in jsp viewer side

<form:form modelAttribute="userhaha">

 

What could be the reason?

Thanks in advance

Last edited by hhuang30 at 15:01 Apr 29, 2014.
cysun
Posts: 2935
Posted 14:58 Apr 29, 2014 |

You need to pass a model object "userhaha" in your controller method that handles the GET request. We'll discuss form processing and model attributes in the class tomorrow.

hhuang30
Posts: 40
Posted 15:03 Apr 29, 2014 |

Sorry, it is a wrong typing, a bean object "userhaha" was already set in the controller side there

models.put( "userhaha", new User() );

cysun
Posts: 2935
Posted 15:14 Apr 29, 2014 |

In that case you shouldn't have any problem displaying the form, but you need to specify the model attribute name in the controller method that handles the form submission, e.g.:

@RequestMapping(value="/user/register", method=RequestMethod.POST)
String register( @ModelAttribute("userhaha") User userhaha, ...)

The reason is that the default name for model attribute is based on the type rather than the name of the method parameter. If you just have @ModelAttribute User userhaha, Spring will look for a model attribute called user rather than userhaha.

hhuang30
Posts: 40
Posted 15:20 Apr 29, 2014 |

Right, display was good but submission died.

Now everything works great after  @ModelAttribute("userhaha") is added in the RequestMethod.POST method

Thank you!