再温jsp自定义标签

再温jsp自定义标签
java:
package  com.rwclp.usermanager.util;

import  java.io.IOException;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.jsp.JspException;
import  javax.servlet.jsp.JspWriter;
import  javax.servlet.jsp.tagext.BodyContent;
import  javax.servlet.jsp.tagext.BodyTagSupport;

import  com.framework.commons.util.StrUtil;
import  com.rwclp.global.util.UserSessionDTO;

/** */ /**
 * <ul>
 * <li>Title:[UserRightTag]</li>
 * <li>Description: [用户权限JSP标签]</li>
 * <li>All right reserved.</li>
 * <li>Created by [Huyvanpull] [2010-8-18]</li>
 * <li>Midified by [修改人] [修改时间]</li>
 * </ul>
 * 
 * 
@version 1.0
 
*/

@SuppressWarnings(
" serial " )
public   class  UserRightTag  extends  BodyTagSupport
{
    
/** *//** 标签体内容 */
    
private BodyContent bodyContent;
    
    
/** *//** 权限名称 */
    
private String name = "";
    
    
public int doEndTag() throws JspException
    
{
        
// 获取JSPWriter对象
        JspWriter out = bodyContent.getEnclosingWriter();
        
try
        
{
            String content 
= bodyContent.getString();
            
// 判断标签体是否有值和权限名是否有值
            if (StrUtil.noVal(content))
            
{
                content 
= "标签体为空";
                out.write(content);
                
return EVAL_PAGE;
            }

            
            
// 判断权限名是否为空
            if (StrUtil.noVal(name))
            
{
                content 
= "权限名为空";
                out.write(content);
                
return EVAL_PAGE;
            }

            
            HttpServletRequest request 
= null;
            request 
= (HttpServletRequest) pageContext.getRequest();
            UserSessionDTO userSessionDTO 
= UserSessionDTO.get(request);
            
            
// 判断用户是否登陆
            if (userSessionDTO == null)
            
{
                content 
= "用户没有登陆";
                out.write(content);
                
return EVAL_PAGE;
            }

            
            
// 得到权限值
            if (userSessionDTO.getRightByName(name).equals("0"))
            
{
                out.write(content);
            }

        }

        
catch (IOException e)
        
{
            e.printStackTrace();
        }

        
return EVAL_PAGE;
    }

    
    
public void setBodyContent(BodyContent bodyContent)
    
{
        
this.bodyContent = bodyContent;
    }

    
    
public void setName(String name)
    
{
        
this.name = name;
    }

}

xml:
<? xml version="1.0" encoding="GBK" ?>
< 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" >
    
< tlib-version > 1.0 </ tlib-version >
    
< short-name > mytaglib </ short-name >
    
< uri > http://www.crazyit.org/mytaglib </ uri >
    
< tag >
        
< name > right </ name >
        
< tag-class >  com.rwclp.usermanager.util.UserRightTag </ tag-class >
        
< body-content > JSP </ body-content >
        
< attribute >   
            
< name > name </ name >   
            
< required > true </ required >   
            
< rtexprvalue > true </ rtexprvalue >   
        
</ attribute >   
    
</ tag >
</ taglib >
jsp:
<% @taglib uri="/WEB-INF/myTag.tld" prefix="myTag"  %>
< myTag:right  name ="addUserRight" >
是否可能显示
</ myTag:right >

你可能感兴趣的:(再温jsp自定义标签)