java - 如何在更新jsp页面中的列表时将两个不同的模型属性映射到jsp中的一张表?

标签 java spring hibernate jsp modelattribute

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

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

// Controller 的方法

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>

请帮忙

最佳答案

我认为您不能使用 ModelAttribute 并使用 Spring 表单绑定(bind)多个模型。 一种方法是您可以跳过 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);
}

关于java - 如何在更新jsp页面中的列表时将两个不同的模型属性映射到jsp中的一张表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56592509/

相关文章:

java - Android 上特定情况下格式化日期和时间

java - Freemarker 模板错误

java - CsrfGuard 出现问题。尽管从官方网站进行了配置,csrf 仍抛出问题

java - 采用和返回相同类型的方法管道

java - 为什么 Hybris 中的促销引擎无法正常工作?

java - 将标识符附加到 Oracle session

java - 为什么要使用实体管理器?

java - JUnit 测试 JSON 响应时出现断言错误

reactjs - 从kafka获取数据并发送到RSocket

java - Hibernate-Criteria:在 'ON Clause' 中使用多个条件