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>