`
阅读更多

因为要搞一个简单的权限系统,所以最近我进行了一些设计和实现。经过研究,根据业务需求,决定使用一个二级菜单和自定义标签来实现权限的控制。

 

首先来解决这款二级菜单,当然实现自己也肯定能实现,但是别人做好了自己就用吧。

其他技术你可以访问我的博客:http://cuisuqiang.iteye.com/

这个控件叫 chromemenu,官方网站是http://www.dynamicdrive.com/,当然我的附件里面已经带了一个,你可以直接下载看一下。

 

使用很简单,就是几个层和超链接,正好我可以控制层和超链接的显示来实现权限控制。

我们来定义一个标签web-html.tld:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  3. "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
  4. <taglib>
  5. <tlib-version>1.0</tlib-version>
  6. <jsp-version>1.2</jsp-version>
  7. <short-name>html</short-name>
  8. <tag>
  9. <name>resourceUrl</name>
  10. <tag-class>com.nms.taglib.ResourceUrl</tag-class>
  11. <body-content>JSP</body-content>
  12. <attribute>
  13. <name>key</name>
  14. <rtexprvalue>true</rtexprvalue>
  15. </attribute>
  16. <attribute>
  17. <name>href</name>
  18. <rtexprvalue>true</rtexprvalue>
  19. </attribute>
  20. <attribute>
  21. <name>rel</name>
  22. <rtexprvalue>true</rtexprvalue>
  23. </attribute>
  24. <attribute>
  25. <name>note</name>
  26. <rtexprvalue>true</rtexprvalue>
  27. </attribute>
  28. <attribute>
  29. <name>isModule</name>
  30. <rtexprvalue>true</rtexprvalue>
  31. </attribute>
  32. </tag>
  33. </taglib>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
	"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>html</short-name>
	<tag>
		<name>resourceUrl</name>
		<tag-class>com.nms.taglib.ResourceUrl</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>key</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>href</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>rel</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>note</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>isModule</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
</taglib>

在web.xml中配置一下:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <!-- 自定义标签 开始 -->
  7. <jsp-config>
  8. <taglib>
  9. <taglib-uri>/tld/web-html</taglib-uri>
  10. <taglib-location>
  11. /WEB-INF/tlds/web-html.tld
  12. </taglib-location>
  13. </taglib>
  14. </jsp-config>
  15. <!-- 自定义标签 结束 -->
  16. <welcome-file-list>
  17. <welcome-file>index.jsp</welcome-file>
  18. </welcome-file-list>
  19. </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">
	<!-- 自定义标签 开始 -->
	<jsp-config>
		<taglib>
			<taglib-uri>/tld/web-html</taglib-uri>
			<taglib-location>
				/WEB-INF/tlds/web-html.tld
			</taglib-location>
		</taglib>
	</jsp-config>
	<!-- 自定义标签 结束 -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

看一下实现类:

Java代码 复制代码 收藏代码
  1. package com.nms.taglib;
  2. import java.io.IOException;
  3. import javax.servlet.jsp.JspException;
  4. import javax.servlet.jsp.JspTagException;
  5. import javax.servlet.jsp.tagext.BodyTagSupport;
  6. /**
  7. * @说明 菜单生成
  8. * @author 崔素强
  9. */
  10. @SuppressWarnings("serial")
  11. public class ResourceUrl extends BodyTagSupport {
  12. @Override
  13. public int doStartTag() throws JspException {
  14. try {
  15. // 从开放的接口取得是否允许Key资源的输出
  16. boolean isCheck = true;
  17. if(isCheck){
  18. StringBuffer results = new StringBuffer("");
  19. if("true".equals(isModule)){
  20. results.append("<li>");
  21. }
  22. results.append("<a ");
  23. if (href != null) {
  24. results.append(" href=\"");
  25. results.append(href);
  26. results.append("\"");
  27. }
  28. if (rel != null) {
  29. results.append(" rel=\"");
  30. results.append(rel);
  31. results.append("\"");
  32. }
  33. results.append(">");
  34. results.append(note);
  35. results.append("</a>");
  36. if("true".equals(isModule)){
  37. results.append("</li>");
  38. }
  39. pageContext.getOut().write(results.toString());
  40. }
  41. } catch (IOException ex) {
  42. throw new JspTagException("错误");
  43. }
  44. return EVAL_BODY_INCLUDE;
  45. }
  46. @Override
  47. public int doEndTag() throws JspException {
  48. return EVAL_PAGE;
  49. }
  50. //权限验证标记
  51. protected String key;
  52. // 实际连接地址
  53. protected String href;
  54. // 对应的下拉板块
  55. protected String rel;
  56. // 是否是总标记
  57. protected String isModule;
  58. // 文字
  59. protected String note;
  60. public String getKey() {
  61. return key;
  62. }
  63. public void setKey(String key) {
  64. this.key = key;
  65. }
  66. public String getHref() {
  67. return href;
  68. }
  69. public void setHref(String href) {
  70. this.href = href;
  71. }
  72. public String getRel() {
  73. return rel;
  74. }
  75. public void setRel(String rel) {
  76. this.rel = rel;
  77. }
  78. public String getIsModule() {
  79. return isModule;
  80. }
  81. public void setIsModule(String isModule) {
  82. this.isModule = isModule;
  83. }
  84. public String getNote() {
  85. return note;
  86. }
  87. public void setNote(String note) {
  88. this.note = note;
  89. }
  90. /**
  91. * doStartTag()方法是遇到标签开始时会呼叫的方法,其合法的返回值是EVAL_BODY_INCLUDE与SKIP_BODY,前者表示将显示标签间的文字,后者表示不显示标签间的文字
  92. * doEndTag()方法是在遇到标签结束时呼叫的方法,其合法的返回值是EVAL_PAGE与SKIP_PAGE,前者表示处理完标签后继续执行以下的JSP网页,后者是表示不处理接下来的JSP网页
  93. * doAfterBody(),这个方法是在显示完标签间文字之后呼叫的,其返回值有EVAL_BODY_AGAIN与SKIP_BODY,前者会再显示一次标签间的文字,后者则继续执行标签处理的下一步
  94. * EVAL_BODY_INCLUDE:把Body读入存在的输出流中,doStartTag()函数可用
  95. * EVAL_PAGE:继续处理页面,doEndTag()函数可用
  96. * SKIP_BODY:忽略对Body的处理,doStartTag()和doAfterBody()函数可用
  97. * SKIP_PAGE:忽略对余下页面的处理,doEndTag()函数可用
  98. * EVAL_BODY_BUFFERED:申请缓冲区,由setBodyContent()函数得到的BodyContent对象来处理tag的body,如果类实现了BodyTag,那么doStartTag()可用,否则非法
  99. * EVAL_BODY_AGAIN:请求继续处理body,返回自doAfterBody(),这个返回值在你制作循环tag的时候是很有用的
  100. * 预定的处理顺序是:doStartTag()返回SKIP_BODY,doAfterBodyTag()返回SKIP_BODY,doEndTag()返回EVAL_PAGE
  101. * 如果继承了TagSupport之后,如果没有改写任何的方法,标签处理的执行顺序是:doStartTag() ->不显示文字
  102. * ->doEndTag()->执行接下来的网页 如果您改写了doStartTag(),则必须指定返回值,
  103. * 如果指定了EVAL_BODY_INCLUDE,则执行顺序是:doStartTag()->显示文字->doAfterBodyTag()->doEndTag()->执行下面的网页
  104. */
  105. }
package com.nms.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * @说明 菜单生成
 * @author 崔素强
 */
@SuppressWarnings("serial")
public class ResourceUrl extends BodyTagSupport {
	@Override
	public int doStartTag() throws JspException {
		try {
			// 从开放的接口取得是否允许Key资源的输出
			boolean isCheck = true;
			if(isCheck){
				StringBuffer results = new StringBuffer("");
				if("true".equals(isModule)){
					results.append("<li>");
				}
				results.append("<a ");
				if (href != null) {
					results.append(" href=\"");
					results.append(href);
					results.append("\"");
				}
				if (rel != null) {
					results.append(" rel=\"");
					results.append(rel);
					results.append("\"");
				}
				results.append(">");
				results.append(note);
				results.append("</a>");
				if("true".equals(isModule)){
					results.append("</li>");
				}
				pageContext.getOut().write(results.toString());
			}
		} catch (IOException ex) {
			throw new JspTagException("错误");
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
	//权限验证标记
	protected String key;
	// 实际连接地址
	protected String href;
	// 对应的下拉板块
	protected String rel;
	// 是否是总标记
	protected String isModule;
	// 文字
	protected String note;
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getIsModule() {
		return isModule;
	}

	public void setIsModule(String isModule) {
		this.isModule = isModule;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}
	
	
	/**
	 * doStartTag()方法是遇到标签开始时会呼叫的方法,其合法的返回值是EVAL_BODY_INCLUDE与SKIP_BODY,前者表示将显示标签间的文字,后者表示不显示标签间的文字
	 * doEndTag()方法是在遇到标签结束时呼叫的方法,其合法的返回值是EVAL_PAGE与SKIP_PAGE,前者表示处理完标签后继续执行以下的JSP网页,后者是表示不处理接下来的JSP网页
	 * doAfterBody(),这个方法是在显示完标签间文字之后呼叫的,其返回值有EVAL_BODY_AGAIN与SKIP_BODY,前者会再显示一次标签间的文字,后者则继续执行标签处理的下一步
	 * EVAL_BODY_INCLUDE:把Body读入存在的输出流中,doStartTag()函数可用
	 * EVAL_PAGE:继续处理页面,doEndTag()函数可用
	 * SKIP_BODY:忽略对Body的处理,doStartTag()和doAfterBody()函数可用
	 * SKIP_PAGE:忽略对余下页面的处理,doEndTag()函数可用
	 * EVAL_BODY_BUFFERED:申请缓冲区,由setBodyContent()函数得到的BodyContent对象来处理tag的body,如果类实现了BodyTag,那么doStartTag()可用,否则非法
	 * EVAL_BODY_AGAIN:请求继续处理body,返回自doAfterBody(),这个返回值在你制作循环tag的时候是很有用的
	 * 预定的处理顺序是:doStartTag()返回SKIP_BODY,doAfterBodyTag()返回SKIP_BODY,doEndTag()返回EVAL_PAGE
	 * 如果继承了TagSupport之后,如果没有改写任何的方法,标签处理的执行顺序是:doStartTag() ->不显示文字
	 * ->doEndTag()->执行接下来的网页 如果您改写了doStartTag(),则必须指定返回值,
	 * 如果指定了EVAL_BODY_INCLUDE,则执行顺序是:doStartTag()->显示文字->doAfterBodyTag()->doEndTag()->执行下面的网页
	 */
}

你要关注这行代码:

Java代码 复制代码 收藏代码
  1. boolean isCheck = true;
boolean isCheck = true;

在使用时,你要根据 KEY 去判断是否显示某个菜单,具体实现就看你的了。

 

然后我们在JSP页面中进行使用:

Java代码 复制代码 收藏代码
  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="/tld/web-html" prefix="html"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>菜单示例</title>
  12. <link rel="stylesheet" type="text/css" href="chromestyle.css" />
  13. <script type="text/javascript" src="chrome.js"></script>
  14. </head>
  15. <body>
  16. <div class="chromestyle" id="chromemenu">
  17. <ul>
  18. <html:resourceUrl href="#" key="" note="Model001" isModule="true"></html:resourceUrl>
  19. <html:resourceUrl href="#" key="" note="Model002" isModule="true"></html:resourceUrl>
  20. <html:resourceUrl href="#" key="" note="Model003" isModule="true" rel="dropmenu1"></html:resourceUrl>
  21. <html:resourceUrl href="#" key="" note="Model004" isModule="true" rel="dropmenu2"></html:resourceUrl>
  22. <html:resourceUrl href="#" key="" note="Model005" isModule="true" rel="dropmenu3"></html:resourceUrl>
  23. <html:resourceUrl href="#" key="" note="Model006" isModule="true" rel="dropmenu4"></html:resourceUrl>
  24. </ul>
  25. </div>
  26. <!--1st drop down menu -->
  27. <div id="dropmenu1" class="dropmenudiv">
  28. <html:resourceUrl href="#" key="" note="a1"></html:resourceUrl>
  29. <html:resourceUrl href="#" key="" note="a2"></html:resourceUrl>
  30. <html:resourceUrl href="#" key="" note="a3"></html:resourceUrl>
  31. </div>
  32. <!--2nd drop down menu -->
  33. <div id="dropmenu2" class="dropmenudiv" style="width: 150px;">
  34. <html:resourceUrl href="#" key="" note="b1"></html:resourceUrl>
  35. <html:resourceUrl href="#" key="" note="b2"></html:resourceUrl>
  36. </div>
  37. <!--3rd drop down menu -->
  38. <div id="dropmenu3" class="dropmenudiv" style="width: 150px;">
  39. <html:resourceUrl href="#" key="" note="c1"></html:resourceUrl>
  40. <html:resourceUrl href="#" key="" note="c2"></html:resourceUrl>
  41. <html:resourceUrl href="#" key="" note="c3"></html:resourceUrl>
  42. <html:resourceUrl href="#" key="" note="c4"></html:resourceUrl>
  43. </div>
  44. <!--4rd drop down menu -->
  45. <div id="dropmenu4" class="dropmenudiv" style="width: 150px;">
  46. <html:resourceUrl href="#" key="" note="d1"></html:resourceUrl>
  47. <html:resourceUrl href="#" key="" note="d2"></html:resourceUrl>
  48. <html:resourceUrl href="#" key="" note="d3"></html:resourceUrl>
  49. <html:resourceUrl href="#" key="" note="d4"></html:resourceUrl>
  50. <html:resourceUrl href="#" key="" note="d5"></html:resourceUrl>
  51. <html:resourceUrl href="#" key="" note="d6"></html:resourceUrl>
  52. <html:resourceUrl href="#" key="" note="d7"></html:resourceUrl>
  53. </div>
  54. <script type="text/javascript">
  55. cssdropdown.startchrome("chromemenu");
  56. </script>
  57. </body>
  58. </html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/tld/web-html" prefix="html"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <title>菜单示例</title>
	<link rel="stylesheet" type="text/css" href="chromestyle.css" />
	<script type="text/javascript" src="chrome.js"></script>
  </head>  
  <body>
<div class="chromestyle" id="chromemenu">
<ul>
<html:resourceUrl href="#" key="" note="Model001" isModule="true"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="Model002" isModule="true"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="Model003" isModule="true" rel="dropmenu1"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="Model004" isModule="true" rel="dropmenu2"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="Model005" isModule="true" rel="dropmenu3"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="Model006" isModule="true" rel="dropmenu4"></html:resourceUrl>
</ul>
</div>
<!--1st drop down menu -->                                                   
<div id="dropmenu1" class="dropmenudiv">
<html:resourceUrl href="#" key="" note="a1"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="a2"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="a3"></html:resourceUrl>
</div>

<!--2nd drop down menu -->                                                
<div id="dropmenu2" class="dropmenudiv" style="width: 150px;">
<html:resourceUrl href="#" key="" note="b1"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="b2"></html:resourceUrl>
</div>
<!--3rd drop down menu -->                                                   
<div id="dropmenu3" class="dropmenudiv" style="width: 150px;">
<html:resourceUrl href="#" key="" note="c1"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="c2"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="c3"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="c4"></html:resourceUrl>
</div>
<!--4rd drop down menu -->                                                   
<div id="dropmenu4" class="dropmenudiv" style="width: 150px;">
<html:resourceUrl href="#" key="" note="d1"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="d2"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="d3"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="d4"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="d5"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="d6"></html:resourceUrl>
<html:resourceUrl href="#" key="" note="d7"></html:resourceUrl>
</div>
<script type="text/javascript">
cssdropdown.startchrome("chromemenu");
</script>
  </body>
</html>

如果是一级菜单,那么要设置:

Java代码 复制代码 收藏代码
  1. isModule="true"
isModule="true"

 

运行你看到了所有的菜单,你可以自己控制显示那个菜单。

 

这个权限就是很简单,就是根据某个Key去判断能否访问那个资源。当然在实际中应该是你请求的连接,这样再定义一个Filter去过滤所有请求,就能实现不能通过地址栏直接访问该资源。

本文出处:http://cuisuqiang.iteye.com/如果你喜欢请到ITeye与我交流。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics