【问题标题】:Dynamic form with multiple objects submission in Spring?Spring中具有多个对象提交的动态表单?
【发布时间】:2014-12-20 18:48:20
【问题描述】:

我有一个对象 CreateProjectFormModel 如下(我使用的是 Spring 4)。

public class CreateProjectFormModel {
    private Project project;
    private List<DUser> users;

    public CreateProjectFormModel() {
        this.project = new Project();
        this.users = new ArrayList<DUser>();
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public List<DUser> getUsers() {
        return users;
    }

    public void setUsers(List<DUser> users) {
        this.users = users;
    }
}

我不知道如何创建控制器和相应的表单,以便可以一次提交多个DUser - 如果对象不包含集合,可以这样做吗?

阅读this,但不知道如何提前将用户添加到项目中,因此无法固定用户大小。

我阅读了thymeleaf tutorial,但很想知道是否可以不使用百里香。

谢谢。

【问题讨论】:

    标签: forms spring-mvc spring-form


    【解决方案1】:

    您在问题List<Foo> as form backing object using spring 3 mvc, correct syntax? 中发布的链接应该为您提供解决方案,cmets 中讨论了什么

    我假设此解决方案需要固定数量的输入 字段,对吗?如果你有一个动态的输入数量怎么办 字段?

    不关心用户的数量,它不必固定,而是关心对象的属性不同的事实,我不认为这是你的情况。所以,如果你的 DUser 有一个属性 userName,例如您的 项目 有一个属性 name。您的控制器方法可能只是,

    @RequestMapping(value = "/test", method=RequestMethod.POST)
    public String processSubmit(CreateProjectFormModel createProjectFormModel) {
           ...
    }
    

    和你的表格

    <form:form action="/form/test" method="post">
        <div class="single">
            <input type="text" name="project.name"/>
            <input type="text" name="users[0].userName"/>
            <a href="#" onclick="addNewUserInputSection();return false">add another user</a>
            <input type="submit" value="Save">
        </div>
    </form:form>
    

    您需要付出一些努力的地方是创建一个 javascript 函数 addNewUserInputSection,该函数将为具有递增索引的 users 属性添加一组新的输入字段,例如

    <form:form action="/form/test" method="post">
        <div class="single">
            <input type="text" name="project.name"/>
            <input type="text" name="users[0].userName"/>
            <input type="text" name="users[1].userName"/>
            <a href="#" onclick="addNewUserInputSection();return false">add another user</a>
            <input type="submit" value="Save">
        </div>
    </form:form>
    

    这些示例是基本的,但应该足以让您解决问题

    【讨论】:

      【解决方案2】:

      虽然上述答案有效,但这里有一个替代方案,不需要您创建包装类/表单类。

      模型和控制器

      public class Foo {
          private String name;
          private List<Foo> fooList;
          public Foo() {
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
          public String getFooList() {
              return fooList;
          }
      
          public void setFooList(String fooList) {
              this.fooList = fooList;
          }
      
      }
      
      @Controller("/")
      public class FooController{
      
          //returns the ModelAttribute fooListWrapper with the view fooForm
          @RequestMapping(value = "/FOO", method = RequestMethod.GET)
          public String getFooForm(Model model) {
              List<Foo> fooList = service.getFooList();
      
              model.addAttribute("fooList", fooList);
      
              return "list_foo"; //name of the view
          }
      
          @RequestMapping(value = "/FOO", method = RequestMethod.POST)
          public String postFooList(@ModelAttribute("foo")Foo foo, Model model) {
              List<Foo> list = foo.getFooList(); // **This is your desired object. 
              //If you debug this code, you can easily find this is the list of  
              //all the foo objects that you wanted, provided you pass them properly. 
              //Check the jsp file to see one of the ways of passing such a list of objects**
              //Rest of the code
          }
      
      }
      

      JSP 视图

      <form:form id="form" action="<paste-target-url-here>" method="POST" modelAttribute="fooList">
      
      
          <c:forEach items="${fooList}" varStatus="i">
                 <form:input path="fooList[${i.index}].name" type="text"/>
                 <!-- Here you are setting the data in the appropriate index which will be caught in the controller -->
          </c:forEach>
      
      
          <button>submit</button>
      </form:form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-05
        相关资源
        最近更新 更多