前段时间工作需要,需要构建一个面包屑导航,想到了了.net下的站点地图、面包屑导航控件,于是自己也做了简单的封装,形成了jsp下的站点地图,不足之处忘大家指正。
涉及到的文件:
SiteMapUtil.java 用来构建站点地图的工具类
BaseAction.java 调用输出导航栏
SiteMapNode.java 自定义站点节点
sitemap.xml 站点地图配置文件
直接上代码了: sitemap.xml站点地图的配置文件 节点由 href:action path(系统路径) title(标题)构成
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 ListallFullPaths = new ArrayList ();//记录站点地图的所有jsp的节点全路径 private static List allPaths = new ArrayList ();//记录站点地图的所有jsp路径 public static void siteMapSetting(HttpServletRequest request,String path,String href){ try { Map map = (Map )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 (); 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 titles = new ArrayList (); List hrefs = new ArrayList (); while(target!=null){ if(target.getTitle()!=null){ titles.add(target.getTitle()); } if(target.isVariable()&&path.equals(target.getPath())){ hrefs.add(href); target.setHref(href); }else if(target.getHref()!=null&!target.getHref().equals("")){ hrefs.add(target.getHref()); }else { hrefs.add(""); target.setHref(""); } target = target.getParent(); } for (int i = titles.size()-1; i >=0; i--) { String a = hrefs.get(i); if(a.equals("")){ content.append(titles.get(i)+"-->"); }else{ content.append(""+titles.get(i)+"-->"); } } if(content.length()>0){ request.getSession().setAttribute("siteMapString", content.delete(content.length()-6, content.length())); request.getSession().setAttribute("nowPath", nowPath); } } } catch (Exception e) { e.printStackTrace(); } } //将xml中的所有节点转换为自定义节点并存储进入Map中,key:fullpath(全路径,可唯一确定一个节点) public static void put2Map(Element root,SiteMapNode parent,Map map){ SiteMapNode node = getXML2Bean(root); if(parent!=null){ node.setParent(parent); parent.getChildren().add(node); } map.put(node.getFullPath(), node); allFullPaths.add(node.getFullPath()); allPaths.add(node.getPath()); Iterator it = root.elementIterator(); while(it.hasNext()){ put2Map(it.next(),node,map); } } //将xml中读取的Element转换成自定义的节点对象:SiteMapNode public static SiteMapNode getXML2Bean(Element ele){ SiteMapNode node = new SiteMapNode(); node.setPath(ele.attribute("path").getText()); node.setTitle(ele.attribute("title").getText()); node.setHref(ele.attribute("href")==null||ele.attribute("href").equals("")?"":ele.attribute("href").getText()); node.setVariable(ele.attribute("href")==null||ele.attribute("href").equals("")?true:false); return node; } }
自定义节点SiteMapNode:
package com.zohl.quicklms.base.util;
import java.util.ArrayList; import java.util.List; public class SiteMapNode { private String title; //节点显示名称 private String path; //jsp页面全路径 与 struts配置文件中的path保持一致 private String href; //action全路径 private boolean variable;//记录该几点的href是否是动态获取的 当配置文件中已经定义好href则 variable = false; 否则为true private String fullPath;//本节点在节点树中的路径 = 父节点的全路径+本节点的path private SiteMapNode parent;//父节点 private Listchildren = new ArrayList ();//子节点集合 public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getHref() { return href; } public void setHref(String href) { this.href = href; } public List getChildren() { return children; } public void setChildren(List children) { this.children = children; } public SiteMapNode getParent() { return parent; } public void setParent(SiteMapNode parent) { this.parent = parent; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getFullPath() { if(fullPath==null||"".equals(fullPath)){ if(parent!=null){ fullPath = parent.getFullPath()+path; }else{ fullPath = path; } } return fullPath; } public void setFullPath(String fullPath) { this.fullPath = fullPath; } public boolean isVariable() { return variable; } public void setVariable(boolean variable) { this.variable = variable; } }
BaseAction.java关键在于调用
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String url = request.getRequestURL().toString(); String queryString = request.getQueryString(); ActionForward forward = super.execute(mapping, form, request, response); try{ request.getSession().setAttribute("siteMapString", ""); //面包屑导航 if(forward!=null&&forward.getPath()!=null&&!forward.getPath().equals("")){ SiteMapUtil.siteMapSetting(request, forward.getPath(), url+"?"+queryString); } }catch(Exception exp){ exp.printStackTrace(); } return forward; }