Ajax struts2 drop-down box assignment (applicable to all)

Source: Internet
Author: User

1. This Code is applicable to all drop-down lists.
2. All the drop-down lists of a project only need this public method;
Step 1: Create an object bean;
Copy codeThe Code is as follows:
Public class DictionaryBean {
Private String value_Id; // The option id in the drop-down list
Private String value; // The option value in the drop-down list.
Private String flag; // The type of the value corresponding to the drop-down box, for example, flag = 1, the drop-down list shows the province information, and the flag = 2 shows the municipal information;
Public String getValue_Id (){
Return value_Id;
}
Public void setValue_Id (String valueId ){
Value_Id = valueId;
}
Public String getValue (){
Return value;
}
Public void setValue (String value ){
This. value = value;
}
Public String getFlag (){
Return flag;
}
Public void setFlag (String flag ){
This. flag = flag;
}
}

Step 2Write the following method in your action to retrieve listbean. listbean encapsulates bean. valueId and bean. value;
Copy codeThe Code is as follows:
/**
*
* @ Author ZhuangZi
* @ Class com. hzdracom. action. DictionaryAction
* @ Method listDictionary
* @ Directions public method for obtaining the pull-down list information
* @ Date 10:08:39 void
*/
Public void listDictionary (){
String json = "";
Try {
ListDictionary = dictionaryService. handleListDictionary (bean );
Json = JSON. toJSONString (listDictionary); // convert to a json string
HttpServletResponse response = ServletActionContext. getResponse ();
Response. setContentType ("text/html ");
Response. setCharacterEncoding ("UTF-8 ");
PrintWriter out;
Out = response. getWriter ();
Out. println (json );
Out. flush ();
Out. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}

Sturts. xml configuration information
Copy codeThe Code is as follows:
<Action name = "listDictionary" method = "listDictionary" class = "com. hzdracom. action. DictionaryAction">
</Action>

Step 3Encapsulate public js Code. js file name dictionary. js
Copy codeThe Code is as follows:
/**
* When there are too many threads, there are too many other threads.
* Author: ZhuangZi
* Class Name: public method Initialization
* Function: public method Initialization
* Details: public method Initialization
* Release: 1.0
* Date: 2013-03-22
* Note:
* When there are too many threads, there are too many other threads.
*/
$ (Function (){
/* 1. The value of the drop-down box is always unavailable */
SelectBox ([

Copy codeThe Code is as follows:
{Id _: "# down_type", id: "# downtype", flag: "2 "},
{Id _: "# kuai_flag", id: "# kuaiflag", flag: "3"}]);
// SelectBox {[{id _: "ID of the selected value in your drop-down box", id: "ID of the drop-down box", flag: "Type of the drop-down box value"}]};

Copy codeThe Code is as follows:
// There Is A {id _: "# kuai_flag", id: "# kuaiflag", flag: "3"} such object has a drop-down box information;
});
/**
* When there are too many threads, there are too many other threads.
* Author: ZhuangZi
* Class Name: common method for getting the drop-down box Value
* Function: select a value from the drop-down list.
* Details: load the drop-down list
* Release: 1.0
* Date: 2013-03-22
* Note: if you add a new drop-down box, you need to add an object to the array list.
* Parameter: id _: ID of the selected value in the drop-down box; id: ID of the drop-down box; flag: Type of the value in the drop-down box, that is, the type of the displayed content
* When there are too many threads, there are too many other threads.
*/
Function selectBox (list ){
For (var I = 0, len = list. length; I <len; I ++ ){
(Function (bean ){
Var pageId = $ (bean. id _). val ();
$. Ajax ({
Type: 'post ',
Url: '../dictionary/listDictionary. do ',
Data: 'bean. flag = '+ bean. flag,
DataType: 'json ',
Success: function (json ){
For (var I = 0; I <json. length; I ++ ){
$ (Bean. id). append ("<option value = '"
+ Json [I]. value_Id + "'>"
+ Json [I]. value + "</option> ");
If (pageId! = Null & pageId! = "" & PageId = json [I]. value_Id ){

$ (Bean. id). find ("option"). attr ("selected", true );
}
}
},
Error: function (){
Alert ('error ');
}
});
}) (List [I]);
}
}

Step 4The field information is hidden under the page information, that is, after you select a query item, it will be selected
Introduce js
Copy codeThe Code is as follows:
<Script language = "javascript1.2" src = "<s: url value ="/js/jquery. js "includeParams =" false "/>"> </script>
<Script language = "javascript1.2" src = "<s: url value ="/js/dictionary. js "includeParams =" false "/>"> </script>

Copy codeThe Code is as follows:
<S: hidden id = "down_type" name = "yhaoPortalsDownBean. downType"/>
<Td width = "25%" height = "33" class = "addtabletd1"> download type ID: </td>
<Td width = "25%" height = "33" class = "addtabletd2">
<Select id = "downtype" name = "yhaoPortalsDownBean. downType" class = "textfrom" class = "width: 120">
<Option value = ""> --- all --- </option>
</Select>
</Td>

Step 5Additional information: Method in dao. The flag used in the following method is the value passed by the flag in js.
Copy codeThe Code is as follows:
Public List <DictionaryBean> handleListDictionary (DictionaryBean bean) throws DataAccessException, Exception {
List <DictionaryBean> list = new ArrayList <DictionaryBean> ();
Try {
String SQL = "";
/* Get the page drop-down list */
If (bean. getFlag (). equals ("1 ")){
SQL = "SELECT PAGE_FLAG VALUE_ID, PAGE_NAME VALUE FROM YHAO_PORTALS_PAGE ";
}
/* Get the download type icon drop-down list */
If (bean. getFlag (). equals ("2 ")){
SQL = "SELECT DOWNTYPE_FLAG VALUE_ID, DOWNTYPE_NAME VALUE FROM YHAO_PORTALS_DOWNTYPE ";
}
/* Get the block ID drop-down list */
If (bean. getFlag (). equals ("3 ")){
SQL = "SELECT KUAI_FLAG VALUE_ID, KUAI_NAME VALUE FROM YHAO_PORTALS_KUAI ";
}
List = this. query (SQL, new DictionaryList ());
} Catch (Exception e ){
E. printStackTrace ();
}
Return list;
}

All done.

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.