Not much to say, code
action
package com.lk.action;
import javax.annotation.Resource;
import com.googlecode.jsonplugin.annotations.JSON;
import com.lk.service.StudentControl;
import com.opensymphony. xwork2.ActionSupport;
public class LoginAjaxAction extends ActionSupport {
private String username;
private StudentControl studentControl;
@JSON(serialize=false) //setStudentControl uses spring, which is very important, so that studentControl does not Serialization, if it is serialized, an error will be reported
public StudentControl getStudentControl() {//I have been looking for this error for a long time...ajax keeps returning error
return studentControl;
}
@Resource(name= "studentControl")
public void setStudentControl(StudentControl studentControl) {
this.studentControl = studentControl;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
if(studentControl.getStudentById(Integer.parseInt( username))!=null){
username = "User exists";
}else{
username = "User does not exist";
}
return "success";
}
}
The most important thing above is the @JSON(serialize=false). . . There are comments on it. . . .
struts.xml
What I want to say here is that json-default is inherited from struts-default....
login.html
//Moving in and out of the event focus bound to the user name
$("#un").bind({
focus:function(){
$(this).addClass("txtclick");
},
blur:function() {
var vtxt = $("#un").val();
if (vtxt.length == 0) {
$("#unerror").html("Username cannot be Empty");
$(this).removeClass("txtclick");
}else if(!isInteger(vtxt)){
//Check whether the username format is correct
$(" #unerror").html("Incorrect format!");
$(this).removeClass("txtclick");
}else{
$.ajax({
url : " loginAjax",
dataType : "json",
data : {
username : $(this).val(),
time : Math.random()*1000
},
success : function(data){
alert("success" data.username);
},
error : function(){
alert("error");
}
})
}
}
});
The above time: Math.random()*1000 has no practical significance, mainly to prevent cache from affecting asynchronous refresh . . . This is the first draft. The function has been implemented and can be modified later.