java面包屑导航制作

做了个简单的面包屑导航功能,比如页面上大家经常看到的这种导航: 
 
您所在的位置:音乐社区-->用户模块-->用户注册 
 
我做成了标签的形式,利用jom4j来解析xml文件:
 
核心类SiteMapTag.java: 


Java代码  
 

package com.market.common;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 站点导航标签的实现 
 * <p>
 * Title: SiteMapTag.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * 
 * @author  fjs
 * @version   Sep 15, 2012
 */
public class SiteMapTag extends TagSupport {

	private static final long serialVersionUID = -3531938467909884528L;
	private String currentFilePath;
	private Element target;
	
	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
		currentFilePath = request.getRequestURI().replaceFirst(request.getContextPath(), "");
		try {
			Element root = (Element)pageContext.getServletContext().getAttribute("webSiteMapSet");
			if(root==null){
				SAXReader reader = new SAXReader();
				InputStream inputStream = SiteMapTag.class.getClassLoader().getResourceAsStream("sitemap.xml");
				Document document = reader.read(inputStream);
				root = document.getRootElement();
				pageContext.getServletContext().setAttribute("webSiteMapSet", root);
			}
			parseParent(root);
			StringBuffer content = new StringBuffer("");
			List<String> titles = new ArrayList<String>();
			List<String> hrefs = new ArrayList<String>();
			
			while(target!=null){
				Attribute attTitle = target.attribute("title");
				if(attTitle!=null){
					titles.add(attTitle.getText());
				}
				
				Attribute attHref = target.attribute("href");
				if(attHref!=null){
					hrefs.add(attHref.getText());
				}else{
					hrefs.add("");
				}
				
				target = target.getParent();
			}
			
			for (int i = titles.size()-1; i >=0; i--) {
				String href = hrefs.get(i);
				if(href.equals("")){
					content.append(titles.get(i)+" - ");
				}else{
					content.append("<a href='"+href+"'>"+titles.get(i)+"</a> - ");
				}
			}
			if(content.length()>0){
				this.pageContext.getOut().println(content.delete(content.length()-3, content.length()));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspException(e);
		}
		
		
		return super.doStartTag();
	}

	private void parseParent(Element parent){
		Iterator<Element> it = parent.elementIterator();
		while(it.hasNext()){
			Element temp = it.next();
			Attribute attr = temp.attribute("path");
			if(attr!=null){
				if(attr.getText().equals(currentFilePath)){
					target = temp;
					return;
				}
			}
			parseParent(temp);
		}
	}
	
}



 sitemap.tld文件(存放在WEB-INF\tlds下):
Xml代码  
 

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <description>站点导航标签</description>
	<display-name>myTaglib siteMap</display-name>
	<tlib-version>1.1</tlib-version>    
    <short-name>tagUtil</short-name>
    <uri>http://tag.market.org/jsp/tagutil/sitemap</uri>

    <tag>
		<description>面包屑标签</description>
		<name>siteMap</name>
		<tag-class>com.market.common.SiteMapTag</tag-class>
		<body-content>empty</body-content>
    </tag>
    
</taglib>


 
 在项目的src下面添加sitemap.xml文件:
 
Xml代码 
<?xml version="1.0" encoding="UTF-8"?>  

<!-- 配置文件说明, sitemap根节点,title是页面上显示的内容,  
   href是超链接,path是该请求页面的物理路径 
--> 
<sitemap title="音乐社区" href="index.action">  
    <node  title="用户模块">  
        <node path="/WEB-INF/pages/user/register_view.jsp" href="user/registerView.action"  title="用户注册" />  
    </node> 
</sitemap>  
  
 
要使用的jsp页面加入标签指令:

<%@taglib uri="http://tag.forever.org/jsp/tagutil/sitemap" prefix="tagUtil" %> 

 

如果运行报找不到 tld文件就在web.xml的web-app节点下加入

<jsp-config>
  <taglib>
   <taglib-uri>/sitemap.tld</taglib-uri>
   <taglib-location>/WEB-INF/tlds/sitemap.tld</taglib-location>
  </taglib>
 </jsp-config>

那在jsp的标签指令需要改为  <%@taglib uri="/sitemap.tld" prefix="tagUtil" %>


 在想放的位置处加上标签:
 
您所在的位置:<tagUtil:siteMap/> 就可以出现面包屑导航了。
 
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐韬科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值