S2SH integrates JQuery + Ajax to implement login verification. Code _ jquery

Source: Internet
Author: User
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
Related Article

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.