[英]how to send user form data to server in spring mvc
I'm having a login form like this: 我有一个这样的登录表单:
<form:form modelAttribute="user" method="post" enctype="application/x-www-form-urlencoded">
<form:input path="userEmailId" />
<form:password path="userPassword />
<input type="submit" value="sign up" />
</form:form>
and this is my user.java
for setters and getters 这是我的
user.java
用于setter和getters
public String getUserEmailId() {
return userEmailId;
}
public void setUserEmailId(String userEmailId) {
this.userEmailId = userEmailId;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
After clicking on sign up, I want this value first get converted to json and then send this value to the server by post method. 单击注册后,我希望首先将此值转换为json,然后通过post方法将此值发送到服务器。 I want to use
RESTful WEB
services in order to achieve this. 我想使用
RESTful WEB
服务来实现这一目标。 Any thoughts on this? 有什么想法吗?
You can use JSON
to serialize your form into a json object
, and then use AJAX
to send a post request to your web service: 您可以使用
JSON
将表单序列化为json object
,然后使用AJAX
向Web服务发送发布请求:
$(function() {
$('#formId').submit(function(event) {
event.preventDefault(); // prevent this form from being submited
var userJson = JSON.stringify(jQuery('#formId').serializeArray());
$.ajax({
type: "POST",
url: "/Path/To/Your/Web/Service",
data: userJson,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
alert(data);//handle it in a proper way
},
failure: function(errMsg) {
alert(errMsg);//handle it in a proper way
}
});
return false;
});
});
And then on your WebService, you should have a method to handle this post request: 然后在您的WebService上,您应该有一个方法来处理此post请求:
@Controller
@RequestMapping("/path/to/your/web/service")
public class WebServiceController{
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<String> handleLogin(@RequestBody User user){
//user contains data passed from UI form. Check it against your repository(database ? )
}
}
Be aware, that this example is only an easy enough example, and it doesn't take into consideration any aspects related to security. 请注意,此示例仅是一个足够简单的示例,并未考虑与安全性相关的任何方面。
function getFormData($form){
// Make sure the checked checkboxes value is true
$.each($form.find("input[type='checkbox']:checked"), function(i, c){
$(c).val(true);
});
// Serialize
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function(n, i){
if(indexed_array[n['name']]){
// concat
var valueArray = [indexed_array[n['name']]];
indexed_array[n['name']] = valueArray.concat(n['value']);
} else {
indexed_array[n['name']] = n['value'];
}
});
// Add the unchecked checkboxes
$.each($form.find("input[type='checkbox']:not(:checked)"), function(i, c){
indexed_array[$(c).attr('name')] = "false";
});
return indexed_array;
}
//...
$.ajax({
url: entityUrl,
method: "POST",
contentType: "application/json",
data: JSON.stringify(getFormData($(form))),
dataType: "json",
success: success,
error: error
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.