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<DropDownListBean> list = new ArrayList<DropDownListBean>();
	
	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<DropDownListBean> getList() {
		return list;
	}

	public void setList(List<DropDownListBean> list) {
		this.list = list;
	}

	@Override
	public 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<DropDownListBean> ddlList=(List<DropDownListBean>)request.getAttribute("list");
%>

4、在jsp页面中给select动态绑定数据并动态选中:

      <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>


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值