我有一个 Controller 如下
@RequestMapping(value = "/user/{userId}/personal", method = RequestMethod.GET)
public String getUserPersonalInfo(@PathVariable("userId") Integer userId, Model model) throws ServiceBusinessException {
UserBO userBO = userServiceImpl.findUserByUserId(userId);
model.addAttribute("user", userBO);
model.addAttribute("userId", userId);
model.addAttribute("genders", EnumSet.allOf(Gender.class));
model.addAttribute("maritalStatuses", EnumSet.allOf(MaritalStatus.class));
model.addAttribute("ethnicities", EnumSet.allOf(Ethnicity.class));
System.out.println(EnumSet.allOf(Ethnicity.class));
return "personal";
}
请注意以下事项:
这是从 UI 到 Controller 的 GET 调用,然后 Controller 处理该请求并将用户以及其他信息返回到 View (personal.html)。
现在在 individual.html 上我需要一种方法来编辑此用户信息。因此我正在考虑将此用户数据绑定(bind)在 th:form 标签中
然后,该表单将用于向 Controller 发送发布请求,以保存用户所做的更改。
问题是我还没有看到任何完成此操作的代码示例。我见过的所有示例,表单都是空的,然后加载页面。用户填写空表单并单击提交,该信息然后存储在数据库中。
然而,另一方面,我正在做相反的事情。我需要在页面加载时使用用户对象填充表单,然后在提交时应将该表单保存在数据库中。
有人可以指导我这个例子吗?
最佳答案
@user641887
在页面加载时填充用户详细信息时,使用 th:field 绑定(bind)数据对于只读表单,当用户单击编辑按钮时,使允许编辑的表单字段变为可编辑。现在用户可以编辑所需的信息并保存详细信息。
thymeleaf 表单代码示例
<input type="text" th:field="*{datePlanted}" />
关于javascript - thymeleaf::使用服务中的数据填充表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47196623/