Java--jsp+ssh+select dynamically bind data and select (solution)

Source: Internet
Author: User

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>


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.