Jsp 自定义标签 三 综合示例

现在实现如下功能:根据用户权限精确控制页面元素。也就是即使是同一个页面,不通用户看到的页面元素也不一样,页面元素比如页面按钮。实现代码如下:</span>

1.  自定义页面标签


<?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>Permission 1.0 library</description>
  <display-name>PMS</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>pms</short-name>
  <uri>http://www.e-tong.cn/tags/pms</uri>

<!--  <validator>-->
<!--    <description>-->
<!--        Provides validation features for PMS tags.-->
<!--    </description>-->
<!--    <validator-class>-->
<!--		com.etong.common.web.safe.HasPermissionTag-->
<!--    </validator-class>-->
<!--  </validator>-->

  <tag>
    <description>
	将当前已授权的用户信息(UserInfo实例)通过指定变量添加到页面上下文中。
    </description>
    <name>user</name>
    <tag-class>com.etong.common.web.safe.UserInfoTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <description>用户信息对象(UserInfo实例)的变量名称</description>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
	此标签仅仅是一个标记,作为其它权限判断标签的父容器。
    </description>
    <name>switchPermission</name>
    <tag-class>com.etong.common.web.safe.SwitchPermissionTag</tag-class>
    <body-content>JSP</body-content>
  </tag>
 
  <tag>
    <description>
	只有当前用户拥有指定授权,才会执行该标签体内的代码,此权限通过
	描述符(value属性)和应用系统代码(appCode,默认为当前应用系统代码)
	指定。如果设置全局权限为true,则应用系统代码将被忽略。
    </description>
    <name>hasPermission</name>
    <tag-class>com.etong.common.web.safe.HasPermissionTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>权限描述符</description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>应用系统代码,默认为当前应用系统</description>
        <name>appCode</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<description>是否为全局权限,默认为非全局权限</description>
    	<name>global</name>
    	<required>false</required>
    	<rtexprvalue>false</rtexprvalue>
 		<type>boolean</type>
    </attribute>
  </tag>
  
  <tag>
    <description>
	通常配合HasPermission标签使用,与HasPermission相反,如果当前用户没有
	指定授权,就会执行该标签体内的代码,此权限通过描述符(value属性)和应
	用系统代码(appCode,默认为当前应用系统代码)指定。如果设置全局权限为
	true,则应用系统代码将被忽略。
    </description>
    <name>noPermission</name>
    <tag-class>com.etong.common.web.safe.NoPermissionTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>权限描述符</description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>应用系统代码,默认为当前应用系统</description>
        <name>appCode</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<description>是否为全局权限,默认为非全局权限</description>
    	<name>global</name>
    	<required>false</required>
    	<rtexprvalue>false</rtexprvalue>
 		<type>boolean</type>
    </attribute>
  </tag>
  
  <tag>
    <description>
	仅在当前用户拥有全部指定的授权,才会执行该标签体内的代码,此权限通过描
	述符(value属性)和应用系统代码(appCode,默认为当前应用系统代码)指
	定。如果设置全局权限为true,则应用系统代码将被忽略。
    </description>
    <name>havePermissions</name>
    <tag-class>com.etong.common.web.safe.HavePermissionsTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>权限描述符</description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>应用系统代码,默认为当前应用系统</description>
        <name>appCode</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<description>是否为全局权限,默认为非全局权限</description>
    	<name>global</name>
    	<required>false</required>
    	<rtexprvalue>false</rtexprvalue>
 		<type>boolean</type>
    </attribute>
  </tag>
  
  <tag>
    <description>
	仅在当前用户拥有任意一项或多项授权时,才会执行该标签体内的代码,此权限通过描
	述符(value属性)和应用系统代码(appCode,默认为当前应用系统代码)指
	定。如果设置全局权限为true,则应用系统代码将被忽略。
    </description>
    <name>hasAnyPermission</name>
    <tag-class>com.etong.common.web.safe.HasAnyPermissionTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>权限描述符</description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>应用系统代码,默认为当前应用系统</description>
        <name>appCode</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<description>是否为全局权限,默认为非全局权限</description>
    	<name>global</name>
    	<required>false</required>
    	<rtexprvalue>false</rtexprvalue>
 		<type>boolean</type>
    </attribute>
  </tag>
  
  <tag>
    <description>
	此标签是switchPermission的子标签,和其他权限判断标签结合使用。
	仅当其它权限判断条件均不成立(标签体内代码未执行)时,此标签体
	内的代码将被执行。
    </description>
    <name>otherwise</name>
    <tag-class>com.etong.common.web.safe.OtherwiseTag</tag-class>
    <body-content>JSP</body-content>
  </tag>
   
</taglib>


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

import com.etong.common.system.bean.UserInfo;
import com.etong.common.util.StringUtil;
import com.etong.common.web.util.WebUtils;

/**
 * <p>Title: UAWP - HavePermissionsTag.java</p>
 * <p>Description: </p>
 * <p>芜湖易通信息技术有限公司 (http://www.e-tong.cn),
 * All rights reserved.</p>
 * <p>Created on 2011-9-10 下午07:39:29.</p>
 *
 * @author <a href="mailto:ApecHsing@gmail.com">Jack</a>
 * @version 1.0
 */
public class HavePermissionsTag extends TagSupport {

	private static final long serialVersionUID = -2673706295137168830L;

	private String descriptors;
	
	private String appCode;
	
	private boolean global;

	/**
	 * 设置指定的权限描述符,不同的描述符之间以半角逗号分隔。
	 * @param desriptors the desriptors to set
	 */
	public final void setValue(String descriptors) {
		this.descriptors = descriptors;
	}

	/**
	 * 设置指定的应用系统代码,缺省为当前应用系统。
	 * @param appCode the appCode to set
	 */
	public final void setAppCode(String appCode) {
		this.appCode = appCode;
	}
	
	/**
	 * 设置全局权限,默认为false。
	 * @param global the global to set
	 */
	public final void setGlobal(boolean global) {
		this.global = global;
	}

	@Override
	public int doStartTag() throws JspException {

		UserInfo ui = WebUtils.getUserInfo((HttpServletRequest)pageContext.getRequest());
		if(ui == null) {
			return SKIP_BODY;
		}
		String[] descriptors = this.descriptors.split(",");
		for(int i = 0; i < descriptors.length; i++) {
			descriptors[i] = descriptors[i].trim();
		}
		if(global) {
			return ui.hasGlobalPermissions(descriptors) ? EVAL_BODY_INCLUDE : SKIP_BODY;
		}
		if(StringUtil.isEmpty(appCode)) {
			return ui.hasPermission(descriptors) ? EVAL_BODY_INCLUDE : SKIP_BODY;
		}
		return ui.hasPermission(appCode, descriptors) ? EVAL_BODY_INCLUDE : SKIP_BODY;
	}
}

jsp

<table width="100%" height="30" align="center"  border="0" cellspacing="0" cellpadding="0" style="margin-top:10px;">
	  	<tr>
	  		<td>		
				<pms:hasPermission value="menu:create"><a class="easyui-linkbutton" href="javascript:void(0)" plain="true" iconCls="icon-add" onClick="add();return false;">新建</a></pms:hasPermission>
				<pms:hasPermission value="menu:modify"><a class="easyui-linkbutton" id="edit" href="javascript:void(0)" plain="true" iconCls="icon-edit" onClick="edit();return false;">修改</a></pms:hasPermission>
				<pms:hasPermission value="menu:remove"><a class="easyui-linkbutton" id="del" href="javascript:void(0)" plain="true" iconCls="icon-cancel" onClick="del();return false;">删除</a></pms:hasPermission>
	  		</td>
	  	</tr>
	  </table>

我们可以看到 在实现类中 有个 doStartTag,其实还有个doEndTag。除了继承于TagSupport,还可以继承与 BodyTagSupport。这些内容,可以Google查阅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值