Controller
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, ModelMap mmap)
{
List<Test> testList= testService.getById(id);
mmap.put("testList", testList);
return prefix + "/edit";
}
edit.html
[[${testList}]]可取出从controller传过来的testList对象,该功能实现了对查询到的testList集合里的所有Test对象的value值的累加,并把最后累加值赋予input。
......
<input name="totalValue" value="0" style=" border-width:0;color:red;" class="form-control" type="text" maxlength="30" readonly="readonly">
......
function sumAmount(){
var totalAmount = 0;
for(var i = 0; i < [[${testList}]].length; i++){
var result = [[${testList}]][i];
totalAmount = accAdd(totalAmount, result.value);
}
$("input[name='totalValue']").val(totalAmount);
}
......
这篇博客展示了如何在SpringBoot中通过Controller处理GET请求,获取特定ID的数据列表,并在前端页面上实现数据的累加计算。利用ModelMap传递数据到编辑模板edit.html,JavaScript函数sumAmount计算Test对象集合的value总和,将结果显示在只读的input字段中。
2097

被折叠的 条评论
为什么被折叠?



