java--jsp+ssh+select動態綁定資料並選中(解決方案)

來源:互聯網
上載者:User

java--jsp+ssh+select動態綁定資料並選中(解決方案)

在三層架構與jsp組合的項目中,如何?select動態綁定資料並動態選中指定行?且看下文:

1、先定義一個Bean類,用於執行個體化select綁定的每一條資料的id和name:

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、在service商務邏輯層中,要有dropdownlist資料來源list的get和set方法:

public class ToDepartmentUpdatePageAction extends ActionSupport {/** *  */private static final long serialVersionUID = 1L;private DepartmentBean departmentBean;private List list = new ArrayList();private 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 getList() {return list;}public void setList(List 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);DropDownListBean dropDownListBean = new DropDownListBean();dropDownListBean.setId((String)objects[0]);dropDownListBean.setName((String)objects[1]);list.add(dropDownListBean);}return "success";}}

3、在jsp頁面中擷取list資料來源以及bean對象:

<% DepartmentBean departmentBean = (DepartmentBean)request.getAttribute("departmentBean"); String selectParentDeptId = String.valueOf(departmentBean.getParentNo());  List ddlList=(List)request.getAttribute("list");%>

4、在jsp頁面中給select動態綁定資料並動態選中:

                                <%                              for(int i=0;i<%if(selectParentDeptId.equals(dropDownListBean.getId().toString())){out.print("selected");} %> ><%=dropDownListBean.getName()%>   <% } %>      


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.