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 );
}