如何使用 thymeleaf 获取 textarea 数据 [英] How to grab textarea data using thymeleaf
问题描述
我有一个包含字段的数组,我使用 textareas 将其中的字段输出到 html 中,以便用户可以编辑它们并再次保存它们.
I have an array with fields, of which I've output onto html using textareas so that a user can edit them and save them again.
我可以使用我拥有的 set 方法设置数据,但我不确定如何获取用户输入的 textarea 文本以将其传递给该方法.我也在使用 Thymeleaf.
I can set the data using a set method I have, but I'm not sure how to get the textarea text the user entered to pass it into the method. I'm using Thymeleaf also.
<td>
<textarea name = "fitNumber" class="form-control input-sm" rows="1"
th:value="${fit.setFitNumber(Need value set here)}">
</textarea>
</td>
编辑
我会稍微改写一下,因为它读起来不正确.
I'll rephrase a little, as it reads incorrectly.
你能在 Thymeleaf 的 setter 中传递一个变量吗?例如${person.setName(name)}
Can you pass a variable within a setter in Thymeleaf.
e.g. ${person.setName(name)}
此外,传递的参数是否可以是 中的文本?
also, can that argument being passed be the text within a <textarea></textarea>
?
我不确定如何获取 textarea 文本并将其传递到 thymeleaf 中的 setter 方法中.
I'm not sure how to get the textarea text and pass it inside a setter method within thymeleaf.
推荐答案
你有办法解决这个问题......你需要阅读创建百里香叶表单(也许整个 spring MVC 过程).
You have a ways to go for this... you need to read up on creating thymeleaf forms (and maybe the whole spring MVC process).
http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#creating-a-form
回答您的问题时,应使用如下所示的表单:
In answer to your question, should be using a form that looks like this:
<form th:object="${fit}">
<textarea class="form-control input-sm" rows="1" th:field="*{fitNumber}" />
</form>
th:object 与 th:field 结合,在您提交表单时自动将 textarea 的值绑定到您的对象——当您提交表单时,spring 将调用 fit.setFitNumber(value from input area) 在幕后,您的控制器方法会将 fit 对象作为参数传递给它.
th:object in combination with th:field automatically binds the value of the textarea to your object when you submit the form -- when you submit the form, spring will call fit.setFitNumber(value from input area) behind the scenes and your controller method will have the fit object passed to it as a parameter.
这篇关于如何使用 thymeleaf 获取 textarea 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!