Java SpringMVC-使用对象请求参数进行表单处理
假设我的应用程序中有以下实体:Java SpringMVC-使用对象请求参数进行表单处理,java,spring,spring-mvc,web,thymeleaf,Java,Spring,Spring Mvc,Web,Thymeleaf,假设我的应用程序中有以下实体: public class Payment { private Long id; private Service service; private User user; private BigDecimal amount; } public cass Service { private Long id; private String name; private BigDecimal minAmount;
public class Payment {
private Long id;
private Service service;
private User user;
private BigDecimal amount;
}
public cass Service {
private Long id;
private String name;
private BigDecimal minAmount;
private BigDecimal maxAmount;
}
public class User {
private Long id;
private String login;
private String password;
private BigDecimal balance;
}
我需要创建允许用户处理付款的html表单(Payment
class的实例)。因此,我需要在我的控制器方法中创建Payment
实例。我知道我可以添加到controller方法中,例如,Service服务
参数,它将由表单中具有相同名称的值填充。但是我如何才能获得已填充的付款
对象?使用填充的服务
和用户
对象?我需要以某种方式将整个服务对象保存在我的服务器页面中?怎么用?
如果有必要的话,我会用百里香 在ELEAF文件中,只要您尊重类的字段结构,Spring就应该足够聪明来填充不同的字段
<form th:object="${payment}" th:action="@{/sendPayment}" method="post">
<input type="text" th:field="*{id}"/>
<input type="text" th:field="*{service.name}"/>
<input type="text" th:field="*{user.id}"/>
<button type="submit">Submit</button>
</form>
你能更清楚地说明你的问题吗?
@RequestMapping(value = "/sendPayment", method = RequestMethod.POST)
public String processPayment(final Payment payment){
doSomethingWithPayment(payment);
}