【问题标题】:How do i pass two objects from thymeleaf form to Spring Controller?如何将 thymeleaf 表单中的两个对象传递给 Spring Controller?
【发布时间】:2026-01-16 22:10:02
【问题描述】:

我想将两个对象从 thymeleaf 形式传递给控制器​​。这是我的百里香代码:

<!DOCTYPE html>
<html lang='en' xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1"/>
<title>Payment Page</title>
</head>
<body>
<h1>Payment List</h1>

<table>
<tr>
 <th>StuId</th>
 <th>Month</th>
 <th>Amount</th>
 
</tr>
<tr th:each="payment:${student.payments}">
<td th:text="${student.id}">2</td>
<td th:text="${payment.month}">devesh</td>
<td th:text="${payment.amount}">23</td>
</tr>
</table>

<h3>Add a Payment</h3>
 <form action="#" th:action= "@{/payments}" th:object="${payment}" method="POST"> 
      <div th:object="${student}" >
 <label for="name">Month:</label>
   <input type="text" name="month" size="50"></input><br/> 
 <label for="amount">Amount:</label> 
   <input type="text" name="amount" size="50"></input><br/>
<input type = "submit"/></form>

</body>
</html>

除了实际在此处提交的付款对象之外,我想将学生对象或 ID 传递给我的控制器,因为任何付款都应对应于特定学生。经过大量搜索,我到现在都找不到任何方法。

我想传递对象的PaymentController方法,因为我正在使用提交表单,我无法在th:action中传递变量

@RequestMapping(method = RequestMethod.POST, value = "/{id}/payments")
    public String doPayment(@ModelAttribute("payment") PaymentRecord paymentRecord, @PathVariable int id) {
        Student st = studentService.getStudentInfo(id);
        
        st.addPayments(paymentRecord);
        System.out.println("entered into do payment");
    

        
        
            studentService.addStudent(st);
      
        
        paymentService.doPayment(paymentRecord);
        return "redirect:{id}/payments";
    }

请提出建议。我被困在这里了

【问题讨论】:

  • 您是否在表单标签中尝试过&lt;input type="hidden" name="studentId" th:value="${student.id}"
  • 是的,我可以试试这个,但是之后如何在我的控制器中获取这个 id,我的意思是它是来自模型参数还是我需要在我的控制器方法签名中显式添加一个参数?抱歉,实际上这是我在 Spring Boot 中的第一个 Web 应用程序,所以我有很多问题:)

标签: html spring spring-boot spring-mvc thymeleaf


【解决方案1】:

你可以用不同的方式做到这一点。

但首先,让我们将 post 方法注解从以下位置缩短:

@RequestMapping(method = RequestMethod.POST, value = "/{id}/payments")

@PostMapping("/{id}/payments")

我还将变量 id 更改为 studentId 以便让下一个阅读您的代码的人更清楚。

现在,将变量作为隐藏输入类型包含在表单标签中:

&lt;input type="hidden" name="studentId" th:value="${student.id}"&gt;

那么你可以

a) 将 String studentId 属性添加到您的 PaymentRecord 类。然后在post方法中,可以调用paymentRecord.getStudentId()

b) 将studentId 的请求参数添加到与@PostMapping 映射的方法中。所以你的方法签名可以有@RequestParam(String studentId)。在这种情况下,studentId 的值将完全暴露给用户。

c) 使用@RequestBody 并将值映射到一个bean。您可以进一步阅读此主题并通过this question 了解一些背景。

【讨论】:

  • 谢谢:)。这个解释正是我想要的:)。我会努力实现这个
  • 当然 - 如果它适合您,请投票或接受作为答案。欢迎来到 SO!
  • 我接受了这个答案,它解决了我的问题,但是当我将控制器方法重定向到另一个控制器方法以填充 thymeleaf 页面时,我在下一步遇到了另一个问题,我收到错误“模型没有价值key id”,我会发布这个问题
【解决方案2】:

而不是:

return "redirect:{id}/payments";

试试这个:

return "redirect:" + id + "/payments";

或(如果需要在重定向中前导 /):

return "redirect:/" + id + "/payments";

如果你想“获取”多个 obj,那么你可以这样做:

 @RequestMapping(value = { "/deleteuserdocument/{docid}/{userid}" }, method = RequestMethod.GET)

这是来自my github repo,我正在“获取”特定用户标识的文档标识。希望这可能会有所帮助。

【讨论】:

  • 但我的问题是在重定向语句之前,我需要从 thymeleaf 页面获取学生 ID 到我的后控制器方法,还是我没有得到你的建议?
最近更新 更多