我想发送带有model.addattribute的对象,并在保存后获取所有对象的变量。
控制器
@GetMapping(value = "/passport/details/{id}")
@PreAuthorize("isAuthenticated()")
public String passportDetails(Model model, @PathVariable(name = "id") Long id) {
PassportRequest passportRequest = businessService.getPassportRequestById(id);
model.addAttribute("passport", passportRequest);
return "/user/passport/create";
}
@PostMapping(value = "/passport/save")
@PreAuthorize("isAuthenticated()")
public String savePassport(PassportRequest passport) {
businessService.savePassportRequest(passport);
return "redirect:/passport/details/" + passport.getId();
}
html代码:
<form enctype="multipart/form-data" method="post" th:action="@{/passport/save}"
data-parsley-validate="" role="form"
novalidate="" th:object="${passport}" id="sign-form">
<input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{stage}">
<div class="col-6 m-input-group">
<label>Кем командирован</label>
<input type="text" name="postedBy" placeholder="Введите" th:field="*{postedBy}" required>
</div>
</form
问题是我需要做些什么 <input type="hidden" th:field="*{objectVariable}"></input>
发送到每个变量。有更简单的方法吗?
1条答案
按热度按时间waxmsbnn1#
一种方法是使用注解来注解控制器
@SessionAttributes("passport")
(这意味着它将在会话中存储对象,并在此过程中保留其所有属性)。您可能还需要更改
public String savePassport(PassportRequest passport) {
到public String savePassport(@ModelAttribute("passport") PassportRequest passport) {
以确保所有属性对齐。