在我的 Spring MVC 应用程序中,我有 2 个页面。
我有一个包含 10 个变量的对象。
在第一页中,我设置了上述对象的 9 个变量(第一页有 9 个字段)。
我在 session 中设置了这个对象,在 Controller 中设置了模型属性。
我还需要将同一个对象传递到下一页。
在第二页中,我需要在同一对象中设置第 10 个变量(第二页只有一个字段第 10 个字段)。
(为什么我需要第二页来设置第 10 个变量是 - 在第二页中,我根据第一页中的条目填充下拉列表)
当我在第二页提交表单时 - 我需要提交具有 10 个变量的相同对象。 (由于公司政策,我无法粘贴代码) 谁能帮我解决这个问题?
最佳答案
请注意,这两种方法都在同一个 Controller 中。你需要做这样的事情:
@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";
}
}
关于java - Spring MVC - 将相同的对象从一个页面传输到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19674229/