开发者

Spring MVC Annotation Based Controller command object losing values on POST

I am loading a user object my calling a service and then store this user as a command object in the model on GET in the controller. This user object has many properties that are not mapped in the jsp page. After submitting the form, I am getting the command object i the controller on POST. But strangely, I only see the properties in the command object which are mapped to the jsp page. All the other properties those were there when I load the object are lost. I need all the properties in object to be able to successfully save it in hte database.

Can anybody help me figure this problem? Thanks!

Update

I am adding some code to better understand it. In POST handler, I was expecting the command object to have all the properties that was loaded in GET handler in addition to the properties that are bound with jsp. Instead I am losing all propeties except those are bound to the jsp. Am I doing something wrong here?

@RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception {
        UserBean user = Util.getUser(session);
        UserBean command = (UserBean)userProfileService.loadByUserName(user.getUserName());
        model.addAttribute("command", command);
        return formView;
    }

@RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("command") UserBean command, BindingResult result, HttpSession session) throws Exception {
     UserBean user = (UserBean) command;
         userProfileService.saveUser(user);
         return "successView";
}

Update

I am adding some code to better understand it. In POST handler, I was expecting the command object to have all the properties t开发者_开发百科hat was loaded in GET handler in addition to the properties that are bound with jsp. Instead I am losing all propeties except those are bound to the jsp. Am I doing something wrong here?

@RequestMapping(method = RequestMethod.GET) public String showForm(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception { UserBean user = Util.getUser(session); UserBean command = (UserBean)userProfileService.loadByUserName(user.getUserName()); model.addAttribute("command", command); return formView; }

@RequestMapping(method = RequestMethod.POST) public String onSubmit(@ModelAttribute("command") UserBean command, BindingResult result, HttpSession session) throws Exception { UserBean user = (UserBean) command; userProfileService.saveUser(user); return "successView"; }

Update

If I store the command object in session how would the jsp bind the propeties. I thought I needed to store it in model for that? Could you explain please.

Update

storing the command object in session solves the problem. I was able to store it by using

@SessionAttributes ("command")

Thanks a lot!


That's expected behaviour. Spring does not take your existing object (how would it get it?) - it creates a new one and fills it with data.

You can use the @ModelAttribute annotated-method to specify a method which will load the existing object from storage (db?) by its ID (submitted).

@ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.

See 15.3.2.8 of the MVC docs


The Spring-MVC Model values become Request scope attributes in your JSP. This is a one way translation, Spring-MVC does not restore the Model values when the user POSTs a form from your page.

When I need to store information between a GET and a POST (that is, set something on a GET and read it back on a POST), I set a Session attribute.

In your case, I believe that you will need to do one of the following:

  1. Call Util.getUser(session); in the onSubmit method.
  2. Store the command object in the session in the showForm and retrieve it in the onSubmit method>


@ModelAttribute is used to direclty set the values in the Student object from the jsp, other wise in the servlet you have to get the properties using request.getattribute() and than call student setter method. so u can use both the keywords in jsp page.

<form action="grade" method="get" name="contact1" modelAttribute="contact1">
               </form>

or

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜