简体   繁体   English

Spring MVC:如何使用表单将模型对象传递给控制器​​方法

[英]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.

相关问题 如何使用Spring MVC 3在控制器中从模型中获取对象? - How can I get an object out of the model in the controller with Spring MVC 3? 如何从控制器传递多个模型对象以及如何将所有作为命令对象传递给spring mvc中的form:form? - How to pass multiple model objects from controller and how to pass all as command objects into the form:form in spring mvc? Spring MVC - 如何使用控制器方法更新模型对象的属性? - Spring MVC - How do I update a model object's attributes using a controller method? 如何将HashMap对象从一个方法传递到同一个Spring MVC控制器类中的另一个方法 - How to pass HashMap object from one method to another method in same Spring MVC controller class Spring MVC / JSP-如何将对象的嵌套列表从选择表单传递给控制器 - Spring MVC / JSP - How to pass nested list of object to controller from a select form 使用以下方式将Map值传递给Spring MVC控制器<form:hidden> - Pass Map value to Spring MVC controller using <form:hidden> 如何从JSP中的循环将Spring MVC模型传递给控制器 - How to pass Spring MVC model to controller from a loop in JSP 使用Ajax将json对象传递给Spring mvc控制器 - Pass json object to Spring mvc controller using Ajax Spring MVC:如何将表单数据从JSP传递到控制器 - Spring mvc : How to pass the form data from jsp to controller Spring MVC传递模型以查看然后传递给控制器 - Spring MVC pass model to view then to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM