Logon verification is often used in project development. This is especially beneficial to the user experience. If you are interested, refer to it and it may help you learn login verification, okay, I'm not talking about it anymore. I don't know much about the code.
Action
The Code is as follows:
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 utilizes spring. It is very important that studentControl is not serialized. If it is serialized, an error is reported.
Public StudentControl getStudentControl () {// This error is returned for a long time.
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 existing ";
} Else {
Username = "user does not exist ";
}
Return "success ";
}
}
The most important one above is @ JSON (serialize = false )... There are comments ....
Struts. xml
The Code is as follows:
Json-default is inherited from struts-default ....
Login.html
The Code is as follows:
// Migrate and remove 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" cmd.html ("username cannot be blank ");
$ (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 ("successful" + data. username );
},
Error: function (){
Alert ("error ");
}
})
}
}
});
The above time: Math. random () * 1000 has no practical significance mainly to prevent the cache from affecting asynchronous refresh... The first draft has been implemented and can be changed later