[英]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.