java Thymeleaf:在表单操作中将输入文本作为参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45716052/
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
Thymeleaf: pass input text as parameter in form action
提问by gualizoe
I'm quite new to Thymeleaf, so I'm struggling with this.
我对 Thymeleaf 很陌生,所以我正在努力解决这个问题。
I want to pass a parameter to the controller when submitting a form, with the value of a text field.
我想在提交表单时将参数传递给控制器,其值为文本字段。
This is my controller:
这是我的控制器:
@PostMapping("/postEndpoint/{myid}")
public String pidUserSubmit(@PathVariable(value = "myid") String myid) {
log.debug("*** MY ID: {}", myid);
return "redirect:/someOtherPage";
}
This is how I've defined the text input:
这就是我定义文本输入的方式:
<input id="myid" name="myid" type="text" maxlength="26" title="Please enter a valid ID" class="texter" th:value="*{myid}">
And this is what I've tried in my html file with thymeleaf:
这就是我在 thymeleaf 的 html 文件中尝试过的:
<form name="myform" id="myform" action="#" th:action="@{/postEndpoint/__${myid}__}" method="post">
I'm getting this log: *** MY ID: null
我收到此日志:*** 我的 ID:空
<form name="myform" id="myform" action="#" th:action="@{/postEndpoint/${myid}}" method="post">
I'm getting this log: *** MY ID: ${myid}
我收到此日志:*** 我的 ID:${myid}
<form name="myform" id="myform" action="#" th:action="@{/postEndpoint/{myid}(myid=${myid})}" method="post">
This is not even getting to the controller
这甚至没有到达控制器
Any help would be appreciated! :)
任何帮助,将不胜感激!:)
回答by RoToRa
This doesn't work like this. Like all server side technologies Thymeleaf templates are executed before being sent to the user, so it can't know what the user will enter and insert it into the action
attribute.
这不是这样工作的。像所有服务器端技术一样,Thymeleaf 模板是在发送给用户之前执行的,因此它无法知道用户将输入什么并将其插入到action
属性中。
You could do something similar with JavaScript, but it would be much easier to use a regular HTML form. That requires you not to use a path variable, but a request parameter in your controller:
您可以使用 JavaScript 执行类似的操作,但使用常规 HTML 表单会容易得多。这要求您不要在控制器中使用路径变量,而是使用请求参数:
@PostMapping("/postEndpoint")
public String pidUserSubmit(@RequestParam(name = "myid") String myid) {
log.debug("*** MY ID: {}", myid);
return "redirect:/someOtherPage";
}
<form th:action="@{/postEndpoint}" th:object="${mymodelobject}">
<input name="myid" type="text" th:value="*{myid}">
</form>
(Don't forget to use th:object
with an object that has a property myid
, if you want to use the *{...}
syntax.)
(如果要使用语法,请不要忘记th:object
与具有属性的对象myid
一起使用*{...}
。)
回答by jdhurricanes
What you're going to need to do is two things:
你需要做的是两件事:
The first item is you need to bind th:field to your input, as:
第一项是您需要将 th:field 绑定到您的输入,如:
<input id="myid" name="myid" type="text" maxlength="26" title="Please enter a valid ID" class="texter" th:field="*{myid}" th:value="*{myid}">
You'll also want to change how your form posts to save by object. That will look like this:
您还需要更改表单帖子以按对象保存的方式。看起来像这样:
<form name="myform" id="myform" action="#" th:action="@{/postEndpoint/(object)}" th:object="${object}" method="post">
Replace 'object' with the variable name of the model you're saving. It should all work following this.
将 'object' 替换为您要保存的模型的变量名称。在此之后它应该都可以工作。