vamshi krishna
vamshi krishna

Reputation: 1

Spring MVC Form Object was resetting

Im using Spring MVC form to construct my jsp form and Im using Model attribute to pass data to and from JSP and Controller.

my prob- From one method in Controller class, I am constructing this complete Model object and sending it to JSP using ModelMap and I am successfully able to receive this model attribute and use it in jsp page which was sent from controller.

@RenderMapping(params ="myaction=showDetails")
public String showPartnerDetails(@ModelAttribute("reportModel") ReportSettingModel reportModel,RenderRequest req, RenderResponse res,ModelMap map){
        List<String> reportsList=portalService.getReportsList();
        reportModel.setReportDisplayName(reportsList);
        reportModel.setComments("Test");
        map.addAttribute("reportModel", reportModel);
}

Now when I do some changes in jsp page (like providing input for input fields, selecting dropdown list, etc) and when I click on Submit button, again with the help of CommandName/modelAttribute attribute I am sending the same(updated) model to another method in controller, and in Controller Im able to receive this model attribute successfully but I am loosing all the data which was present earlier (from previous method in the same controller) in the model attribute -I am still able to get the new data fields which I have done in jsp (like providing input for input fields, selecting dropdown list, etc) in the second method of controller.

sample JSP:

<form:form name="reportSetup" id="reportSetup" action="" class="form-horizontal" commandName="reportModel" method="post">
....
</form>

Second Method:

@RenderMapping(params = "myaction=getreportSetup")
    public String getReportSetup(@ModelAttribute("reportModel") ReportSettingModel reportModel, RenderRequest req,
            RenderResponse res, ModelMap map){
//here in reportModel, I am getting only data of elements which I have changed in JSP page, but not the fields data which I have calculated in the above showPartnerDetails method
       portalService.getReportFields(req,res,map,reportModel);

}

Upvotes: 0

Views: 774

Answers (1)

Miroslav Ligas
Miroslav Ligas

Reputation: 1307

The behaviour is expected. Spring does not work the way how you describe. The data that you put in the model do not get persists in a session or something.

What's happening is that when you populate the model it will be sent to JSP. JSP will use the data to render the form and populate all fields and the model is "lost". When you hit submit all the fields are collected to the server and the server will put together a model object based on the request data. What's not in the request will not be in the model.

If you want to keep some values you need to use hidden fields in the form to get them into the submit request. Of course, you are limited to the amount of data you can "save" this way. Usually, you save some IDs by this means as you don't want the user to see them but you might need them to pull some data.

Enumeration, list of values... they need to be always fetched a new. If you want to optymies this use caching on service layer.

Upvotes: 1

Related Questions