[英]How to map two different model attribute to one table in jsp while updating the list in jsp page?
我想在jsp頁面中添加兩個模型。
我正在使用spring mvc。我已經創建了一個控制器,在編寫查詢后我創建了一個arrayList並將其添加為第一個modelAttribute而另一個查詢正在獲取一個列,我將其保存在另一個模型Attribute..So,這里我想要更新我的表...即。 單擊更新按鈕后,該方法應該運行並將之前輸入的值設置為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>
請幫忙
我認為你不能使用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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.