Java自定义简单标签实例
程序员文章站
2023-12-19 10:51:58
下面将以权限的控制为例自定义一个标签:一、标签类型复制代码 代码如下:
<wxt:per uri="${pagecontext.request.contextpath }/privilege/list"></wxt:per>
步骤:
1.自定义一个类perssiontag 继承simpletagsupport(自定义标签一般都会继承这个类)
package cn.com.liveuc.privilege.tag;
import java.io.ioexception;
import java.util.arraylist;
import java.util.list;
import java.util.set;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.pagecontext;
import javax.servlet.jsp.tagext.simpletagsupport;
import cn.com.liveuc.privilege.model.privilege;
import cn.com.liveuc.privilege.model.resource;
import cn.com.liveuc.privilege.model.role;
import cn.com.liveuc.privilege.model.user;
/**
*
* @说明 自定义标签
*/
public class perssiontag extends simpletagsupport {
//自定义标签属性,用于标签传入参数
private string uri;
//接收标签传入的参数
public void seturi(string uri) {
this.uri = uri;
}
@override
public void dotag() throws jspexception, ioexception {
//获取用户登陆后保存的session
pagecontext page = (pagecontext) this.getjspcontext();
user user = (user) page.getsession().getattribute("login");
//如果用户登陆
if(user != null) {
//用户登陆判断用户权限
list<string> list = new arraylist<string>();
//获取用户的角色
set<role> role = user.getrole();
for(role r:role) {
//获取角色对应的权限
set<privilege> privilege = r.getprivilege();
for(privilege p:privilege) {
//获取权限对应的资源
set<resource> res = p.getresource();
for(resource re:res) {
list.add(re.geturi());
}
}
}
for(string ur:list) {
//判断用户的权限
if(ur.equals(uri)) {
this.getjspbody().invoke(null); //有权限输出标签体内容
}
}
}
}
}
2.在web-inf下创建tld文件描述标签。
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
version="2.0"
xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<description><![cdata["to make it easier to access dynamic data;
the apache struts framework includes a library of custom tags.
the tags interact with the framework's validation and internationalization features;
to ensure that input is correct and output is localized.
the struts tags can be used with jsp freemarker or velocity."]]></description>
<display-name>"struts tags"</display-name>
<tlib-version>2.2.3</tlib-version>
<short-name>s</short-name>
<uri>/wxt</uri>
<tag>
<name>per</name><!-- 标签名 -->
<tag-class>cn.com.liveuc.privilege.tag.perssiontag</tag-class>
<body-content>scriptless</body-content>
<!-- 标签属性 -->
<attribute>
<name>uri</name><!-- 属性名称 -->
<required>true</required><!-- 是否必须 -->
<rtexprvalue>true</rtexprvalue><!-- 是否为动态标签 -->
</attribute>
</tag>
</taglib>
3.运用标签
在jsp页面导入标签:
<a href="mailto:%@taglib prefix='wxt' uri='/wxt' %">%@taglib prefix="wxt" uri="/wxt" %</a>
运用标签:
<wxt:per uri="${pagecontext.request.contextpath }/user/list">
<a href="${pagecontext.request.contextpath }/user/list" target="reight">用户管理</a>
</wxt:per>
用户权限包含uri资源的将会输出标签内容。
下面将以权限的控制为例自定义一个标签:
一、标签类型
复制代码 代码如下:
<wxt:per uri="${pagecontext.request.contextpath }/privilege/list"></wxt:per>
步骤:
1.自定义一个类perssiontag 继承simpletagsupport(自定义标签一般都会继承这个类)
复制代码 代码如下:
package cn.com.liveuc.privilege.tag;
import java.io.ioexception;
import java.util.arraylist;
import java.util.list;
import java.util.set;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.pagecontext;
import javax.servlet.jsp.tagext.simpletagsupport;
import cn.com.liveuc.privilege.model.privilege;
import cn.com.liveuc.privilege.model.resource;
import cn.com.liveuc.privilege.model.role;
import cn.com.liveuc.privilege.model.user;
/**
*
* @说明 自定义标签
*/
public class perssiontag extends simpletagsupport {
//自定义标签属性,用于标签传入参数
private string uri;
//接收标签传入的参数
public void seturi(string uri) {
this.uri = uri;
}
@override
public void dotag() throws jspexception, ioexception {
//获取用户登陆后保存的session
pagecontext page = (pagecontext) this.getjspcontext();
user user = (user) page.getsession().getattribute("login");
//如果用户登陆
if(user != null) {
//用户登陆判断用户权限
list<string> list = new arraylist<string>();
//获取用户的角色
set<role> role = user.getrole();
for(role r:role) {
//获取角色对应的权限
set<privilege> privilege = r.getprivilege();
for(privilege p:privilege) {
//获取权限对应的资源
set<resource> res = p.getresource();
for(resource re:res) {
list.add(re.geturi());
}
}
}
for(string ur:list) {
//判断用户的权限
if(ur.equals(uri)) {
this.getjspbody().invoke(null); //有权限输出标签体内容
}
}
}
}
}
2.在web-inf下创建tld文件描述标签。
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
version="2.0"
xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<description><![cdata["to make it easier to access dynamic data;
the apache struts framework includes a library of custom tags.
the tags interact with the framework's validation and internationalization features;
to ensure that input is correct and output is localized.
the struts tags can be used with jsp freemarker or velocity."]]></description>
<display-name>"struts tags"</display-name>
<tlib-version>2.2.3</tlib-version>
<short-name>s</short-name>
<uri>/wxt</uri>
<tag>
<name>per</name><!-- 标签名 -->
<tag-class>cn.com.liveuc.privilege.tag.perssiontag</tag-class>
<body-content>scriptless</body-content>
<!-- 标签属性 -->
<attribute>
<name>uri</name><!-- 属性名称 -->
<required>true</required><!-- 是否必须 -->
<rtexprvalue>true</rtexprvalue><!-- 是否为动态标签 -->
</attribute>
</tag>
</taglib>
3.运用标签
在jsp页面导入标签:
<a href="mailto:%@taglib prefix='wxt' uri='/wxt' %">%@taglib prefix="wxt" uri="/wxt" %</a>
运用标签:
<wxt:per uri="${pagecontext.request.contextpath }/user/list">
<a href="${pagecontext.request.contextpath }/user/list" target="reight">用户管理</a>
</wxt:per>
用户权限包含uri资源的将会输出标签内容。