基于项目中前台页面需要根据当前用户的权限来决定是否显示超链接、按钮等
第一步:首先必须有自定义标签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>
以上两步完成后,就可以在页面中使用自定义的权限标签了。