spring-boot - 无法使用 th:object 在 Spring Boot 中加载页面
问题描述
它包含一行错误:“模板解析期间发生错误(模板:“类路径资源 [templates//register.html]”)”。我无法打开注册 html 页面以将新对象添加到数据库中。为什么?
<form th:action="@{/register-user}" th:object="user" method="post">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" placeholder="Enter password" name="pswd">
</div>
<div class="form-group form-check">
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
我的控制器
@RequestMapping(path="/register-user", method = RequestMethod.POST)
public String registerNewUser(@Valid @ModelAttribute("user") User user){
userService.register(user);
return "redirect:/login";
}
解决方案
您的“用户”是一个模型属性,因此,要访问它(并将其用作th:object
),您必须使用${...}
语法。结果将如下所示:
<form th:action="@{/register-user}" th:object="${user}" method="post">
推荐阅读
- clojure - 不能在 cljs 文件中要求 clj ns
- ruby-on-rails - 如何在 Rspec 中测试一个简单的块参数?
- html - Boostrap 4 上的中心页脚容器
- javascript - nanogallery2:单击缩略图时是否可以转到链接而不是打开灯箱
- javascript - 剑道弹出加载html内容
- azure - Authorization_RequestDenied 使用 /groups/ID/members 获取组成员
- sql - Google BigQuery [标准 SQL] - 仅合并选定列上的行,数组 agg 其余部分
- mysql - 对表中没有记录的列显示零
- r - 在现有的 excel 文件中插入在 R 中创建的可编辑图形
- perforce - Perforce Helix4Git 无法在没有权限的客户端中映射图形存储库