使用多个 modelAttributes 从 JSP 接收 2 个对象

using multiple modelAttributes to receive 2 objects from JSP

我正在开发一个 Spring-MVC 应用程序,其中有两个部分。它基本上是一个具有 2 种模式的笔记应用程序,一种是 groupMode,另一种是 personalMode。现在他们都有不同的 dao,后端有 serviceImpl,但他们在一个控制器视图中。

我有一个布尔值要设置,要知道用户当前处于哪种模式,在特定数据库中执行CRUD操作table。现在,由于两种组模式和个人模式都在同一个视图中,我必须制定我的方法,以便它们可以接受来自任一模式的对象。即使我在模型属性中声明 2,spring 是否支持只接受一个对象。这是我想要实现的示例:

  @RequestMapping(value = "/section/add", method = RequestMethod.POST)
    public
    @ResponseBody
    boolean addSection(@ModelAttribute("section") Section section, @ModelAttribute("groupsection") GroupSection groupSection,Model model) {
       if(boolean == true){
       this.groupSectionService.addGroupSection(groupSection);
       model.addAttribute("groupsection", new GroupSection());
     } else{
       this.sectionService.addSection(section);
        model.addAttribute("section", new Section());
       }
        return true;
    }

这可能吗,我总是从前端发送一个对象。非常感谢。欢迎任何指点或建议。

只要有这样的 if 声明 "split" 完整的控制器方法,就像你的那样,我觉得应该用两种方法代替一种控制器方法,一种用于每种情况.

最简单、最直接的解决方案是使用两个不同的 URL。

但也许您出于某些原因使用相同的 URL,那么我会在 [=13] 中使用两个具有相同 URL 但不同 params 属性的不同控制器方法=]

@RequestMapping(value = "/section/add",
                method = RequestMethod.POST
                params="createGroupSection=false")
@ResponseBody
public boolean addSection(@ModelAttribute("section") Section section) {...}

@RequestMapping(value = "/section/add",
                method = RequestMethod.POST
                params="createGroupSection=true")
@ResponseBody
public boolean addGroupSection(@ModelAttribute("section") Section section) {...}