javascript - 如何在 JavaScript 中构建 Spring 表单
问题描述
我有一个.jsp
视图页面,它在加载时动态构建一个表。下表显示了一个片段:
根据参数是否为非空,每行末尾personFilename
有或没有按钮。Download
单击此按钮会将行中的值发送到 Java Spring 控制器方法,该方法会在浏览器中下载与该人对应的文件。
在同一视图页面上还有两个输入文本字段start
和end
年龄以及一个按钮。Filter
单击此按钮必须删除整个表并动态构建另一个表,其中我们只包含Age
所需范围内的行。
我正在努力的是在 JavaScript 中创建这个新表。我不知道如何将弹簧<form:form...>...</form:form>
作为innerHTML
行的单元格。这是必不可少的,因为我希望从按钮单击到控制器的映射能够像在.jsp
视图页面的初始加载上构建的原始表单一样工作。
我知道弹簧表单不能用 JavaScript 构建,那么有没有替代方法可以帮助我创建带有映射到 Java 控制器方法的按钮的表?
这是相关.js
代码,后面是.jsp
文件中的相关部分。
function deleteWholeTable() {
var table = document.getElementById("dataTable");
var rows = table.rows;
var i = rows.length;
while (--i) {
table.deleteRow(i);
}
}
function appendRowToTable(name, age, personFilename) {
var table = document.getElementById("dataTable");
// get the current length of the table
var rowsOfTable = table.rows.length;
// insert a new row at the end
var row = table.insertRow(rowsOfTable);
// put name and age in cells 1 and 2
var cell1 = row.insertCell(0); cell1.innerHTML = name;
var cell2 = row.insertCell(1); cell2.innerHTML = age;
if (personFilename){
var cell8 = row.insertCell(7); cell8.innerHTML =
// WHAT TO WRITE HERE? CAN'T MAKE A SPRING FORM AS DONE IN JSP FILE
// NEED TO INSERT A FORM WITH BUTTON THAT SENDS DATA TO /downloadPersonFile WHEN IT IS CLICKED (just like in jsp file)
}
else{
var cell8 = row.insertCell(7); cell8.innerHTML = '';
}
}
<!DOCTYPE html>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%--other irrelevant stuff here--%>
<div class="form-group col-md-2">
<input type="text" id="ageStart" placeholder="Start Age..">
</div>
<div class="form-group col-md-2">
<input type="text" id="ageEnd" placeholder="End Age..">
</div>
<div class="form-group col-md-2">
<button class="btn btn-primary" id="filterData">Filter</button>
</div>
<%--this is the outer form--%>
<form:form id = "outerForm" enctype="multipart/form-data" modelAttribute="detailList" >
<div id="tableDiv" >
<table id = "dataTable" class="table table-striped table-bordered table-sm " >
<tr>
<th>Name</th>
<th>Age</th>
<th>Download File</th>
</tr>
<c:forEach items="${detailList.personDetailList}" var="personDetail" varStatus="tagStatus">
<tr>
<td>${personDetail.name}</td>
<td>${personDetail.age}</td>
<td>
<%--this is the spring form that I care about and want to recreate in JS--%>
<form:form method = "POST" action = "/downloadPersonFile">
<c:choose>
<c:when test="${empty (personDetail.personFilename)}">
</c:when>
<c:otherwise>
<%--when the personFilename is non-empty, add a download button--%>
<button type="submit" class="btn btn-success btn-lg" name="personDetail" value="${personDetail.name},${personDetail.age},${personDetail.personFilename}">Download</button>
</c:otherwise>
</c:choose>
</form:form>
</td>
</tr>
</c:forEach>
</table>
</div>
</form:form>
<script>
$('#filterData').on('click', function (e) {
// first we delete the table built above and then rebuilt it only using the rows where age is in desired range
deleteWholeTable();
var startAge = $('#ageStart').val();
var endAge = $('#ageEnd').val();
<c:forEach items="${detailList.personDetailList}" var="personDetail" varStatus="tagStatus">
var personAge = "${personDetail.age}";
// convert personAge, startAge, endAge to integer
if (personAge >= startAge && personAge <= endAge ){
appendRowToTable("${personDetail.name}", "${personDetail.age}","${personDetail.personFilename}");
}
</c:forEach>
});
</script>
这是Java控制器:
@RequestMapping(value = "/downloadPersonFile", method = RequestMethod.POST, params={"personDetail"})
public ModelAndView getAndDownloadPersonFile(@RequestParam("personDetail") String personDetail, HttpServletResponse response, HttpServletRequest request) throws Exception {
String personAttr[] = personDetail.split(",");
// the following function fetches the file from database and downloads it in browser
fetchAndDownload(personAttr[0], personAttr[1], personAttr[2], response);
return null;
}
一般来说,我是 JavaScript 和 Spring MVC 框架的新手。任何帮助表示赞赏。谢谢。
解决方案
推荐阅读
- netlogo - 两个不相关的条件 - 预期的命令错误
- php - Laravel 将数组合并为一个数组
- c - 为非阻塞 connect() 编写代码的理想方法是什么?
- ios - 如何在 SwiftUI 中创建网格视图?
- android - 符号层和圆形层崩溃的Android Studio Mapbox层问题
- angular - 如何在角度路线之间传递一些额外的动态数据?
- beego - 找到模块 github.com/ataxie/beego@latest (v1.12.3),但不包含包 github.com/ataxie/beego/client/orm
- python - 以优化模式运行诗歌安装的 python 脚本
- javascript - html5 音频:区分 NotAllowedError 和 NotSupportedError
- plot - 根据旧的 x 范围调整散景中的 extra_x_range