做了個簡單的麵包屑導航功能,比如頁面上大家經常看到的這種導航:
您所在的位置:音樂社區-->使用者模組-->使用者註冊
我做成了標籤的形式,利用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;@Overridepublic 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/> 就可以出現麵包屑導航了。