简体   繁体   English

如何在不使用Form Tag的情况下将文本字段值从Jsp页面传递给Spring Controller

[英]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其他方式将使用来自jQueryJavaScript 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.

相关问题 如何将文本字段值从jsp传递到spring控制器类,反之亦然 - How to pass text field value from a jsp to spring controller class and vice versa 如何在Spring中将值从控制器传递到JSP页面? - How do I pass a value from a controller to a JSP page in Spring? 我需要将用户名的值从 Spring controller 传递到 JSP 页面 - I need to pass the value of userName from Spring controller to JSP page Spring MVC:如何将表单数据从JSP传递到控制器 - Spring mvc : How to pass the form data from jsp to controller 如何将值或参数从jsp传递到Spring控制器? - How to pass value or param from jsp to Spring controller? 如何将值从Spring Controller传递到jsp页面?(传递的值将用于初始化jsp页面中的javascript变量 - How to pass values from Spring Controller to jsp page?( the passed values is to be used to intialise javascript variables in jsp page 如何使用ModelAndview将Model(bean)值从控制器传递到JSP页面? - How to pass model(bean) value using ModelAndview from controller to jsp page? 如何将文本字段值从jsp传递给java类 - How to pass text field value from jsp to java class 如何使用表单在spring中将数据从jsp发送到控制器 - How to send data from jsp to controller in spring using form 在提交时将多个值从spring表单jsp传递给spring控制器 - Pass multiple values from spring form jsp to spring controller on submit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM