【发布时间】:2019-11-24 08:24:57
【问题描述】:
我在 Model 对象中有数据,我想将其放入 Thymeleaf 模板的输入字段中,这样如果值为 null,则不会显示任何内容,否则,输入将在其中包含值。我试过了,但这不起作用。
<input id="first_name" type="text" name="firstName" placeholder="First" value=${accountInfo.firstName} maxlength=31 required>
我将值传递给 java 异常处理程序中的模型对象,如下所示:
@ExceptionHandler(value=SignupFormException.class)
public String handle(HttpSession session, SignupFormException ex, Model response) {
response.addAttribute("accountInfo", (Account) session.getAttribute("accountToRegister"));
response.addAttribute("Error", ex.getMessage());
session.invalidate();
return "redirect:/signup";
}
如何从我在 thymeleaf 模板中传入模型的 accountInfo 对象中获取属性?
更新: 现在它正在工作,但不是第一次在没有模型对象时访问页面。以下是代码:
我的百里香形式:
<form action="/signup_do" th:object="${accountInfo}" method="post">
<input id="first_name" type="text" name="firstName" placeholder="First" th:value=*{firstName} maxlength=31 required>
控制器:
@PostMapping("/signup_do")
public String register(Account account, HttpSession session) {
session.setAttribute("accountToRegister", account);
accountManagement.accountRegistration(account);
return "Success";
}
有一个账户注册服务会抛出 SignupFormException ,由:
@ExceptionHandler(value=SignupFormException.class)
public String handle(HttpSession session, SignupFormException ex, Model response) {
response.addAttribute("accountInfo", (Account) session.getAttribute("accountToRegister"));
response.addAttribute("Error", ex.getMessage());
session.invalidate();
return "redirect:/signup";
}
现在我才拥有一个带有百里香模板属性的模型对象...
【问题讨论】:
标签: spring-boot thymeleaf