java - 如何将数据从 HTML 传输到 Spring Controller

标签 java html spring spring-mvc

我使用 Spring Boot。 我没有 web.xml 和 jsp 文件

我有一个 Controller 来处理数据并将其写入数据库。

@RequestMapping(value = "/registration", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Registration user", response = Account.class)
public AccountDto registration(@Valid @RequestBody RegistrationDto registration, HttpServletResponse response) {
    Account account = new Account(registration.getEmail(), registration.getPassword());
    return new AccountDto(account);
}

我在 Swagger 的帮助下检查了 Controller ,它是否有效。

我有一个 HTML 页面,用户可以在其中输入数据。

<body>
    <h1 th:inline="text">Please register</h1>

    <form th:action="@{/logout}" method="post">
        <div>
            <label>
                E-mail:<input type="email" name="email"/>
                Password:<input type="password" name="password"/>
            </label>
        </div>
        <input type="submit" name="registration" value="Registration"/>
    </form>
</body>

如何将数据从页面传输到 Controller ?

感谢您的帮助

最佳答案

这取决于您使用什么来在客户端(浏览器)和服务器之间进行调用 我将通过使用 JQuery 并执行类似的操作来使用 AJAX 调用

var dataObj = new Object();
dataObj.email = $("#email").val();
dataObj.password = $("#passord").val();

$.ajax({
url : '/registration',
dataType : 'json',
contentType : 'application/json; charset=UTF-8',
type : 'POST',
data: JSON.stringify(dataObj),              
beforeSend : function(){
    //do something before send (e.g. show a message like: please wait)
}); 

},
complete   : function(){
    //do something after sent (e.g. remove the message please wait)
},
success : function(data) {
    //handle the success response 
},
error : function(data) {
    //handle the error response 
}
});

希望有用

安杰洛

编辑

为了提交数据,您可以通过这种方式添加一个监听器来提交(始终使用 JQuery)

//This is called after the DOM is fully loaded
$(function() {
   $("#mySubmitId").click(function(evt){
       //Prevent the default submit
       evt.preventDefault();
       //call the submit funtion
      submitData();
   });
});

function submitData()
{
    var dataObj = new Object();
    dataObj.email = $("#email").val();
    dataObj.password = $("#passord").val();

    $.ajax({
    url : '/registration',
    dataType : 'json',
    contentType : 'application/json; charset=UTF-8',
    type : 'POST',
    data: JSON.stringify(dataObj),              
    beforeSend : function(){
        //do something before send (e.g. show a message like: please wait)
    }); 

    },
    complete   : function(){
        //do something after sent (e.g. remove the message please wait)
    },
    success : function(data) {
        //handle the success response 
    },
    error : function(data) {
        //handle the error response 
    }
    });
}

安杰洛

关于java - 如何将数据从 HTML 传输到 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43514945/

相关文章:

java - 是否有任何原因 EnumMap 和 EnumSet 不可导航

html - 为什么明显合适的子内联 block 会溢出?

javascript - 为智能手机制作动画 HTML 表格

java - tomcat运行时上下文初始化失败

java - 如何将导入的 jar 文件添加到 eclipse 中的 web-inf/lib 以用于我的构建?

java - 如何在 android studio 中将 TimeStamp datetime 的值转换为 mysql

java - 拆分字符串并用标点符号和空格分隔

macos - Oracle 的 JDK 7 for Mac 安装不一致,如何彻底摆脱旧的 Apple JDK?

html - CSS Bootstrap : How to line up items?

java - Setter 注入(inject)不适用于 Spring 的工厂方法注入(inject)