Reputation: 771
In my Spring MVC application, I have 2 pages.
I have an object with 10 variables in it.
In the first page, I am setting 9 variables of the above object(first page has 9 fields).
I set this object in session as well as model attribute in the controller.
I also need to pass this same object to the next page.
In the second page, I need to set the 10th variable in the same object(second page has only one field 10th field).
(why I need a second page for setting the 10th variable is - In the second page, I populate a dropdown based on the entries in the first page)
When I submit the form in the second page - I need to submit the same object with 10 variables. (I couldn't paste the code because of my company policy) Can anyone please help me in fixing this?
Upvotes: 3
Views: 4464
Reputation: 79
If you have considered all other options( I mean model attribute etc) , I would suggest using Spring-Web-Flow to do this (if the two pages you are talking about were related), this way you can ensure that data will be transferred from one page to another.
Ref: http://docs.spring.io/spring-webflow/docs/1.0.3/reference/practical.html
Hope this works.
Regards Ashok Gudise
Upvotes: 0
Reputation: 130
If you think you will have of these wizard type screens, you should consider using spring-webflow. Spring-Webflow's DSL will help you manage more complex scenarios if they arise.
You can get a basic introduction about Spring-Webflow from here.
Upvotes: 0
Reputation: 106
This can be done in this way:
@Controller
public class PageController {
@RequestMapping("/page1")
public String showPage1(Model model) {
Obj obj = new Obj();
model.addAttribute("obj", obj);
return "test/page1";
}
@RequestMapping("/page2")
public String showPage2(@ModelAttribute Obj obj, Model model) {
model.addAttribute("obj", obj);
return "test/page2";
}
@RequestMapping("/page3")
public String showPage3(@ModelAttribute Obj obj, Model model) {
model.addAttribute("obj", obj);
return "test/page3";
}
}
Page 1 (relevant part only):
<sf:form modelAttribute="obj" action="page2">
<sf:input path="field1"/><br/>
<sf:input path="field2"/><br/>
<sf:input path="field3"/><br/>
<input type="submit" value="To page 2">
</sf:form>
Page 2:
<sf:form modelAttribute="obj" action="page3">
<sf:hidden path="field1"/>
<sf:hidden path="field2"/>
<sf:hidden path="field3"/>
<sf:input path="field4"/><br/>
<sf:input path="field5"/><br/>
<input type="submit" value="To page 3">
</sf:form>
Page 3 serves only to display all object fields which were entered on previous 2 pages.
Upvotes: 0
Reputation: 1584
Note that both methods are in the same controller. You need to do something like this:
@Controller
@SessionAttributes("myObject")
public class SessionAttributesController {
// Save session attribute in model.
@RequestMapping(value = "/page1.html", method = RequestMethod.GET)
public String page1( @ModelAttribute MyClass myObject, ModelMap model ) {
model.addtAttribute("myObject", myObject);
return "page1.html";
}
// Now you model is having myObject, so it has to be used in jsp. You can add ModelMap parameter to this method and check what contains model in debug mode.
@RequestMapping(value = "/page2.html", method = RequestMethod.GET)
public String page(ModelMap model) {
model.get("myObject").setAttr10(value);
return "page2.html";
}
}
Upvotes: 1