thymeleaf - 如何使用 thymleaf 中的另一个对象设置对象的值?
问题描述
我正在传递一个帖子对象和当前的loggedInUser,我想将“*{author}”的值设置为“${loggedInUser.uid}”。
@GetMapping("/showNewBlogForm")
public String showNewPostForm(Model model) {
Post post = new Post();
model.addAttribute("post", post);
model.addAttribute("loggedInUser", getLoggedInUser());
return "new_post";
}
<div class="container">
<h2>Create Blog</h2>
<h2 th:text="${loggedInUser.uid}"></h2>
<form action="#" th:action="@{/publishPost}" th:object="${post}" method="post">
<input type="text" th:field="*{title}" placeholder="Title" class="form-control mb cp-4" required>
<input type="text" th:field="*{excerpt}" placeholder="Excerpt" class="form-control mb cp-4" required>
<textarea type="text" rows="10" th:field="*{content}" placeholder="Content" class="form-control mb cp-4" required></textarea>
<input type="text" th:value="${loggedInUser.uid}" th:field="*{author}" class="form-control mb cp-4">
<input type="text" th:field="*{tagsString}" placeholder="Tags" class="form-control mb cp-4">
<button type="submit" class="btn btn-info col-2"> Publish </button>
</form>
<hr>
<a th:href = "@{/}"> Back to Home Page</a>
</div>
解决方案
只需替换th:field="*{author}"
为name="author"
:
<input type="text" th:value="${loggedInUser.uid}" name="author" class="form-control mb cp-4">
由于th:value
输入将默认包含“loggedInUser”的uid。
由于name
输入的值将Post
在提交表单时绑定到对象的“作者”字段。
推荐阅读
- python - 尝试在 Kaggle 中加载模块 pynrrd 时出现导入错误
- ios - 如何使用 SwiftUI 将图像和标签添加到标签栏项目
- sql - 如何删除产品
- java - java.io.FileNotFoundException:/raw/storage/1693-44A7/DCIM/Camera/IMG_20190525_080405.jpg(没有这样的文件或目录)
- azure-devops - 使用 GitHub 检查 Azure Pipelines 中的注释
- jquery - jQuery 不会找到动态创建的父元素来绑定子元素
- java - 如何在java中的数组列表中向前和向后移动元素
- python - Powershell 正在尝试从不存在的目录中使用 python
- javascript - 如何从 Firestore 取回整个数组而不是对象?
- javascript - DatePicker 是否反应具有“值”属性作为输入?