zoukankan      html  css  js  c++  java
  • Java自定义简单标签

     Java自定义简单标签可以方便的在页面输出信息,并且对于权限的控制,和对于Jsp标签和servlet代码的分离有着很好的作用。

    下面将以权限的控制为例自定义一个标签:

    一、标签类型

    <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;
    /**
     * 
     * @作者  wuxiaotie
     * @邮件  wuxiaotie@iactive.com.cn
     * @说明   自定义标签
     */
    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页面导入标签:

    %@taglib prefix="wxt" uri="/wxt" %
    运用标签:
    <wxt:per uri="${pageContext.request.contextPath }/user/list">
          <a href="${pageContext.request.contextPath }/user/list" target="reight">用户管理</a>
    </wxt:per>
    用户权限包含uri资源的将会输出标签内容。




     

  • 相关阅读:
    图片自适应居中
    常用正则记录
    继承
    HTML学习
    js原型与原型链理解
    mac 及mac下开发常用快捷键命令
    服务器返回的各种HTTP状态码介绍(转)
    webstorm使用笔记
    《Javascript Dom 高级程序设计》读书笔记
    ECMAScript6学习笔记
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3202926.html
Copyright © 2011-2022 走看看