關於SpringMvc的ajax請求的問題

來源:互聯網
上載者:User

標籤:json   springmvc   資料支援   

利用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請求的問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.