1. This code is appropriate for all Drop-down list values
2. Only one common method is required for all Drop-down lists of a project;
Step One: Create an entity bean;
Copy Code code as follows:
public class Dictionarybean {
Id of the private String value_id;//drop-down box option
The value of the private String value;//drop-down box option
Private String flag;//The type of value for the Drop-down box, such as flag=1, Drop-down list for province information, flag=2 for city level information, etc.
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 TwoIn your action, write the following method to take Listbean, Listbean package is Bean.valueid and bean.value;
Copy Code code as follows:
/**
*
* @author Zhuangzi
* @class com.hzdracom.action.DictionaryAction
* @method ListDictionary
* @Directions get Drop-down list information Common method
* @date 2013-3-21 a.m. 10:08:39 void
*/
public void ListDictionary () {
String json= "";
try{
ListDictionary = Dictionaryservice.handlelistdictionary (bean);
Json=json.tojsonstring (listdictionary);//Convert to 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 Code code as follows:
<action name= "ListDictionary" method = "ListDictionary" class= "Com.hzdracom.action.DictionaryAction" >
</action>
Step threeEncapsulation Common JS code js file name Dictionary.js
Copy Code code as follows:
/**
*↓↓↓↓↓↓↓↓↓↓
* Author: Zhuangzi
* Class Name: Public method initialization
* Function: Public method initialization
* Detailed: Common method initialization
* Version: 1.0
* Date: 2013-03-22
Description
*↑↑↑↑↑↑↑↑↑↑
*/
$ (function () {
/*1. Drop down box value ↓↓↓*/
Selectbox ([
Copy Code code as follows:
{id_: "#down_type", ID: "#downtype", Flag: "2"},
{id_: "#kuai_flag", ID: "#kuaiflag", Flag: "3"}]);
SELECTBOX{[{ID_: "The ID of the selected value of your Drop-down box", ID: "The ID of the dropdown box", Flag: "The type of Drop-down box value"}]};
Copy Code code as follows:
There is a {id_: "#kuai_flag", ID: "#kuaiflag", Flag: "3"} This object has a dropdown box information;
});
/**
*↓↓↓↓↓↓↓↓↓↓
* Author: Zhuangzi
* Class name: Get Drop-down Box Value common method
* Function: Drop down list to take value
* Detailed: Load drop down list
* Version: 1.0
* Date: 2013-03-22
* Description: If the new Drop-down box needs to add an object to the array list
* Parameter: id_: Dropdown box is selected value id;id: Drop-down box Id;flag: The Type of Drop-down box value, that is, display content type
*↑↑↑↑↑↑↑↑↑↑
*/
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 FourThe hidden field information below the page information is the value that will be selected when you select a query.
Introduce JS
Copy Code code 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 Code code as follows:
<s:hidden id= "Down_type" name= "Yhaoportalsdownbean.downtype"/>
<TD width= "25%" height= "class=" "addtabletd1" > Download type identification:</td>
<TD width= "25%" height= "class=" >
<select id= "Downtype" name= "Yhaoportalsdownbean.downtype" class= "Textfrom" class= "width:120" >
<option value= "" >---all---</option>
</select>
</td>
Step FiveAdditional information DAO inside method The flag used in the following method is the flag value in JS
Copy Code code as follows:
Public list<dictionarybean> handlelistdictionary (Dictionarybean Bean) throws Dataaccessexception,exception {
list<dictionarybean> list = new arraylist<dictionarybean> ();
try{
String sql= "";
/* Get page drop-down list * *
if (Bean.getflag (). Equals ("1")) {
sql = "Select Page_flag value_id,page_name VALUE from Yhao_portals_page";
}
/* Get download Type ID dropdown list * *
if (Bean.getflag (). Equals ("2")) {
sql = "Select Downtype_flag value_id,downtype_name VALUE from Yhao_portals_downtype";
}
/* Get block ID dropdown 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;
}
Well, it's done.