如何在Spring Boot中为对象设置表单的默认值:Thymeleaf

huangapple go评论89阅读模式
英文:

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.

               &lt;div th:each=&quot;woo : ${workers}&quot;&gt;
					&lt;h1 th:text=&quot;${woo.getFirstName() + &#39; &#39; + woo.getLastName()}&quot;&gt;&lt;/h1&gt;
					&lt;form th:action=&quot;@{/updateWorker}&quot; th:object=&quot;${worker}&quot; method=&quot;post&quot;&gt;
       					&lt;div&gt;First name:&lt;input type=&quot;text&quot; placeholder=&quot;John&quot; name=&quot;firstName&quot; th:value=&quot;${woo.firstName}&quot;/&gt;&lt;/div&gt;
       					&lt;div&gt;Last name: &lt;input type=&quot;text&quot; placeholder=&quot;Smith&quot; name=&quot;lastName&quot; th:value=&quot;${woo.lastName}&quot;/&gt;&lt;/div&gt;    
       					&lt;button type=&quot;submit&quot;&gt;UPDATE&lt;/button&gt;
					&lt;/form&gt;
				&lt;/div&gt;

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,
&lt;button type=&quot;submit&quot; class=&quot;btn update&quot; name=&quot;id&quot; th:value=&quot;${woo.id}&quot;&gt;UPDATE&lt;/button&gt;, 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:

&lt;input type=&quot;hidden&quot; name=&quot;id&quot; th:value=&quot;${woo.id}&quot;/&gt;

huangapple
  • 本文由 发表于 2020年5月30日 09:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/62096714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定