首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jsp站点map的实现

2012-08-21 
jsp站点地图的实现? ? ?前段时间工作需要,需要构建一个面包屑导航,想到了了.net下的站点地图、面包屑导航控

jsp站点地图的实现

? ? ?前段时间工作需要,需要构建一个面包屑导航,想到了了.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&amp;topMenu=index"><!--个人中心 --><node  title="个人资料" path="/pages/stu/user/personal_data.jsp" href="leftNav.do?method=personalData&amp;type=data&amp;leftMenu=userInfo" >    </node>    <node  title="修改头像" path="/pages/stu/user/change_avatar.jsp" href="leftNav.do?method=personalData&amp;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&amp;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(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)+"--&gt;");}else{content.append("<a href='"+a+"'>"+titles.get(i)+"</a>--&gt;");}}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<Element> it = root.elementIterator();while(it.hasNext()){put2Map(it.next(),node,map);}}//将xml中读取的Element转换成自定义的节点对象:SiteMapNodepublic 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; 否则为trueprivate String fullPath;//本节点在节点树中的路径 = 父节点的全路径+本节点的pathprivate SiteMapNode parent;//父节点private List<SiteMapNode> children = new ArrayList<SiteMapNode>();//子节点集合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<SiteMapNode> getChildren() {return children;}public void setChildren(List<SiteMapNode> 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关键在于调用

@Overridepublic 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;}
?

热点排行