Spring MVC如何将列表中的WordDto传递至POST接口?
问题描述
我有一个Spring控制器,其中包含一个接收WordDto的POST接口;另有一个GET接口将WordDto列表存入ModelMap并渲染JSP页面。在JSP页面中,我将列表的每个条目展示在表格的一行中,每行还有一个按钮,需要调用POST接口并传递该行对应的WordDto,但我不知道如何正确地将行中的WordDto传递给POST接口。
接口代码
@GetMapping("/generate") public String generateWords(ModelMap model, @RequestParam(value = "language", defaultValue = "-1", required = false) long language, @RequestParam(value = "amount", defaultValue = "-1", required = false) int amount) { model.put("language", language); model.put("amount", amount); LanguageData languageData = languageService.getLanguageData(language); model.put("languageName", languageData.getName()); model.put("words", generationService.generateWords(language, amount)); return "list-words"; } @PostMapping("word") public String saveWord(ModelMap model, HttpServletRequest request, @Valid WordDto singleWord) { translationService.saveOrGetWord(singleWord); return "redirect:" + request.getHeader("Referer"); }
JSP页面代码
<body> <%--@elvariable id="languageName" type="java.lang.String"--%> <h1>Generated words with '${languageName}'</h1> <table class="table"> <thead> <tr> <th>Word</th> </tr> </thead> <tbody> <%--@elvariable id="words" type="java.util.List"--%> <c:forEach items="${words}" var="singleWord"> <tr> <td> ${singleWord.word} </td> <td> <form:form method="post" action="word" modelAttribute="singleWord.word"> <form:input type="hidden" path="languageId"/> <form:input type="text" path="word"/> </form:form> </td> </tr> </c:forEach> </tbody> </table> </body>
解决方案
核心问题是JSP中Spring表单标签的modelAttribute配置错误,导致无法正确绑定当前行的WordDto对象,以下是修正步骤:
- 引入必要标签库
在JSP页面开头添加Spring表单和JSTL标签库声明,否则表单标签无法生效:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- 修正表单绑定配置
将form:form的modelAttribute改为当前循环的singleWord对象,同时为action添加上下文路径,避免请求地址错误:
<form:form method="post" action="${pageContext.request.contextPath}/word" modelAttribute="singleWord">
- 完善表单字段与提交按钮
根据需求用隐藏或输入字段传递WordDto的所有属性,并添加提交按钮触发请求:
<td> <form:form method="post" action="${pageContext.request.contextPath}/word" modelAttribute="singleWord"> <form:hidden path="languageId"/> <form:hidden path="word"/> <button type="submit">保存</button> </form:form> </td>
如果需要让用户编辑word字段,将<form:hidden path="word"/>替换为<form:input path="word"/>即可。
- 确保WordDto类的正确性
WordDto必须包含所有绑定属性的getter和setter方法,否则Spring MVC无法完成参数绑定,示例结构:
public class WordDto { private Long languageId; private String word; public WordDto() {} public Long getLanguageId() { return languageId; } public void setLanguageId(Long languageId) { this.languageId = languageId; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } }
修改后的完整JSP代码
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <title>Generated Words</title> </head> <body> <%--@elvariable id="languageName" type="java.lang.String"--%> <h1>Generated words with '${languageName}'</h1> <table class="table"> <thead> <tr> <th>Word</th> <th>操作</th> </tr> </thead> <tbody> <%--@elvariable id="words" type="java.util.List"--%> <c:forEach items="${words}" var="singleWord"> <tr> <td>${singleWord.word}</td> <td> <form:form method="post" action="${pageContext.request.contextPath}/word" modelAttribute="singleWord"> <form:hidden path="languageId"/> <form:hidden path="word"/> <button type="submit">保存单词</button> </form:form> </td> </tr> </c:forEach> </tbody> </table> </body> </html>
内容的提问来源于stack exchange,提问作者Fi0x
相关产品推荐
相关产品推荐

