使用jsp自定义标签控制内容是否显示

1.自定义标签类编写:

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

import xxx.xxx.util.SessionUtil;
import xxx.xxx.dao.UserDao;

/**
 * 权限控制标签
 * @author zejun.zhang
 *
 */
public class RightTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	
	private String rightCode;
	
	private UserDao userDao = new UserDao();
	
	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		if (userDao.hasRight(SessionUtil.getCompanyId(request),
                         SessionUtil.getUserId(request), rightCode)) {
			return BodyTagSupport.EVAL_BODY_INCLUDE;
		} else {
			return BodyTagSupport.SKIP_BODY;
		}
	}
	
	@Override
	public int doEndTag() throws JspException {
		return BodyTagSupport.EVAL_BODY_INCLUDE;
	}

	public void setRightCode(String rightCode) {
		this.rightCode = rightCode;
	}

	public String getRightCode() {
		return rightCode;
	}
	
	
}

2.编写tld文件customTag.tld,放到WEB-INF目录下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"   
    "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>custom</short-name>
    <uri>http://www.xxx.com/tag</uri>
    <tag>
        <name>hasRight</name>
        <tag-class>xxx.xxx.tag.RightTag</tag-class>
        <attribute>
            <name>rightCode</name>
            <required>true</required>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</taglib>

3.JSP页面引入标签:

<%@ taglib prefix="r" uri="http://www.xxx.com/tag"%>

4.使用标签:

<r:hasRight rightCode="check">
	  <button onclick="check();">审核</button>
</r:hasRight>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值