[英]How to pass text field value from Jsp page to Spring Controller without using Form Tag
I have 3 fields like Name, Age, Address
and it has a default value that's coming from a database, now I want to edit or update each field value using the edit button, so I want to pass field edit value from jsp to spring controller. 我有3个字段,如
Name, Age, Address
,它有一个默认值来自数据库,现在我想使用编辑按钮编辑或更新每个字段值,所以我想将字段编辑值从jsp传递给spring控制器。
<input type="text" value="${map.Fname}" id="nameEdit" disabled>
<input type="button" class="btn btn-outline-default float-right btn-sm" value="Edit" onclick="nameEdit()">
<input type="submit" value=submit>
Here I have two buttons edit is for to edit the default value another submit button for to submit the value which is entered in the text field. 这里我有两个按钮编辑用于编辑默认值另一个提交按钮,用于提交在文本字段中输入的值。
If you don't want to use Form other way would be using POST call from jQuery
or JavaScript
如果您不想使用Form其他方式将使用来自
jQuery
或JavaScript
POST调用
Let's assume your Rest Controller looks like this 我们假设您的Rest Controller看起来像这样
@PostMapping("/save")
public void saveDetail(@RequestBody UserDetail data){
}
since you don't want to use form you can replace type submit with button like this 因为你不想使用表单,你可以用这样的按钮替换类型提交
<input type="button" value="submit" id="btnSubmit">
POST call using jQuery 使用jQuery进行POST调用
var data = {
"fname" : "Test",
"lname" : "Test",
}
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: "application/json"
});
Using JavaScript 使用JavaScript
var xhr = new XMLHttpRequest();
var url = "url";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
console.log(json.email + ", " + json.password);
}
};
var data = JSON.stringify(data );
xhr.send(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.