首页 > 解决方案 > 如何在更新jsp页面中的列表时将两个不同的模型属性映射到jsp中的一个表?

问题描述

我想在 jsp 页面中添加两个模型。

我正在使用spring mvc。我制作了一个控制器,在编写查询后我创建了一个arrayList并将其添加为第一个modelAttribute,另一个查询正在获取一个列,我将它保存在另一个模型属性中..所以,我想要更新我的表...即。单击更新按钮后,该方法应该运行并将先前输入的值设置为 jsp 页面。我已经从数据库获取值到控制器。但我不知道如何在 jsp 页面上设置两个不同的 modelAttribute。

//控制器的方法

public String editDataProfile(@RequestParam("dpid") int dpid, Model model) {
    System.out.println("Inside edit dataProfile method");
    List < TestData > tData = null;
    try {

        //here we are getting the list of testData where testcase id 
        tData = testDataService.editTestData(testcaseIds, dpid);
        System.out.println("TestDataList size: " + tData.size());
        model.addAttribute("tData", tData);

        //here we are getting DataProfileName for updating purpose
        DataProfile dProfile = testDataService.editDP(dpid);
        model.addAttribute("dProfile", dProfile);

    }
    catch(Exception ex) {
        System.out.println("Exception occured while updating dataprofile: " + ex);
    }

    return "testdata";
}

//jsp页面

<div class="form-group">
   <table id="example2" class="table table-bordered table-striped table-hover" style="">
      <!-- before panel -->
      <form:form class=""  method="POST" action="insertdata" modelAttribute="dProfile" >
         <%-- <form:hidden path="dataId"/> --%>
         <form:errors path="dataProfileId"></form:errors>
         <label>TestData Name:</label><input type="text" class="form-control" placeholder="Enter testData Name"   name="testDataName" aria-required="true" autocomplete="off"></input>
         <form:errors path="testDataName" style="color:red"></form:errors>
         <br>
         <thead style=" ">
            <tr>
               <th class="" style="">#</th>
               <th class="col-md-4">TestStepDetails</th>
               <th class="col-md-2">Action</th>
               <th class="col-md-6">Data</th>
            </tr>
         </thead>
         <tbody>
            <% int i = 0; %>
            <c:forEach var="TestDataDetails" items="${TestCaseDetails}">
               <tr>
                  <td><%= ++i%></td>
                  <td class=" col-md-4"> ${TestDataDetails.testStepName }</td>
                  <td class=" col-md-2">${ TestDataDetails.actions.actionName} </td>
                  <td class=" col-md-6"><input class="form-control "
                     id="data" autocomplete="off" placeholder="Enter Data "
                     name="data"  />
               </tr>
            </c:forEach>
         </tbody>
         <br>
</div>
<button type="submit" class="btn btn-primary  btn-sm pull-right" style="position: relative; margin-top:-30px">Submit
Test Data</button>
</form:form>
</table>

请帮忙

标签: javaspringhibernatejspmodelattribute

解决方案


我认为您不能使用 ModelAttribute 并使用 Spring 表单绑定多个模型。一种方法是您可以跳过 spring 表单并使用普通的 HTML5 表单来做同样的事情。

<form method="POST" action="insertdata">
 //Use the variables used in ModelMap in the controller to iterate over and 
 //print
</form>

在 Controller 中,使用 ModelMap 来映射您需要的结果。就像是

public String editDataProfile(@RequestParam("dpid") int dpid, ModelMap map) {

    map.addAttribute("tData", tData);
    map.addAttribute("dProfile", dProfile);
}

推荐阅读