梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
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>
posted on 2010-08-18 14:05 HUIKK 阅读(192) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: