jsp的自定义标签 控制jsp内容显示

引入方式示例

<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>

 

tld文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"   
    "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>bgt</short-name>
    <!-- backGroundTag -->
    <uri>http://www.sdyy.tag</uri>
    <tag>
        <name>hasUrlPerm</name>
        <tag-class>com.sdyy.common.tags.HasUrlPermissionTag</tag-class>
        <attribute>
            <name>link</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue><!-- 是否支持恶劣表达式 -->
            <type>java.lang.String</type>
            <description>示例:acApplication/forMain.do</description>
        </attribute>
        
    </tag>
</taglib>

 

A、【判断标签】HasUrlPermissionTag标签是一个判断标签,通过该标签来决定标签包裹的内容是否显示,写法如下:
package com.sdyy.common.tags;



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

import com.sdyy.common.spring.interceptor.PermissionInterceptor;
/**
 * 
 * @ClassName: HasUrlPermissionTag
 * @Description: 根据url判断权限标签
 * @author: liuyx
 * @date: 2015年12月21日上午11:15:32
 */
public class HasUrlPermissionTag extends BodyTagSupport {

    private String link;//  abc.do

    @Override
    public int doStartTag() throws JspException { // 在标签开始处出发该方法
        
        HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
        //获取session中存放的权限
        
        //判断是否有权限访问
        if (PermissionInterceptor.isOperCanAccess(request, link)) {
            //允许访问标签body
            return BodyTagSupport.EVAL_BODY_INCLUDE;// 返回此则执行标签body中内容,SKIP_BODY则不执行
        } else {
            return BodyTagSupport.SKIP_BODY;
        }

    }

    @Override
    public int doEndTag() throws JspException {
        return BodyTagSupport.EVAL_BODY_INCLUDE;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

}

在JSP中的使用方式

    <bgt:hasUrlPerm link="abc.do">
    <div>tttttttttttttttttest</div>
    </bgt:hasUrlPerm>

link=abc.do直接传入

 

 

B、【控件标签】,这种标签直接返回一个控件,不过是通过java代码生成的控件内容,写法示例:

package com.sdyy.common.tags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

/*import com.sdyy.base.ac.ac_permission.model.AcPermission;*/

public class ButtonUrlTag extends BodyTagSupport {
    private static final long serialVersionUID = -7811902545513255473L; 


    //标签属性用户名
    private String user = null;
    //标签属性操作url
    private String url = null;
    //标签属性 js方法
    private String jsmethod = null;
    //标签属性image 按钮图片
    private String image = null;
    //标签属性 alt 提示
    private String alt = null;
    //标签属性操作value 按钮文本
    private String value  = null;




    /* 标签初始方法 */
    public int doStartTag() throws JspTagException{
        return super.EVAL_BODY_INCLUDE;
    }


    /* 标签结束方法 */
    public int doEndTag() throws JspTagException{
        pageContext.getSession();
        Boolean b = false;
        List list = new ArrayList();
        /*AcPermission p = new AcPermission();*/
        /*JDBCHibernate jdbca = new JDBCHibernate();*/
        try {
            /*list = jdbca.getHaveURLByUsernameList(user);*/
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        for(int i = 0;i < list.size(); i++){
            /*p = (AcPermission) list.get(i);*/
            if(1==1) {//p.getUrl().trim().equals(url.trim())){
                b = true;
                //如果jsmethod属性不为null 则把超链接href改为调用js
                if(jsmethod!=null){
                    url = jsmethod;
                }
            }
        }
        JspWriter out = pageContext.getOut();
        if(b){
            try {
                //有权限 显示操作按钮
                out.println("<a href='" +url+ "' class='regular'><img src='" + image + "' alt='" + alt +"' />" + value + "</a>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return super.SKIP_BODY;
    }

    /* 释放资源 */
    public void release(){
        super.release();
    }


    public String getUser() {
        return user;
    }


    public void setUser(String user) {
        this.user = user;
    }


    public String getUrl() {
        return url;
    }


    public void setUrl(String url) {
        this.url = url;
    }


    public String getImage() {
        return image;
    }


    public void setImage(String image) {
        this.image = image;
    }


    public String getAlt() {
        return alt;
    }


    public void setAlt(String alt) {
        this.alt = alt;
    }


    public String getValue() {
        return value;
    }


    public void setValue(String value) {
        this.value = value;
    }

    public String getJsmethod() {
        return jsmethod;
    }


    public void setJsmethod(String jsmethod) {
        this.jsmethod = jsmethod;
    }
}

 

posted @ 2016-12-14 10:39  crazyYong  阅读(3590)  评论(0编辑  收藏  举报