日期:2014-05-16 浏览次数:20541 次
? ? ?前段时间工作需要,需要构建一个面包屑导航,想到了了.net下的站点地图、面包屑导航控件,于是自己也做了简单的封装,形成了jsp下的站点地图,不足之处忘大家指正。
涉及到的文件:
SiteMapUtil.java 用来构建站点地图的工具类
BaseAction.java 调用输出导航栏
SiteMapNode.java 自定义站点节点
sitemap.xml? 站点地图配置文件
?
直接上代码了: sitemap.xml站点地图的配置文件 节点由 href:action path(系统路径) title(标题)构成
<?xml version="1.0" encoding="UTF-8"?>
<!--面包屑导航配置文件说明:
title 是页面上显示的内容 即:连接名称
href是超链接,如果没有动态数据则可以直接写入此配置文件 系统优先使用默认配置的href,最后使用系统后台动态赋予的值
path是该请求页面的物理路径 path须与struts中设置的路径保持一致
-->
<sitemap title="首页" path="/pages/stu/index_student.jsp" href="login.do?method=toUserIndex&topMenu=index">
<!--个人中心 -->
<node title="个人资料" path="/pages/stu/user/personal_data.jsp" href="leftNav.do?method=personalData&type=data&leftMenu=userInfo" >
</node>
<node title="修改头像" path="/pages/stu/user/change_avatar.jsp" href="leftNav.do?method=personalData&type=avatar">
</node>
<node title="可用积分" path="/pages/stu/study/myPointList.jsp" href="leftNav.do?method=myPointList">
</node>
<node title="我的档案" path="/pages/stu/user/myRecords_StudyCourse.jsp" href="leftNav.do?method=myRecords&leftMenu=myrecords">
<node title="已学课程" path="/pages/stu/user/myRecords_StudyCourse.jsp"/>
<node title="已获积分" path="/pages/stu/user/myRecords_StudyScore.jsp"/>
<node title="已获证书" path="/pages/stu/user/myRecords_CertificateUser.jsp"/>
<node title="已完成考试" path="/pages/stu/user/myRecords_ExamAchievement.jsp"/>
</node>
</sitemap>
?SiteMapUtil.java
package com.maxi.base.util;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 站点导航的实现
* @author mx
*/
public class SiteMapUtil {
private static List<String> allFullPaths = new ArrayList<String>();//记录站点地图的所有jsp的节点全路径
private static List<String> allPaths = new ArrayList<String>();//记录站点地图的所有jsp路径
public static void siteMapSetting(HttpServletRequest request,String path,String href){
try {
Map<String,SiteMapNode> map = (Map<String,SiteMapNode>)request.getSession().getAttribute("webSiteMap");
String nowPath = (String)request.getSession().getAttribute("nowPath");
if(map==null){
SAXReader reader = new SAXReader();
InputStream inputStream = SiteMapUtil.class.getClassLoader().getResourceAsStream("sitemap.xml");
Document document = reader.read(inputStream);
Element root = document.getRootElement();
map = new HashMap<String,SiteMapNode>();
put2Map(root,null,map);
request.getSession().setAttribute("webSiteMap", map);
}
if(allPaths.size()!=0&&!allPaths.contains(path)){
return;
}else{
if(nowPath==null||"".equals(nowPath)){
nowPath = path;
}else{
if(allFullPaths.contains(nowPath+path)){
nowPath = nowPath + path;
}else{
if(nowPath.contains(path)){
nowPath = nowPath.substring(0, nowPath.indexOf(path))+path;
}else{
for(String s:allFullPaths){
if(s.endsWith(path)){
nowPath = s;
break;
}
}
}
}
}
}
SiteMapNode target = map.get(nowPath);
if(target==null){
return;
}else{
StringBuffer content = new StringBuffer("您的当前位置:");
List<String> titles = new ArrayList<String>();
List<String> hrefs = new ArrayList<String>();
while(target!=null){
if(target.getTitle()!=null){
titles.add(target.getTitle());
}
if(target.isVariable()&&path.equals(target.getPath())){
hrefs.add