關於SpringMvc的ajax請求的問題
利用springmvc接收前臺ajax傳過來的數據完成註冊功能。
為了方便把,前臺js的model和後臺的user寫成一致的功能。代碼如下
前端:
var User = function() {
var id = null;
var name = null;
var password = null;
var sex = null;
var telephone = null;
var role = null;
var regist_time = null;
var email_address = null;
var active_code = null;
var status = null;
var checkCode = null;
this.setId = function(_id) {
id = _id;
};
this.getId = function() {
return id;
}
this.setName = function(_name) {
name = _name;
};
this.getName = function() {
return name;
}
this.setPassword = function(_password) {
password = _password;
};
this.getPassword = function() {
return password;
}
this.setSex = function(_sex) {
sex = _sex;
};
this.getSex = function() {
return sex;
}
this.setTelephone = function(_telephone) {
telephone = _telephone;
};
this.getTelephone = function() {
return telephone;
}
this.setRole = function(_role) {
role = _role;
};
this.getRole = function() {
return role;
}
this.setRegist_time = function(_regist_time) {
regist_time = _regist_time;
};
this.getRegist_time = function() {
return regist_time;
}
this.setEmail_address = function(_email_address) {
email_address = _email_address;
};
this.getEmail_address = function() {
return email_address;
}
this.setActive_code = function(_active_code) {
active_code = _active_code;
};
this.getActive_code = function() {
return active_code;
}
this.setStatus = function(_status) {
status = _status;
};
this.getStatus = function() {
return status;
}
this.setCheckCode = function(_checkCode) {
checkCode = _checkCode;
}
this.getCheckCode = function() {
return checkCode;
}
this.getUserObj = function() {
return{
"id" : id,
"name" : name,
"password" : password,
"sex" : sex,
"telephone" : telephone,
"role" : role,
"regist_time" : regist_time,
"email_address" : email_address,
"active_code" : active_code",
"status" : status,
"checkCode" : checkCode,
};
}
};
後臺代碼:
package org.klh.book_store.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table (name="user_info")
@JsonInclude(value=Include.NON_NULL)
public class User {
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="password")
private String password;
@Column(name="sex")
private String sex;
@Column(name="telephone")
private String telephone;
@Column(name="role")
private String role;
@Column(name="regist_time")
private String regist_time;
@Column(name="email_address")
private String email_address;
@Column(name="active_code")
private String active_code;
@Column(name="status")
private String status;
@Column(name="checkCode")
private String checkCode;
public User() {
}
public User(int id, String name, String password, String sex, String telephone, String role, String regist_time,
String email_address, String active_code, String status, String checkCode) {
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.telephone = telephone;
this.role = role;
this.regist_time = regist_time;
this.email_address = email_address;
this.active_code = active_code;
this.status = status;
this.checkCode = checkCode;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getRegist_time() {
return regist_time;
}
public void setRegist_time(String regist_time) {
this.regist_time = regist_time;
}
public String getEmail_address() {
return email_address;
}
public void setEmail_address(String email_address) {
this.email_address = email_address;
}
public String getActive_code() {
return active_code;
}
public void setActive_code(String active_code) {
this.active_code = active_code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCheckCode() {
return checkCode;
}
public void setCheckCode(String checkCode) {
this.checkCode = checkCode;
}
@Override
public String toString() {
return "User [\nid=" + id + ",\n name=" + name + ", \npassword=" + password + ", \nsex=" + sex + ", \ntelephone="
+ telephone + ", \nrole=" + role + ",\n regist_time=" + regist_time + ",\n email_address=" + email_address
+ ",\n active_code=" + active_code + ",\n status=" + status + ",\n checkCode=" + checkCode + "\n]";
}
}
在controller裏面的代碼如下:主要是處理關於註冊端的業務邏輯,想著接收到數據用郵箱驗證,而且郵箱發送代碼也已經測試通過。接下來就是前臺通過ajax發送數據就好了。
@Controller
public class Register {
public Register() {
}
@RequestMapping(value="/register", method=RequestMethod.POST,
produces="application/json;charset=UTF-8")
@ResponseBody
public User doUserLogin(HttpServletRequest request, HttpServletResponse response, User user) throws IOException {
System.out.println(request.getMethod());
System.out.println(request.getContextPath());
System.out.println("ajax進來了");
System.out.println(user);
//System.out.println(UUID.randomUUID().toString());
//SendJMail.sendMail("[email protected]", "一次偉大的嘗試");
return null;
}
}
前臺ajax發送代碼如下:
var url = “./register”
$$.$setAction(pro + ".registAction",function(newUser,url,success,error) {
$$.$ajax({
url : url,
data :newUser,
async : true,
success : success,
error : error
});
});
但是老是顯示badrequest400. 這就很尷尬,然後通過百度才知道。Springmvc下ajax利用json數據和後臺交互時。必須得保證數據和後臺model裏面的數據量是一致的。簡單來說,就是我註冊頁面的數據沒有填滿我前端的js下的user對象。
然後改成如下情況:
. . .
this.getUserObj = function() {
return{
"id" : id || 0,
"name" : name,
"password" : password,
"sex" : sex,
"telephone" : telephone,
"role" : role || "普通用戶",
"regist_time" : regist_time || new Date().toLocaleTimeString(),
"email_address" : email_address,
"active_code" : active_code || "未設定",
"status" : status || "0",
"checkCode" : checkCode,
};
}
. . .
然後執行:後臺打印數據如下
POST
/%E5%9C%A8%E7%BA%BF%E4%B9%A6%E5%9F%8E0.2
ajax進來了
User [
id=0,
name=hzg,
password=123,
sex=1,
telephone=15829243533,
role=普通用戶,
regist_time=下午2:28:19,
[email protected],
active_code=未設定,
status=0,
checkCode=ymxq
]
總結:就是在springmvc下,利用json和後臺交互時,必須的保證數據量的一致性,所以建議在沒有值得情況下,最好給一些默認值。
關於SpringMvc的ajax請求的問題