[英]Spring MVC : How to pass model object to controller method with out using form
I have a controller class where i am adding an object to model, On view i can access it, and now i want to send this object back to a new controller method, Is it possible to do it with out using form? 我有一个控制器类,我在其中添加一个对象模型,在视图上我可以访问它,现在我想将此对象发送回新的控制器方法,是否可以使用表单来完成它? and example code is:
示例代码是:
Here i am adding 'details' to model. 在这里,我将“细节”添加到模型中。
@RequestMapping(...)
public ModelAndView method1() {
.....
mv.addObject("details", details);
mv.setViewName(REVIEW_PAGE);
return mv;
}
I have an "Ok" button on review page where details are reviewed. 我在评论页面上有一个“确定”按钮,其中详细信息已经过审核。 Now i want to send this "details" object back to a new method for submission.
现在我想将此“详细信息”对象发送回新的提交方法。 i want to access the details object in this second method of same controller class.
我想在同一个控制器类的第二个方法中访问详细信息对象。
I have tried to add this as model attribute (as you can see in following code) but i am getting null values inside details object. 我试图将其添加为模型属性(您可以在下面的代码中看到),但我在详细信息对象中获取空值。
@RequestMapping(....)
public ModelAndView method2(@ModelAttribute("details") Details details){
//access details object here
}
The flow is like : ( add details in model (method1) --> send to view for review --> confirm (click ok) --> send back for submission (method2)) 流程如下:(在模型中添加详细信息(method1) - >发送到视图进行审核 - >确认(单击确定) - >发送回提交(方法2))
I am new to Spring MVC so if there are mistakes in my question, i am sorry for that. 我是Spring MVC的新手,所以如果我的问题有错误,我很抱歉。
You can tell Spring to keep a copy of the model on the server side by using the @SessionAttributes
annotation on the controller 您可以通过在控制器上使用
@SessionAttributes
注释告诉Spring在服务器端保留模型的副本
@Controller
@SessionAttributes("details")
public class TheController {
}
This comes with some caveats. 这有一些警告。 The default built-in implementation is pretty basic and does not, for example, account for multi-tab browsers using the same session across tabs.
默认的内置实现非常基本,例如,不会考虑跨标签使用相同会话的多标签浏览器。 It also has no automatic cleanup.
它也没有自动清理。 You have to manually call
session.setComplete()
when you are done with the data. 完成数据后,必须手动调用
session.setComplete()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.