项目刚开始菜单或者分类只支持两级,但是要多级菜单无法支持,因此,花点时间使用java和freemarker 实现多级菜单。。直接上代码。。
一.java代码
1.model类 :分类
public class Type {
private Integer id;
private Integer parentTypeId;
private String typeName;
.....get set 方法
}
2.view类 用来组装分类之间的父子关系
public class TypeView {
private Type type;//当前分类
private List<TypeView> children ; //孩子分类
private Integer level;
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public List<TypeView> getChildren() {
return children;
}
public void setChildren(List<TypeView> children) {
this.children = children;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
}
3.Controller类
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
@RequestMapping("/test/type")
@Controller
public class Test {
@RequestMapping("/list")
public ModelAndView listTypes (HttpServletRequest request){
List<Type> types = typeService.list();//代码省略,功能:从db中获取需要的分类
Multimap<Integer,Type> typeMultimap = ArrayListMultimap.create(); //使用google guava 包 对以获取的分类按照父类Id进行组装
for(int i=0 ;i<types.size();i++){
typeMultimap.put(types.get(i).getParentTypeId(), types.get(i));
}
List<TypeView> list = new ArrayList<TypeView>();
list = subType(0,typeMultimap,0);//递归调用 生成当前节点的子节点
print(list,0);
ModelAndView model = new ModelAndView("/test/type_test");
model.addObject("types", list);
return model;
}
/**
* 递归处理多级分类问题
* @param parentId 父类Id
* @param maps 所有分类的按照父类ID组装后容器
* @param level 分类的级别 0:根
* @return 返回 parentId 节点的子分类节点【可能是多个】
*/
public List<TypeView> subType(Integer parentId, Multimap<Integer,Type> maps,int level){
List<TypeView> list = new ArrayList<TypeView>();
Collection<Type> trList = maps.get(parentId);
for(Iterator<Type> iterator =trList.iterator();iterator.hasNext();){
Type typeTemp = iterator.next();
TypeView typeView = new TypeView();
typeView.setType(typeTemp);
typeView.setLevel(level);
list.add(typeView);
typeView.setChildren(subType(typeTemp.getId(),maps,level+1));
}
if(list.isEmpty()){
return null;
}else{
return list;
}
}
//打印当前归类好的分类
private void print(List<TypeView> list,int level) {
if(list==null ||list.isEmpty()){
return ;
}
for (Iterator<TypeView> iterator = list.iterator(); iterator.hasNext();) {
TypeView typeView = iterator.next();
Type type = typeView.getType();
System.out.println("目录等级:"+typeView.getLevel()+"----"+type.getTypeName());
print(typeView.getChildren(),level+1);
}
}
}
2.freemarker 代码
定义递归宏 代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>type</title>
</head>
<body>
<#macro typeChilden items>
<#assign countSize = items?size>
<ul>
<#list items as item>
<li>${item.type.typeName}
<#if item.children?? && item.children?size >0>
<#if item.children?? && item.children?size >0>
<@typeChilden item.children></@typeChilden>
</#if>
</#if>
</li>
</#list>
</ul>
</#macro>
<br>
<ul>
<#list types as item>
<li>
${item.type.typeName!''}
<#if item.children?? && item.children?size >0>
<@typeChilden item.children></@typeChilden>
</#if>
</li>
</#list>
</ul>
</body>
</html>