Questions about Ajax requests for SPRINGMVC

Source: Internet
Author: User

Using SPRINGMVC to receive the data from the foreground Ajax to complete the registration function.

In order to facilitate the, the front-end JS model and the background user write a consistent function. The code is as follows

Front:

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,

};

}

};

Background code:

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]";

}

}

The code inside the controller is as follows: mainly dealing with the business logic on the registration side, thinking of receiving the data with the mailbox verification, and the mailbox sending code has been tested. The next step is for the foreground to send data via 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 came in");

SYSTEM.OUT.PRINTLN (user);

System.out.println (Uuid.randomuuid (). toString ());

Sendjmail.sendmail ("[Email protected]", "a great Try");

return null;

}

}


The front desk Ajax sends the code as follows:

var url = "./register"

$$. $setAction (Pro + ". Registaction", function (Newuser,url,success,error) {

$$. $ajax ({

Url:url,

Data:newuser,

Async:true,

Success:success,

Error:error

});

});

But always show badrequest400. This is very embarrassing, and then through the Baidu just know. SPRINGMVC Ajax takes advantage of JSON data and background interaction. It must be ensured that the data in the data and background model is consistent. Simply put, the data on my registration page does not fill the user object in my front-end JS.

Then change to the following situation:

. . .

This.getuserobj = function () {

return{

"ID": id | | 0,

"Name": Name,

"Password": password,

"Sex": Sex,

"Telephone": Telephone,

"Role": Role | | "Normal User",

"Regist_time": Regist_time | | New Date (). toLocaleTimeString (),

"Email_address": email_address,

"Active_code": Active_code | | "Not set",

"Status": status | | "0",

"Checkcode": Checkcode,

};

}

. . .

Then execute: The spool data is printed as follows

POST

/%e5%9c%a8%e7%ba%bf%e4%b9%a6%e5%9f%8e0.2

Ajax's coming in.

User [

Id=0,

NAME=HZG,

Password=123,

Sex=1,

telephone=15829243533,

Role= Ordinary users,

Regist_time= 2:28:19,

[Email protected],

Active_code= not set,

Status=0,

Checkcode=ymxq

]


Summary: It is in the SPRINGMVC, the use of JSON and background interaction, it is necessary to ensure the consistency of the data volume, so it is recommended that there is no case, it is best to give some default values.

Questions about Ajax requests for SPRINGMVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.