I will not talk about it anymore.
Action
Copy codeThe 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. xmlCopy codeThe Code is as follows: <package name = "ajax" extends = "json-default">
<Action name = "loginAjax" class = "com. lk. action. LoginAjaxAction">
<Result type = "json"/>
</Action>
</Package>
Json-default is inherited from struts-default ....
Login.htmlCopy codeThe Code is as follows: // The Migration and removal 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" 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