如何为 Thymeleaf 中的输入字段添加默认值 [英] How to add default values to input fields in Thymeleaf
问题描述
我正在 Spring 中编程并使用 Thymeleaf 作为我的视图,并且正在尝试创建一个表单,用户可以在其中更新他们的个人资料.我有一个个人资料页面,其中列出了用户的信息(名字、姓氏、地址等),并且有一个链接显示编辑个人资料".当点击该链接时,他们会将他们带到一个表单,在那里他们可以编辑他们的个人资料.该表单由他们可以输入的文本字段组成,就像您的标准注册表单一样.
I am programming in Spring and using Thymeleaf as my view, and am trying to create a form where users can update their profile. I have a profile page which lists the user's information (first name, last name, address, etc), and there is a link which says "edit profile". When that link is clicked it takes them to a form where they can edit their profile. The form consists of text fields that they can input, just like your standard registration form.
一切正常,但我的问题是,当点击该链接时,如何将用户的信息添加到输入字段中,以便它已经存在,并且他们只修改他们想要更改的内容,而不必修改重新输入所有字段.
Everything works fine, but my question is, when that link is clicked, how do I add the user's information to the input fields so that it is already present, and that they only modify what they want to change instead of having to re-enter all the fields.
这应该像标准的编辑个人资料"页面一样.
This should behave just like a standard "edit profile" page.
这是我的 edit_profile.html 页面的一部分:
Here is a segment of my edit_profile.html page:
名字:
这是返回edit_profile.html页面的视图控制器方法:
Here is the view controller method that returns edit_profile.html page:
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String getEditProfilePage(Model model) {
model.addAttribute("currentUser", currentUser);
System.out.println("current user firstname: " + currentUser.getFirstname());
model.addAttribute("user", new User());
return "edit_profile";
}
currentUser.getFirstname() 打印出预期值,但我在表单中得到空白输入值.
currentUser.getFirstname() prints out the expected value, but I'm getting blank input values in the form.
谢谢.
推荐答案
通过完全删除 th:field
并改为使用 th:value
来存储默认值解决了问题值,以及模型字段的 html name
和 id
.所以name
和id
就像th:field
.
Solved the problem by removing th:field
altogether and instead using th:value
to store the default value, and html name
and id
for the model's field. So name
and id
is acting like th:field
.
这篇关于如何为 Thymeleaf 中的输入字段添加默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!