码迷,mamicode.com
首页 > 其他好文 > 详细

自定义标签

时间:2015-06-10 11:40:40      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

1.首先在web.xml中要引入标签tld: /WEB-INF/jspSecurity.tld /WEB-INF/jspSecurity.tld

2.jspSecurity.tld内容:

 

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
 PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>custTag</shortname>
  <uri/>
  <tag>
    <name>JspSecurity</name>
    <tagclass>util.tag.JspSecurityTag</tagclass>
    <info>
       JspSecurityTag
    </info>
    <attribute>
       <name>elementName</name>
       <required>true</required>
       <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
 
  <tag>
    <name>JspSecurityByRole</name>
    <tagclass>util.tag.JspSecurityTagByRole</tagclass>
    <info>
       JspSecurityTagByRole
    </info>
    <attribute>
       <name>resourceName</name>
       <required>true</required>
       <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>

3.类

import java.math.BigDecimal;
import java.util.Hashtable;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class JspSecurityTagByRole extends BodyTagSupport {

    private static final long serialVersionUID = -4745786903307048282L;

    // 页面元素(角色控制资源)的名称
    private String resourceName;

    public String getResourceName() {
        return resourceName;
    }

    /**
     * 设置页面元素(角色控制资源)的名称
     * @param str
     */

    public void setResourceName(String str) {
        this.resourceName = str;
    }

    // 判断resourceName是否存在在用户的session中。如果是admin都能看见,其他只能看到自己的权限。如果admin分配的管理员看到的权限少,那么这个管理员能下发的也少了
    // TODO 可以考虑是超级管理员就都可以看见所有链接
    private boolean checkUserRoleId(BigDecimal str) {
        try {
            if ("admin".equals(this.pageContext.getSession().getAttribute(GlobalConstant.LOGON_ID))
                    || (str.intValue() - 1 == 1)) {
                return true;
            }
            Hashtable roleIdTable = (Hashtable) this.pageContext.getSession().getAttribute(
                    "ROLE_ID_TABLE");
            if (roleIdTable.get(str) == null) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    // 校验用户的角色,是否可以增删改查,如果是超级管理员0,如果是普通操作员1,如果系统查询员2
    // str 2是限制查询员的标签
    private boolean checkUserRole(BigDecimal str) {
        // 如果是系统查询员遇到增删改按钮就不能显示按钮
        if ((str.intValue() - 1 == 1)
                && str.intValue() == ((BigDecimal) (this.pageContext.getSession()
                        .getAttribute(GlobalConstant.USER_ROLE))).intValue()) {
            return false;
        }
        return true;
    }

    //
    /**
     * 通过角色资源描述文件决定该页面标签是否显示
     */
    public int doAfterBody() {
        // System.out.println("-------------start---------------");
        try {
            // System.out.println(this.getResourceName());
            // System.out.println(this.checkUserRoleId(new BigDecimal(this.getResourceName())));
            // System.out.println(this.checkUserRole(new BigDecimal(this.getResourceName())));
            BigDecimal rn = new BigDecimal(this.getResourceName());
            if (this.checkUserRoleId(rn) && this.checkUserRole(rn) && bodyContent != null) {
                JspWriter out = bodyContent.getEnclosingWriter();
                bodyContent.writeOut(out);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        // System.out.println("-------------end---------------");
        return SKIP_BODY;

    }

}

4.页面

<custTag:JspSecurityByRole resourceName="902030302">
     <input type="submit" value="添 加" class="bt_add" onclick="addGroup();" />
</custTag:JspSecurityByRole>

5.页面也要引入 <%@ taglib uri="/WEB-INF/jspSecurity.tld" prefix="custTag"%>

自定义标签

标签:

原文地址:http://www.cnblogs.com/lele88lala/p/4565283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!