Spring MVC在控制器之间传递相同的对象 [英] Spring MVC pass same object between controller

查看:100
本文介绍了Spring MVC在控制器之间传递相同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring MVC中,如何在两个控制器方法之间传递对象?我有一个更新表格和一个updateController.在控制器中,我有2种方法,一种用于获取数据并将其显示在视图中.当用户单击具有更改的更新按钮时,将调用控制器的第二种方法.我观察到的是,我在控制器的第二种方法中获得的对象与我在第一个控制器方法调用中传递给视图的对象不同.它是一个新对象,所有表单字段都映射到它.如何确保将相同的对象传递给第一个控制器方法提供给视图的第二个控制器方法?

In Spring MVC how do I pass an object between two controller methods? I have an update form and an updateController. In the controller I have 2 methods, one for fetching the data and displaying it in a view. The second method of the controller is invoked when the user clicks update button with modified changes. What I am observing is that the object which I get in second method of the controller is not the same object which I passed to the view in the first controller method call. Its a new object altogether with all form fields mapped to it. How do I make sure that the same object is passed to the second controller method which was provided to the view by the first controller method?

@RequestMapping(value = "/showEmpDetail.html", method = RequestMethod.GET)
public String showEmpDetails(
        @RequestParam(value = "page", required = false) Integer page,
        HttpServletRequest request, @RequestParam("empId") Long empId,
        ModelMap model) {
    // Get employee using empId from DB
    model.addAttribute("emp",emp);
    return "showEmpDetail";
    }

上述控制器方法从Db获取emp值并将其正确显示在视图中.现在,用户更改了一些详细信息,然后单击提交按钮.调用以下控制器方法.

The above controller method gets the emp values from Db and displays it correctly in the view. Now the user changes some details and clicks submit button. The following controller method is called.

@RequestMapping(value = "/editEmpFormSubmission.html", method = RequestMethod.POST)
public String editEmpFormSubmission(
        @RequestParam(value = "page", required = false) Integer page,
        @ModelAttribute("emp") Employee emp, BindingResult result,
        ModelMap model, HttpServletRequest request) {
     // update changes in DB
    }

在上述控制器方法中,当我检查emp对象时,它与我在上一个控制器调用中传递的对象不同.没有表单支持但其值已更改为null的字段.我如何确保同一对象通过视图传递.我不想将对象添加为sessionAttribute,因为用户可能会修改会话中的许多员工.

In the above controller method when I check the emp object its not the same object which I passed in previous controller call. The fields which are not form backed but had values were changed to null. How can I make sure the same object is passed by view. I do not want to add the object as sessionAttribute since a user might modify many employees in a session.

推荐答案

您有3个选择

  1. 使用@SessionAttributes将对象存储在两次请求之间的会话中.
  2. 在每个请求之前使用带有@ModelAttribute注释的方法检索对象
  3. 编写您自己的代码并将其存储在会话中(类似于1,但您需要做更多的工作).
  1. Use @SessionAttributes to store the object in the session in between requests.
  2. Use a @ModelAttribute annotated method to retrieve the object before each request
  3. Write your own code and store it in the session (similair to 1 but more work on your part).

选项1

  1. @SessionAttributes批注添加到您的控制器类中
  2. 完成对象后,将SessionStatus作为参数添加到更新方法和setComplete()方法中
  1. Add the @SessionAttributes annotation to your controller class
  2. Add the SessionStatus as a parameter to your update method and the setComplete() method when you are finished with the object


@SessionAttributes("emp")
public class EmployeeController {
@RequestMapping(value = "/editEmpFormSubmission.html", method = RequestMethod.POST)
public String editEmpFormSubmission(
    @RequestParam(value = "page", required = false) Integer page,
    @ModelAttribute("emp") Employee emp, BindingResult result,
    ModelMap model, HttpServletRequest request
    SessionStatus status) {
 // update changes in DB
 status.setComplete();
}    
} 

选项2

  1. 添加从数据库中检索对象并用@ModelAttribute
  2. 进行注释的方法
  3. 清理您的showEmpDetails方法,因为它只能返回视图名称
  1. Add the method which retrieves the object from the database and annotate it with @ModelAttribute
  2. Cleanup your showEmpDetails method as it should only return a view name


public class EmployeeController {
    
    @ModelAttribute("emp")
    public Employee getEmployee(@RequestParam("empdId") Long id) {
        // Get employee using empId from DB
        return  emp;
    }
    
    @RequestMapping(value = "/showEmpDetail.html", method = RequestMethod.GET)
    public String showEmpDetails() {) {
        return "showEmpDetail";
    }
}

选项3

  1. 在您的方法中添加HttpSession作为参数
  2. 在您的showDetails方法中,将其添加到模型旁边,然后将其添加到会话中
  3. 在您的editEmpFormSubmission中,使用会话中的一个并将所有非空字段复制到会话中的对象并将其存储在数据库中.
  1. In your methods add the HttpSession as an argument
  2. In your showDetails method next to adding it to the model add it to the session
  3. In your editEmpFormSubmission use the one from the session and copy all non-null fields to the object from the session and store that in the database.

我不会选择选项,我强烈建议选项1特别是在SessionStatus对象上包括setComplete()进行清理.您还可以结合使用1和2(具有@ModelAttribute注释的方法,但仍使用@SessionAttributes.).

I wouldn't go for option, I strongly would suggest option 1 especially including the setComplete() on the SessionStatus object for cleanup. You could also combine 1 and 2 (have a @ModelAttribute annotated method and still use @SessionAttributes.).

这篇关于Spring MVC在控制器之间传递相同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆