英文:
How to set default form values for objects in Spring Boot : Thymeleaf
问题
好的,以下是翻译好的部分:
好的,所以我正在尝试学习Spring Boot和Thymeleaf。我有一个名为worker的对象,该对象被提交和创建,放入数据库,然后Thymeleaf页面会更新,以允许我编辑刚刚创建的worker。对于编辑过程,我需要将Worker对象以相同的id保存到数据库,以便更新它,我的问题在于ID部分。我不希望用户在网页上输入id,而是希望它自动设置。问题是,Spring Boot不知道我当前正在编辑哪个对象,但我有一种感觉Thymeleaf知道,只是我不知道如何操作。
以下是代码部分,已经进行了翻译:
<div th:each="woo : ${workers}">
<h1 th:text="${woo.getFirstName() + ' ' + woo.getLastName()}"></h1>
<form th:action="@{/updateWorker}" th:object="${worker}" method="post">
<div>姓氏:<input type="text" placeholder="John" name="firstName" th:value="${woo.firstName}"/></div>
<div>名字:<input type="text" placeholder="Smith" name="lastName" th:value="${woo.lastName}"/></div>
<button type="submit">更新</button>
</form>
</div>
编辑:所以我有这个^... worker对象有一个变量id,我希望它在表单提交时自动设置为对象“woo”的id。有关如何操作的任何想法吗?另外,如果对我编写的Thymeleaf模板有任何有益的批评意见,将不胜感激,谢谢。
编辑:所以,就在一个小时后,我找到了部分解决方案,
<button type="submit" class="btn update" name="id" th:value="${woo.id}">更新</button>
它允许我在按下按钮时设置一个值。尽管如此,如果我将来想设置几个默认值怎么办,我刚刚找到的这个临时解决方案只允许一个值,如果我想设置两个甚至三个值怎么办?有什么想法吗?
英文:
Alright, so im trying to learn Spring Boot and thymeleaf. I have object worker that gets submitted and created, put in a database then the thymelaf page is updated to allow me to edit the worker I just created. For the editing process I need to save the Worker object to the database with the same id so it will update it, my problem is that ID part. I dont want the user to input the id on the webpage but I want it to automatically be set. The problem is, Spring Boot doesnt know which object im editing at the moment, but I have a feeling that thymeleaf knows, I just dont know how to go about it.
<div th:each="woo : ${workers}">
<h1 th:text="${woo.getFirstName() + ' ' + woo.getLastName()}"></h1>
<form th:action="@{/updateWorker}" th:object="${worker}" method="post">
<div>First name:<input type="text" placeholder="John" name="firstName" th:value="${woo.firstName}"/></div>
<div>Last name: <input type="text" placeholder="Smith" name="lastName" th:value="${woo.lastName}"/></div>
<button type="submit">UPDATE</button>
</form>
</div>
And So I have this ^... The worker object has a variable id which I would like it to be automatically set to the id of object "woo" on form submission. Any ideas on how to go about it? Also any helpful critique on how I go about writing my thymeleaf template would be much appreciated, thanks.
edit: And so, just an hour later I figured out a partial solution,
<button type="submit" class="btn update" name="id" th:value="${woo.id}">UPDATE</button>
, that allows me to set one value on button press. Although what if I want to set a few default values in the future, this temporary solution I just found only allows one value, what if I want to set two or even three. Any ideas?
答案1
得分: 0
你可以创建一个带有 ID 值的隐藏输入:
<input type="hidden" name="id" th:value="${woo.id}"/>
英文:
You can create a hidden input with ID value:
<input type="hidden" name="id" th:value="${woo.id}"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论