簡體   English   中英

Spring MVC:多行表單提交

[英]Spring MVC: Multiple Row Form Submit

我有一個要求,在使用Spring MVC的地方,我們必須從用戶那里獲取多行數據的輸入。 表單上有很多行,用戶可以編輯和提交我的代碼,如下所示。當我嘗試顯示區域列表時,它為null。這是什么原因

我的看法是

 <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>

控制器是

     @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";
      }

表單類是

public class MasterCitySizeAreaForm {

    private ArrayList<CitySizeAreaForm> areaList;

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

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

表格類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;
      }
    }

更換:

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

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

這個鏈接對我有幫助!!!

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

可以使用這種格式顯示:

<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