简体   繁体   English

Spring MVC:多行表单提交

[英]Spring MVC: Multiple Row Form Submit

I had a requirement where using Spring MVC we had to take inputs multiple rows of data from user. 我有一个要求,在使用Spring MVC的地方,我们必须从用户那里获取多行数据的输入。 The form had many rows which user can edit and submit My code look like this.When I tried to display the area list it is null What will be the reason 表单上有很多行,用户可以编辑和提交我的代码,如下所示。当我尝试显示区域列表时,它为null。这是什么原因

My view is 我的看法是

 <form  data-th-action="@{/qprmonitorlottery}"  method="POST" >
        <section>
          <div class="panel panel-default" style="margin-top: 15px;"> 
            <!-- Default panel contents -->
            <div class="panel-heading">エリア間補完の有無</div>
            <div class="panel-body">

              <div class="table-responsive">
                <table class="table table-bordered">
                  <tr>
                    <th width="13%" style="background: #FFFFFF; border-top: 1px solid #ffffff; border-left: 1px solid #ffffff;" >&nbsp;</th>
                    <th colspan="3" style="text-align: center">都市規模</th>
                  </tr>
                  <tr>
                    <th>エリア </th>
                    <th width="28%">大都市 </th>
                    <th width="29%">中都市</th>
                    <th width="30%">郊外 </th>

                  </tr>

                  <tr data-th-each="qprLottery,stat : ${qprLottery}">
                    <td data-th-text = "${qprLottery.areaName}">北海道 </td>
                    <td> <span style="display: inline-block; width: 80%">
                        <input type="number" data-th-value = "${qprLottery.rate1}" class="form-control" data-th-name="firstRate +${stat.index}" required >
                        <span class="help-block with-errors"></span>
                      </span> <span style="position: relative; top:-10px;" >%</span>  </td>
                    <td>  <span style="display: inline-block; width: 80%">
                        <input type="number" data-th-value = "${qprLottery.rate2}" class="form-control" data-th-name="secondRate +${stat.index}"   required >
                      </span> <span style="position: relative; top:-10px;" >%</span>  </td>
                    <td>  <span style="display: inline-block; width: 80%">
                        <input type="number" data-th-value = "${qprLottery.rate3}"  class="form-control" data-th-name="thirdRate +${stat.index}"  required >
                      </span> <span style="position: relative; top:-10px;" >%</span>  </td>

                  </tr>


                </table>
              </div>

            </div>
          </div>
        </section>

        <div class="row">
          <div align="center" style="padding: 30 0px;">
            <button type="submit"  class="btn btn-primary" value="実行">実行
            </button>
          </div>
        </div>
      </form>

Controller is 控制器是

     @RequestMapping(value = "/qprmonitorlottery", method = RequestMethod.POST)
      public String setQprMonitorLottery(@Valid @ModelAttribute() MasterCitySizeAreaForm form, BindingResult result) {

        CitySizeByAreaView a = new CitySizeByAreaView();
        System.out.println("Area list" + a.getRate1());
        System.out.println("Area list-------------" + form.getAreaList());

        if (result.hasErrors()) {
          return "new-monitor-lottery";
        }

        return "new-monitor-lottery";
      }

form classes are 表单类是

public class MasterCitySizeAreaForm {

    private ArrayList<CitySizeAreaForm> areaList;

    public ArrayList<CitySizeAreaForm> getAreaList() {
        return areaList;
    }

    public void setAreaList(ArrayList<CitySizeAreaForm> areaList) {
        this.areaList = areaList;
    }      
}

form class 2 表格类2

  public class CitySizeAreaForm {

      private int areaId;
      private Integer area;
      private Integer citySize;
      private String areaName;
      private String citySizeName;
      private Float rate1;
      private Float rate2;
      private Float rate3;

      public CitySizeAreaForm() {
      }

      public CitySizeAreaForm(Integer area, String areaName, Float rate1, Float rate2, Float rate3) {
        this.area = area;
        this.areaName = areaName;
        this.rate1 = rate1;
        this.rate2 = rate2;
        this.rate3 = rate3;
      }
    }

replace: 更换:

<tr data-th-each="qprLottery,stat : ${qprLottery}"> 

with

<tr data-th-each="qprLottery,stat : ${qprLottery.areaList}">

This link helped me!!!Do try it!! 这个链接对我有帮助!!!

http://jsltech.blogspot.com/2013/12/how-add-multiple-objects-into-database.html http://jsltech.blogspot.com/2013/12/how-add-multiple-objects-into-database.html

can use this format for displaying:- 可以使用这种格式显示:

<c:forEach items="${contactForm.contacts}" var="contact" varStatus="status">
        <tr>
            <td>${contact.firstname}</td>
            <td>${contact.lastname}</td>
            <td>${contact.email}</td>
            <td>${contact.phone}</td>
        </tr>
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM