根据用户权限使用自定义标签控制链接、按钮是否显示

                             基于项目中前台页面需要根据当前用户的权限来决定是否显示超链接、按钮等

第一步:首先必须有自定义标签handler,用于在JSP引擎解析自定义标签用的。

package com.yangsj.tag;

import javax.servlet.http.HttpSession;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

public class RightControlTag extends TagSupport 

{

/**

*/

private static final long serialVersionUID = -5847648182347653121L;

private boolean validate = true;

@Override

public int doStartTag() throws JspException

{

if (!this.validate)

{

return TagSupport.SKIP_BODY;

}

else

{

//这里进行鉴权     JSP引擎是否解析标签体的内容

HttpSession session = super.pageContext.getSession();

// if (鉴权成功)

// {

// return TagSupport.EVAL_BODY_INCLUDE;

// }

// else

// {

// return TagSupport.SKIP_BODY;

// }

String sessionId = session.getId();

System.out.println(sessionId);

return TagSupport.EVAL_BODY_INCLUDE;

}

}

public boolean isValidate()

{

return validate;

}

public void setValidate(boolean validate)

{

this.validate = validate;

}

}

第二步:在WEB-INF目录下添加名为rightcotrol.tld的tld文件。tld文件用于描述页面自定义标签对应的handler,标签属性等等。

<?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>*** right control library</description>

<display-name>test</display-name>

<tlib-version>1.1</tlib-version>

<short-name>test</short-name>

<uri>http://yangsj.com/jsp/jctl/rightcontrol</uri>

<tag>

<description>

The right control tag is used to determines tag body whether interpreted or not.

    </description>

<name>rightcontrol</name>

<tag-class>com.yangsj.tag.RightControlTag</tag-class>

<body-content>JSP</body-content>

<attribute>

<description>

The validate that determines whether or not the body content should be processed or shown.

        </description>

<name>validate</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

<type>boolean</type>

</attribute>

</tag>

</taglib>

以上两步完成后,就可以在页面中使用自定义的权限标签了。

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="test" uri="http://yangsj.com/jsp/jctl/rightcontrol" %>
 
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="<%=basePath%>/resources/jquery-1.7.1.js"></script>
 
<script type="text/javascript">
$(document).ready(function () {
var content = '<test:rightcontrol validate="false">123456</test:rightcontrol>';
 
$("#jstltest").append(content);
});
</script>
  </head>
  
  <body>
    This is my JSP page. <br>
    
    <div id="jstltest">
    123 <br />
    </div>
 
  </body>
</html>
 

猜你喜欢

转载自yangsj19870829.iteye.com/blog/1867554