我正在传递一个post对象和当前作者,我希望将“*{ loggedInUser }”的值设置为"${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>1:https://i.stack.imgur.com/7o7d5.png
发布于 2021-02-12 07:35:33
只需用name="author"替换th:field="*{author}"即可
<input type="text" th:value="${loggedInUser.uid}" name="author" class="form-control mb cp-4">多亏了th:value,默认情况下,输入将包含"loggedInUser“的uid。
多亏了name,输入值将在提交表单时绑定到Post对象的"author“字段。
https://stackoverflow.com/questions/66159103
复制相似问题