[英]How to send parametr to Controller properly? (Thymeleaf + Javascript)
I am beginner at Thymeleaf.我是 Thymeleaf 的初学者。 All I know is the fact that "Thymeleaf is not Javascript, it is rendered on server."
我所知道的是“Thymeleaf 不是 Javascript,它是在服务器上呈现的”这一事实。 I am making mistakes all the time because I am usually trying to use that Thymeleaf a little like JavaScript.
我一直在犯错误,因为我通常尝试像 JavaScript 一样使用 Thymeleaf。
HTML HTML
<form th:action="@{/user/sqlCode}" method="post">
<button id="newDatabase"></button>
</form>
<textarea id="generatedSql" readonly></textarea>
Controller控制器
@PostMapping(path = { "/user/sqlCode" })
public String createSchema(@RequestParam(???) String tmp) {
String finallyMyValue = tmp;
// STOP I want to have data from generatedSql in this moment (finallyMyValue)
// ...
}
@ MISSION @ @ 使命 @
1. Click button (id: newDatabase) 1.点击按钮(id:newDatabase)
2. Get data from the textarea (id: generatedSql) 2.从textarea中获取数据(id:generatedSql)
3. Send the value with that data to the controller 3. 将带有该数据的值发送给控制器
4. Be happy :) 4. 快乐:)
@ MISSION @ @ 使命 @
I tried a lot of things but only using Javascript.我尝试了很多东西,但只使用 Javascript。 Scenario is always the same, JavaScript is executed totally before Thymeleaf and finally I can't properly read that data... I tried that scenario:
场景总是相同的,JavaScript 完全在 Thymeleaf 之前执行,最后我无法正确读取该数据......我尝试了这种场景:
name
in input tag using JavaScriptname
中name
to the controller.name
发送到控制器。 Screenshot with my failed approach, which ended in a null at the breakpoint:我失败的方法的屏幕截图,在断点处以 null 结束:
I think you should try use Javascript with Ajax:我认为您应该尝试在 Ajax 中使用 Javascript:
<div>
<button type="button" onclick="submitData()">Change Content</button>
</div>
function submitData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("post", "$URL", true);
xhttp.send();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.