使用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目录下:




    1.0
    2.0
    custom
    http://www.xxx.com/tag
    
        hasRight
        xxx.xxx.tag.RightTag
        
            rightCode
            true
            java.lang.String
        
    

3.JSP页面引入标签:

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

4.使用标签:


	  

 

你可能感兴趣的:(Servlet/JSP)