问题描述
< form:form modelAttribute =usermethod = postenctype =application / x-www-form-urlencoded>
< form:input path =userEmailId/>
< input type =submitvalue =注册/>
< / form:form>
,这是我的 user.java
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;
}
点击注册后,我希望先把这个值先转换成json,然后通过post方法把这个值发送到服务器。我想用 RESTful WEB
服务为了实现这一点。对此有何想法?
您可以使用 JSON
将表单序列化为 json对象
,然后使用 AJAX
发送一个请求到你的web服务:
$(function(){
$ ('#formId')。submit(function(event){
event.preventDefault(); //防止这个表单被引用
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,
成功:函数(数据){
alert(data); //在正确的方式
},
failure:function(errMsg){
alert(errMsg); //以正确的方式处理它
}
});
返回false;
});
});
然后在你的WebService上,你应该有一个方法来处理这个post请求:
@Controller
@RequestMapping(/ path / to / your / web / service)
public class WebServiceController {
@RequestMapping(value =/ login,method = RequestMethod.POST)
public ResponseEntity< String> handleLogin(@RequestBody用户用户){
//用户包含从UI表单传递的数据。在数据库中检查它(数据库?)
}
}
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
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. I want to use
RESTful WEB
services in order to achieve this. 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:
$(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:
@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.
这篇关于如何在春季mvc发送用户表单数据到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!