Html 如何为 Thymeleaf 中的输入字段添加默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31542032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add default values to input fields in Thymeleaf
提问by Kingamere
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.
我在 Spring 中编程并使用 Thymeleaf 作为我的视图,并且正在尝试创建一个表单,用户可以在其中更新他们的个人资料。我有一个个人资料页面,其中列出了用户的信息(名字、姓氏、地址等),并且有一个链接显示“编辑个人资料”。当点击该链接时,他们会将他们带到一个表单,在那里他们可以编辑他们的个人资料。该表单由他们可以输入的文本字段组成,就像您的标准注册表单一样。
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.
这应该就像标准的“编辑个人资料”页面一样。
Here is a segment of my edit_profile.html page:
这是我的 edit_profile.html 页面的一部分:
First Name:
名:
Here is the view controller method that returns edit_profile.html page:
这是返回 edit_profile.html 页面的视图控制器方法:
@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() prints out the expected value, but I'm getting blank input values in the form.
currentUser.getFirstname() 打印出预期值,但我在表单中得到空白输入值。
Thanks.
谢谢。
回答by dardo
I'm slightly confused, you're adding currentUser and a new'd user object to the model map.
我有点困惑,您正在将 currentUser 和一个新的用户对象添加到模型映射中。
But, if currentUser is the target object, you'd just do:
但是,如果 currentUser 是目标对象,您只需执行以下操作:
<input type="text" name="firstname" value="James" th:value="${currentUser.firstname}" />
<input type="text" name="firstname" value="James" th:value="${currentUser.firstname}" />
From the documentation: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html
从文档:http: //www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html
回答by Kingamere
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
.
通过th:field
完全删除并使用th:value
存储默认值、htmlname
和id
模型字段来解决问题。所以name
和id
表现得像th:field
。
回答by Invest
I did not have a form with input elements but only a button that should call a specific Spring Controller method and submit an ID of an animal in a list (so I had a list of anmials already showing on my page). I struggled some time to figure out how to submit this id in the form. Here is my solution:
我没有带有输入元素的表单,只有一个按钮,它应该调用特定的 Spring Controller 方法并在列表中提交动物的 ID(所以我的页面上已经显示了一个动物列表)。我花了一些时间才弄清楚如何在表单中提交这个 id。这是我的解决方案:
So I started having a form with just one input field (that I would change to a hidden field in the end). In this case of course the id would be empty after submitting the form.
所以我开始有一个只有一个输入字段的表单(最后我会更改为一个隐藏字段)。在这种情况下,当然,提交表单后 id 将为空。
<form action="#" th:action="@{/greeting}" th:object="${animal}" method="post">
<p>Id: <input type="text" th:field="*{id}" /></p>
<p><input type="submit" value="Submit" /> </p>
</form>
The following did not throw an error but neither did it submit the animalIAlreadyShownOnPage's ID.
以下没有抛出错误,但也没有提交animalIAlreadyShownOnPage 的ID。
<form action="#" th:action="@{/greeting}" th:object="${animal}" method="post">
<p>Id: <input type="text" th:value="${animalIAlreadyShownOnPage.id}" /></p>
<p><input type="submit" value="Submit" /> </p>
</form>
In another post user's recommended the "th:attr" attribute, but it didn't work either.
在另一个帖子中,用户推荐了“th:attr”属性,但它也不起作用。
This finally worked - I simply added the name element ("id" is a String attribute in the Animal POJO).
这终于奏效了 - 我只是添加了 name 元素(“id”是 Animal POJO 中的 String 属性)。
<form action="#" th:action="@{/greeting}" th:object="${animal}" method="post">
<p>Id: <input type="text" th:value="${animalIAlreadyShownOnPage.id}" name="id" /></p>
<p><input type="submit" value="Submit" /> </p>
</form>