Use Ajax and jquery In the SSH framework to check whether the user registration name is repeated.

Source: Internet
Author: User

1. Import the json package in the lib package library and search for the downloaded resources in CSDN. There are still many

2. Insert the following code in struts. xml:


[Html]
<Package name = "userjson" namespace = "/" extends = "json-default">
<Action name = "userJSONAction _ *" method = "{1}" class = "userAction">
<Result type = "json"> </result>
</Action>
</Package>

<Package name = "userjson" namespace = "/" extends = "json-default">
<Action name = "userJSONAction _ *" method = "{1}" class = "userAction">
<Result type = "json"> </result>
</Action>
</Package> 3. Create a new plug-in named user_add.js and insert the following code (the following code includes the required department or position)


[Javascript]
Var user = {
FormValidate: function (){
$ ("Input [type = 'image']"). unbind ("click ");
$ ("Input [type = 'image']"). bind ("click", function (){
If ($ ("select [name = 'did'] option: selected"). attr ("value") = ""){
Alert ("select a Department ");
Return false;
}
Else {
If (! $ ("Select [name = 'pkids'] option: selected"). attr ("value ")){
Alert ("select a position ");
Return false;
} Else {
If ($ ("# message"). text () = "this user name already exists "){
Alert ("enter a reasonable user name ");
Return false;
} Else {
Return true;
}
}
Return true;
}
});
},
InintEvent: function (){
$ ("Input [name = 'username']"). unbind ("blur ");
$ ("Input [name = 'username']"). bind ("blur", function (){
User. checkUser ($ (this). val ());
});
},
CheckUser: function (username ){
Var parameter = {
Username: username
};
$. Post ("userJSONAction_checkUsername.action", parameter, function (data ){
$ ("# Message"). text (data. message );
If (data. message = "this user name can be used "){
$ ("# Message" ).css ("color", "blue ");
} Else {
$ ("# Message" ).css ("color", "red ");
}
});
}
};
$ (). Ready (function (){
User. formValidate ();
User. inintEvent ();
});

Var user = {
FormValidate: function (){
$ ("Input [type = 'image']"). unbind ("click ");
$ ("Input [type = 'image']"). bind ("click", function (){
If ($ ("select [name = 'did'] option: selected"). attr ("value") = ""){
Alert ("select a Department ");
Return false;
}
Else {
If (! $ ("Select [name = 'pkids'] option: selected"). attr ("value ")){
Alert ("select a position ");
Return false;
} Else {
If ($ ("# message"). text () = "this user name already exists "){
Alert ("enter a reasonable user name ");
Return false;
} Else {
Return true;
}
}
Return true;
}
});
},
InintEvent: function (){
$ ("Input [name = 'username']"). unbind ("blur ");
$ ("Input [name = 'username']"). bind ("blur", function (){
User. checkUser ($ (this). val ());
});
},
CheckUser: function (username ){
Var parameter = {
Username: username
};
$. Post ("userJSONAction_checkUsername.action", parameter, function (data ){
$ ("# Message"). text (data. message );
If (data. message = "this user name can be used "){
$ ("# Message" ).css ("color", "blue ");
} Else {
$ ("# Message" ).css ("color", "red ");
}
});
}
};
$ (). Ready (function (){
User. formValidate ();
User. inintEvent ();
}); 4. Add the message in UserAction and generate the get method.


[Java]
Private String message;

Public String getMessage (){
Return message;
}

Private String message;
 
Public String getMessage (){
Return message;
}
Then add the following method:


[Java]
Public String checkUsername (){
User user = this. userService. getUserByName (this. getModel (). getUsername ());
If (user = null ){
This. message = "this user name can be used ";
} Else {
This. message = "this user name already exists ";
}
Return SUCCESS;
}

Public String checkUsername (){
User user = this. userService. getUserByName (this. getModel (). getUsername ());
If (user = null ){
This. message = "this user name can be used ";
} Else {
This. message = "this user name already exists ";
}
Return SUCCESS;
}
5. Add the following method to the UserDao class:


[Java]
Public User getUserByUsername (String username );

Public User getUserByUsername (String username );
Implement this method in the UserDaoImpl class


[Java]
Public User getUserByUsername (String username ){
// TODO Auto-generated method stub
List <User> userList = this. hibernateTemplate. find ("from User where username =? ", Username );
If (userList. size () = 0)
{
Return null;
}
Else
{
Return userList. get (0 );
}
}

Public User getUserByUsername (String username ){
// TODO Auto-generated method stub
List <User> userList = this. hibernateTemplate. find ("from User where username =? ", Username );
If (userList. size () = 0)
{
Return null;
}
Else
{
Return userList. get (0 );
}
}
6. Add the following method to the UserService class:


[Java]
Public User getUserByName (String username );

Public User getUserByName (String username );
This method is implemented in UserServiceImpl.


[Java]
@ Transactional (readOnly = false)
Public User getUserByName (String username ){
// TODO Auto-generated method stub
Return this. userDao. getUserByUsername (username );
}

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.