In a three-tier architecture-to-JSP project, how do I implement select dynamic binding data and dynamically select the specified row? and see below:
1. First define a bean class that instantiates the ID and name of each piece of data that is bound to the select:
public class Dropdownlistbean { private String ID; private String name; Public String GetId () {return id; } public void SetId (String id) {this.id = id; } Public String GetName () {return name; } public void SetName (String name) {this.name = name; } }
2, in the service business logic layer, to have the DropDownList data source list of the Get and set method:
public class Todepartmentupdatepageaction extends Actionsupport {/** * */private static final long serialversionuid = 1L; Private Departmentbean departmentbean;private list<dropdownlistbean> List = new Arraylist<dropdownlistbean > ();p rivate int id;public int getId () {return ID;} public void setId (int id) {this.id = ID;} @Resource private Idepartmentmanage departmentmanage;public Departmentbean Getdepartmentbean () {return Departmentbean ;} public void Setdepartmentbean (Departmentbean departmentbean) {This.departmentbean = Departmentbean;} Public Idepartmentmanage Getdepartmentmanage () {return departmentmanage;} public void Setdepartmentmanage (Idepartmentmanage departmentmanage) {this.departmentmanage = Departmentmanage;} Public list<dropdownlistbean> getList () {return List;} public void setlist (list<dropdownlistbean> list) {this.list = list;} @Overridepublic String Execute () throws Exception {Setdepartmentbean (Departmentmanage.getdepartments0 ("where nid =" + ID). Get (0)); LiSt Tmplist=departmentmanage.getddldepartments (); for (int i = 0; i < tmplist.size (); i++) {object[] objects = (object[]) Tmplist.get (i);D ropdownlistbean Dropdownlistbean = new Dropdownlistbean ();d Ropdownlistbean.setid ((String) objects[0 ]);d Ropdownlistbean.setname ((String) objects[1]); List.add (Dropdownlistbean);} Return "Success";}}
3. Get the list data source and the Bean object in the JSP page:
<% Departmentbean Departmentbean = (Departmentbean) request.getattribute ("Departmentbean"); String Selectparentdeptid = string.valueof (Departmentbean.getparentno ()); List<dropdownlistbean> ddllist= (list<dropdownlistbean>) request.getattribute ("List");%>
4. Dynamically bind the data to select in the JSP page and select it dynamically:
<select name= "Departmentbean.parentno" id= "Parentno" > <% for (int i=0;i<ddllist.size (); i++) { Dropdownlistbean Dropdownlistbean=ddllist.get (i); %><option value= "<%=dropdownlistbean.getid ()%>" <%if (Selectparentdeptid.equals ( Dropdownlistbean.getid (). toString ()) {out.print ("selected");}%> ><%=dropdownlistbean.getname ()%> </option> <%}%> </select>