简体   繁体   中英

How to map two different model attribute to one table in jsp while updating the list in jsp page?

I want to add two models into jsp page .

I am using spring mvc .I have made a controller , where after writing queries i have created a arrayList and added it as first modelAttribute and another query is fetching one column and i am saving it in another model Attribute..So , here i want to update my table...ie. after clicking update button the method should run and set the previous entered value to the jsp page.. I have got the values from database to controller.But i am not sure how to set the two different modelAttribute on jsp page.

//Controller 's method

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 Page

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

please help

I think you cannot use ModelAttribute and bind multiple models using the Spring form. One way is you can skip the spring form and use the normal HTML5 form to do the same.

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

In Controller, use ModelMap to map the results you need. Something like

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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