最近在写一个部门与人员的上下级联动,用了Struts2的doubleselect,代码成功执行,无报错,但不显示数据。代码如下:
- Java code
//部门与员工的doubleselect public Map<Department, List<User>> getDepUser() { db=DBConnectionManager.getInstance(); con=db.getConnection("idb"); sql="select departmentcode,departmentname from tb_department order by departmentcode"; //ArrayList listDepUser=new ArrayList(); Map<Department,List<User>> mapDepUser=new HashMap<Department,List<User>>(); try { ps=con.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ Department department=new Department(); department.setDepartmentcode(rs.getString("departmentcode")); department.setDepartmentname(rs.getString("departmentname")); sql2="select userid,username from tb_user where departmentcode='"+rs.getString("departmentcode")+"'"; ps=con.prepareStatement(sql2); rs1=ps.executeQuery(); List<User> uList=new ArrayList<User>(); while(rs1.next()) { //List<User> uList=new ArrayList<User>(); User user=new User(); user.setUserid(rs1.getString("userid")); user.setUsername(rs1.getString("username")); uList.add(user); //mapDepUser.put(department, uList); } mapDepUser.put(department, uList); } }catch(SQLException ex) { ex.printStackTrace(); }finally { freeCon(); } //listDepUser.addAll(mapDepUser.values()); return mapDepUser; }
Action里的代码如下:
- Java code
private Map<Department, List<User>> coldepuser;public void setColdepuser(Map<Department, List<User>> coldepuser) { this.coldepuser=coldepuser; }public Map<Department, List<User>> getColdepuser() { return coldepuser; }//跳转到部门添加页面public String add() throws Exception { //HttpServletRequest request=ServletActionContext.getRequest(); UserDao userdao=new UserDao(); //Map<Department,List<User>> mapDepUser=new HashMap<Department,List<User>>(); DepartmentDao departmentdao=new DepartmentDao(); Map<Department, List<User>> colDepUser=userdao.getDepUser(); Collection departmentcodeCol=departmentdao.getDepartmentCode(); setDepartmentcode(departmentcodeCol); setColdepuser(colDepUser); //request.setAttribute("mapDepUser", mapDepUser); return "add"; }
JSP中的代码如下:
- JScript code
<p> <label>上级主管:</label> <s:doubleselect name="department.higherhead" list="coldepuser" listKey="departmentcode" listValue="departmentname" doubleList="coldepuser[top]" doubleName="userid" doubleListKey="userid" doubleListValue="username" theme="simple" /></p>
此问题已经困扰小弟一天,望各位高手解决,不胜感激!
------解决方案--------------------
list="coldepuser"不能拿到你那list的,你指向的是个map
关键就在于LZ用的是个Map,然后接下来的listKey,listValue,dobuleListKey的指向就
都存在问题.
建议LZ这样处理
Action里定义一个List和一个Map
List里寸上一级别的数据,Map则是你那样存
然后<s:dobuleselect的list指向你那list,listKey,listValue则不用说了.