I have many pages with the same form where I bind my object using commandName. I try to put the object to session with name "myObject" and use it for form (commandName = "myObject"). But system throws exception (Neither BindingResult nor plain target object for bean name "myObject"). How can I bind the object to session for any controllers and requests?
That error is typical when your use a form:form
tag that targets a command object that is not available in the request.
A good approach is to combine @ModelAttribute
annotated method with @SessionAttributes
on a Controller that intially forwards to the view that contains the form, something like
@Controller
@SessionAttributes("myObject")
public class FormController {
@ModelAttribute("myObject")
public MyObject createMyObjectBean() {
return new MyObject();
}
...
}
The initally createMyObjectBean
will be called with whatever is the first request to the controller methods, but it won't be called on subsequent request as the myObject
value will come from session due to @SessionAttributes
just note that for this approach to work, you must have a controller that forwards to the view that contains your form
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.