如何将数据从HTML传输到控制器Spring [英] How to transfer data from HTML to controller Spring

查看:86
本文介绍了如何将数据从HTML传输到控制器Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I work with Spring Boot. I do not have a web.xml and jsp file

我有一个控制器来处理数据并将其写入数据库.

I have a controller that processes the data and writes it to the database.

@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的帮助下检查了控制器.

I checked the controller with the help of the Swagger it works.

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

And I have an HTML page in which the user enters data.

<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>

如何将数据从页面传输到控制器?

How to transfer data from the page to the controller?

谢谢您的帮助

推荐答案

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

It depends on what you are using in order to make calls between client (the browser) and server I would use an AJAX call by using JQuery and doing something like this

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)将侦听器添加到提交中

In order to submit data you can add a listener to the submit in this way (always by using 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 
    }
    });
}

安吉洛

这篇关于如何将数据从HTML传输到控制器Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆