但是:作为一个后端开发人员,可能对前端的js不大熟,因此我这里记录一个思路:从后端动态生成html代码,然后输出到前端进行显示。
<myTag:menutree/>
同时我还要引入mytag.tld自定义标签文件:
<%@ taglib prefix="myTag" uri="/WEB-INF/tlds/myTag.tld" %>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.0</jsp-version>
<short-name>myTag</short-name>
<uri>/WEB-INF/tlds/myTag.tld</uri>//文件路径
<tag>
<name>menutree</name>//自定义标签属性
<tag-class>org.root.ambow.web.tag.MyTag</tag-class>//该属性的目标类,作为内容输出
<body-content>scriptless</body-content>
</tag>
</taglib>
package org.root.ambow.web.tag;
import java.util.List;
import javax.servlet.jsp.JspWriter;
import org.apache.shiro.SecurityUtils;
import org.root.ambow.model.vo.FirstMenuVo;
import org.root.ambow.model.vo.UserVo;
import org.root.ambow.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
public class MyTag extends RequestContextAwareTag{
private static final long serialVersionUID = 1L;
@Autowired
private ResourceService rs;
@Override
protected int doStartTagInternal() throws Exception {
UserVo uservo = (UserVo) SecurityUtils.getSubject().getSession().getAttribute("uservo");
System.out.println(+uservo);//得到当前登录的uservo对象
StringBuffer menuString = new StringBuffer();
JspWriter out=pageContext.getOut();
rs=this.getRequestContext().getWebApplicationContext().getBean(ResourceService.class);
//将当前的uservo对象的id传入,查找出此登录对象的权限
List<FirstMenuVo>fmv=rs.selectFirstMenuById(uservo.getId());
//fmv里面全部装的是一级菜单的信息
for(FirstMenuVo firstMenuVo:fmv) {
menuString.append("<li>");
menuString.append("<a href=\"\"><i class=\""+firstMenuVo.getIcon()+"\"></i> <span class=\"nav-label\">"+firstMenuVo.getName()+"</span><span class=\"fa arrow\"></span></a>");
//判断该一级标签下面是否还有二级菜单:
//通过用户id和当前一级菜单的id查找它的子菜单信息
List<FirstMenuVo>fmvSon=rs.selectSunMenuByUseridAndPid(uservo.getId(), firstMenuVo.getId());
if(fmvSon!=null) {//如果有二级菜单的话
menuString.append("<ul class=\"nav nav-second-level\">");
for(FirstMenuVo firstMenuSon:fmvSon) {//遍历子级菜单${contextPath}
menuString.append("<li><a class=\"J_menuItem\" href=\"/root-ambow-web"+firstMenuSon.getUrl()+"\" ><i class=\""+firstMenuSon.getIcon()+" \"></i> <span class=\"nav-label\">"+firstMenuSon.getName()+"</span></a></li>");
}
menuString.append("</ul>");
}
menuString.append("</li>");
}
out.write(menuString.toString());
out.flush();
return 0;
}
}
结果就是通过out.write()方法将内容输出到前端。
至此,完成!
?