java - 如何在更新jsp页面中的列表时将两个不同的模型属性映射到jsp中的一个表?
问题描述
我想在 jsp 页面中添加两个模型。
我正在使用spring mvc。我制作了一个控制器,在编写查询后我创建了一个arrayList并将其添加为第一个modelAttribute,另一个查询正在获取一个列,我将它保存在另一个模型属性中..所以,我想要更新我的表...即。单击更新按钮后,该方法应该运行并将先前输入的值设置为 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);
}
推荐阅读
- php - Block.json 返回错误的路径
- android - 在 FLUTTER 中应用程序长时间不活动后,firebase_messaging 无法在 android 10 e 中运行
- bash - 在 bash 中处理短选项和长选项的两种方法
- java - 尝试访问作为依赖项添加到项目的库中的文件时出现 FileNotFound 异常
- c# - 如何更改画布中图像的颜色
- bash - WSL2 互操作问题导致 shell 脚本中的读取循环过早退出
- javascript - 选择选项正在更新状态,但不会将当前状态的正确名称呈现为选项
- wordpress - 如何将网站从子域转移到主域
- java - 空白敏感属性密钥需要 Nifi 迁移
- javascript - 如何使用 java 脚本设置“--webkit-backdrop-filter”?