Use struts2 and Ajax to transmit json objects, and then implement level-2 Association of menus
The following is the source code of my js file:
Copy codeThe Code is as follows:
Var mail = {
// Initialization
Init :{
// Initialize data
Initdata :{
Did :'',
Ttitle :'',
Sendpassword :'',
Description :''
},
// Initialization event
Initevent :{
DataEvent: function (){
$ ("# Did"). unbind ("change"); // obtain the level-1 menu and bind events
$ ("# Did"). bind ("change", function (){
// Alert ($ (this). val ());
Mail. init. initdata. did = $ (this). val ();
// Alert (mail. init. initdata. did );
Mail. init. initevent. getuser ();
});
},
<Span style = "color: # ff0000;"> getuser: function (){
$. Post ("mailAction_showUsers? Did = "+ mail. init. initdata. did, null, function (data ){
Var uidoption = $ ("# uid"); // obtain the level-2 menu
Uidoption. empty (); // clear the list
For (var I = 0; I <data. users. length; I ++ ){
// Fill the option with the loop
Uidoption. append ("<option value =" + data. users [I]. uid + ">" + data. users [I]. username + "</option> ");
}
}); </Span>
},
SubmitCheck: function (){
$ ("# Send"). unbind ("click ");
$ ("# Send"). bind ("click", function (){
Mail. init. initdata. description = $ ("input [type = 'texta']"). text ();
Mail. init. initdata. sendpassword = $ ("input [name = 'sendpassword']"). val ();
Mail. init. initdata. ttitle = $ ("input [name = 'ttitle']"). val ();
If (mail. init. initdata. sendpassword = ""){
Alert ("enter the password! ");
Return false;
}
Else if (mail. init. initdata. ttitle = ""){
Alert ("enter a topic! ");
Return false;
}
Else if (mail. init. initdata. description = ""){
Alert ("Enter the content! ");
Return false;
}
Else
Return true;
});
}
}
}
}
$ (). Ready (function (){
Mail. init. initevent. DataEvent ();
Mail. init. initevent. submitCheck ();
});
This is the background action:
Copy codeThe Code is as follows:
Private int did;
Public String getDid (){
...
}
Public void setDid (Strign did ){
...
}
Public String showUsers (){
Users = (ArrayList <User>) this. userService. getUsersByDid (did );
System. out. println (users. size () + "...");
Return SUCCESS;
}
Struts. xml configuration:
Copy codeThe Code is as follows:
<Package name = "users" namespace = "/" extends = "json-default">
<Action name = "mailAction_showUsers" method = "showUsers" class = "mailAction">
<Result type = "json"> </result>
</Action>
</Package>
Front-end jsp page:
Copy codeThe Code is as follows:
<Td width = "65px"> recipient </td>
<Td> <s: select list = "departmentlist" listKey = "did" listValue = "dname" id = "did" name = "did"> </s: select>
<S: select list = "users" listKey = "uid" listValue = "username" id = "uid" name = "uid"> </s: select>
</Td>
This is implemented and run:
Write to persons in need... Reference