自定义jsp标签

时间:2021-05-19 13:35:12

1.类:

package ===============================;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport; public class DCAclTag extends BodyTagSupport { private static final long serialVersionUID = 1L; /**
* 传入进来的值,权限判断需要的条见
*/
private int control;
private int permission; public int getControl() {
return control;
} public void setControl(int control) {
this.control = control;
} public int getPermission() {
return permission;
} public void setPermission(int permission) {
this.permission = permission;
} /**
* doStartTag方法,如果value为true,那么 就计算tagbody的值,否则不计算body的值。
*/
public int doStartTag() throws JspTagException {
if (PermissionUtil.checkPermission(permission, control)) {
return EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
} /**
* 覆盖doEndTag方法
*/
public int doEndTag() throws JspTagException {
try {
if (bodyContent != null) {
bodyContent.writeOut(bodyContent.getEnclosingWriter());
}
} catch (java.io.IOException e) {
throw new JspTagException("IO Error: " + e.getMessage());
}
return EVAL_PAGE;
} }

在WEB-INF下的配置文件dc.tdl

<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version> <short-name>dctag</short-name>
<uri>http://www.dc.com/tags</uri>
<description>
dctag
</description> <tag>
<name>acl</name>
<tag-class>com.dc.platform.ss.permission.DCAclTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>control</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>permission</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

web.xml下的配置:

    <taglib>
<taglib-uri>http://www.dc.com/tags</taglib-uri>
<taglib-location>/WEB-INF/dc.tld</taglib-location>
</taglib>

页面调用: 引入改自定义标签

<%@ taglib prefix="dc" uri="http://www.dc.com/tags" %>

<dc:acl permission="<%=11%>" control="<%=aclState %>">
<li><a class="edit" href="s?md=${module.sn}&ac=turnNotice&rowId={rowid}" target="dialog" width="500" height="220" title="确定要转入公告吗?"><span>转入公告</span></a></li>
<li class="line">line</li>
</dc:acl>