我想在 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/