【JSP案例】无限级联|下拉列表

本案例主要通过无限级联的数据结构来设计下拉列表

效果图:

【JSP案例】无限级联|下拉列表_第1张图片

1)设计无限级联数据库,并赋值

设计Id和ParentId字段,根类Id通过子类的ParentId来寻找,比如计算机是根类,他的子类是java和csharp。那java和csharp的parentId是计算机的Id。

【JSP案例】无限级联|下拉列表_第2张图片

2)思路逻辑

建立一个数据结构List>>,List代表这张表,Object代表根类,List代表其子类。

 

我们建立相应的bean Category类(对应数据表)

 

在建立其对应的daoImpl类里面有找到其根类记录的方法和其子类记录的方法(参数就是ParentId,如果传入的是0就是找根类,传入的是其他数字就是找子类)

 

业务逻辑层负责把根类和子类集合封装到List>>结构中

 

Jsp得到集合后,先遍历List 得到map,然后通过keyset得到键值category对象,在通过category键值得到Map的值(集合)

 

遍历子集合后,得到子类对象。

3)JSP逻辑

最外一层先包一层 <% CategoryBiz cb=new CategoryBizImpl(); List>> list=cb.findAllCategory(); for(Map> map:list){ Set set=map.keySet(); for(Category category:set){ String option=""; if(null!=CurrId&&CurrId==String.valueOf(category.getId())){ out.println(String.format(option,category.getId(),"selected=selected",category.getCategoryName())); }else{ out.println(String.format(option,category.getId()," ",category.getCategoryName())); } List childList=map.get(category); for(Category cate:childList){ if(null!=CurrId&&CurrId.equals(String.valueOf(cate.getId()))){ out.println(String.format(option,cate.getId(),"selected=selected","-----"+cate.getCategoryName())); }else{ out.println(String.format(option,cate.getId()," ","-----"+cate.getCategoryName())); } } } } %>




你可能感兴趣的:(jsp)