1. 程式人生 > >jsp tag 自定義標籤實現按鈕的顯示

jsp tag 自定義標籤實現按鈕的顯示

前些天在一個專案中做了一個最基本的許可權管理(標準的許可權結構,使用者-角色-選單),後來需要精確地控制到按鈕就想到了使用自定義標籤(jsp tag)來處理按鈕的顯示與隱藏。

下面是我參考網上的資料自己寫的一個自定義標籤的demo:

首先自定義標籤類:

package com.vanfon.p2p.admin.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.vanfon.p2p.entity.Admin;

public class PermissionTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4592227792811389132L;


	@Override
	public int doStartTag() throws JspException {
		boolean result = false;
		HttpServletRequest request = (HttpServletRequest) this.pageContext
				.getRequest();// 通過成員變數獲取HttpServletRequest物件
		Admin admin = (Admin) request.getSession().getAttribute("admin");//獲取登入到系統的使用者
		if(admin!=null&&"1".equals(String.valueOf(admin.getIfsuper()))){
			result = true;
		}
		return result? EVAL_BODY_INCLUDE : SKIP_BODY;//EVAL_BODY_INCLUDE代表執行自定義標籤中的內容,SKIP_BODY代表不執行自定義標籤中的內容。
	}
}
在WEB-INF下新建資料夾tlds,然後建立檔案shiros.tld
<?xml version="1.0" encoding="UTF-8" ?>

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

	<description>p2p permission taglib</description>
	<display-name>permission taglib</display-name>
	<tlib-version>1.0</tlib-version>
	<short-name>p2p_admin</short-name>
	<!-- 在taglib.jsp檔案中加上如下內容,<%@ taglib uri="<span style="font-family: Arial, Helvetica, sans-serif;">http://vanfon.p2p.cn</span>" 
		prefix="p2p"%> uri中的值必須與標籤<uri></uri>中的值一致,prefix名稱隨便取,取完後JSP那端的字首名必須與這一致。 -->
	<uri>http://vanfon.p2p.cn/</uri>

	<tag>
		<description>許可權校驗標籤,有許可權就顯示標籤體的內容,否則不顯示</description>
		<name>permission</name><!-- 裡面的內容是JSP檔案中標籤裡面的<p2p:permission >permission -->
		<tag-class>com.vanfon.p2p.admin.core.PermissionTag</tag-class><!-- JSP檔案中的自定義標籤裡面的值會傳到PermissionTag類接受,PermissionTag類接受到的值決定是否要顯示JSP檔案中自定義標籤的內容。 -->
		<body-content>JSP</body-content><!-- 裡面的值代表<p2p:permission>只不能用在JSP中。 -->
	</tag>
</taglib>

web,xml中加入:

<jsp-config>     
            <taglib>
                <taglib-uri>http://vanfon.p2p.cn/</taglib-uri>
                <taglib-location>/WEB-INF/tlds/shiros.tld</taglib-location> 
            </taglib>    
  </jsp-config>


最後在jsp中引入標籤:
<%@ taglib prefix="p2p" uri="http://vanfon.p2p.cn/" %>

使用標籤:
<p2p:permission><a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openCreateDialog();">新增</a></p2p:permission>
			<p2p:permission ><a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openUpdateDialog();">編輯</a></p2p:permission>
			<p2p:permission ><a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="del();">刪除</a></p2p:permission>



然後就ok了,以管理員的身份(ifsuper=1)登入系統表示可見,以普通使用者的身份(ifsuper!=1)登入系統按鈕將不可見,以上就是jsp tag自定義標籤最簡單的用法,僅僅只做了是否是管理員的判斷,其實完整的應該是對所有使用者進行一個通用的控制。

擴充套件

比如:

PermissionTag.java

public class PermissionTag extends TagSupport {
 
private String module;//屬性名必須與JSP自定義標籤的屬性名一樣
 
private String privilege;
 
public String getModule() {
return module;
}
 
public void setModule(String module) {
this.module = module;
}
 
public String getPrivilege() {
return privilege;
}
 
public void setPrivilege(String privilege) {
this.privilege = privilege;
}
 
@Override
public int doStartTag() throws JspException {
boolean result = false;
Employee employee = this.pageContext.getRequest();//通過成員變數獲取HttpServletRequest物件。
WebUtil.getEmployee((HttpServletRequest)this.pageContext.getRequest());//獲取登入到系統的員工
SystemPrivilege methodPrivilege = new SystemPrivilege(new SystemPrivilegePK(this.getModule(), this.getPrivilege()));
for(PrivilegeGroup group : employee.getGroups()) {
if(group.getPrivileges().contains(methodPrivilege)) {
result = true;
break;
}
}
//EVAL_BODY_INCLUDE代表執行自定義標籤中的內容,SKIP_BODY代表不執行自定義標籤中的內容。
return result? EVAL_BODY_INCLUDE : SKIP_BODY;
}
 
}

shiros.tld

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

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

	<description>p2p permission taglib</description>
	<display-name>permission taglib</display-name>
	<tlib-version>1.0</tlib-version>
	<short-name>p2p</short-name>
	<!-- 在taglib.jsp檔案中加上如下內容,<%@ taglib uri="<span style="font-family: Arial, Helvetica, sans-serif;">http://vanfon.p2p.cn/</span>" 
		prefix="p2p"%> uri中的值必須與標籤<uri></uri>中的值一致,prefix名稱隨便取,取完後JSP那端的字首名必須與這一致。 -->
	<uri>http://vanfon.p2p.cn/</uri>

	<tag>
		<description>許可權校驗標籤,有許可權就顯示標籤體的內容,否則不顯示</description>
		<name>permission</name><!-- 裡面的內容是JSP檔案中標籤裡面的<p2p:permission module="admin" privilege="delete">permission -->
		<tag-class>com.vanfon.p2p.admin.core.PermissionTag</tag-class><!-- JSP檔案中的自定義標籤裡面的值會傳到PermissionTag類接受,PermissionTag類接受到的值決定是否要顯示JSP檔案中自定義標籤的內容。 -->
		<body-content>JSP</body-content><!-- 裡面的值代表<p2p:permission module="admin" privilege="delete">只不能用在JSP中。 -->
		<attribute>
			<description></description>
			<name>module</name><!-- 裡面的值代表JSP檔案中自定義標籤 <p2p:permission module="admin" privilege="delete">中的module。-->
			<required>true</required><!-- 裡面的值代表在JSP檔案中敲入自定義標籤(<p2p:permission)的時候,自定義標籤裡面屬性名稱(module)不用手工去寫會自動顯示出來。 -->
			<rtexprvalue>false</rtexprvalue><!-- 裡面的值代表JSP檔案中自定義標籤的屬性的值不能用EL表示式(${})來表示,如果為true自定義標籤的屬性的值能用EL表示式(${})來表示。 -->
		</attribute>
		<attribute>
			<description></description>
			<name>privilege</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
	</tag>
</taglib>


jsp中用法:
<p2p:permission privilege="add" module="admin"><a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openCreateDialog();">新增</a></p2p:permission>

參考:http://blog.sina.com.cn/s/blog_a2de16f401016cpc.html