当前位置: 移动技术网 > IT编程>开发语言>Jsp > jsp自定义标签

jsp自定义标签

2018年03月19日  | 移动技术网IT编程  | 我要评论

jsp自定义标签。今天和大家分享jsp自定义标签的实现,首先介绍自定义标签的整体实现流程,然后介绍几种常见的标签的实现方法,根据这些内容可以影射出各种各样的标签。

一、jsp自定义标签的实现过程

自定义标签的实现分三步,第一步就是编写tld文件;第二步,编写java文件;第三步,在jsp页面使用自定义标签。下面分步骤实现(以显示本地IP地址标签为例):
1、第一步:编写login.tld文件 
不会写tld文件没关系,在项目下有jstl标签的tld文件,直接copy一个过来修改就行了,文件路径:
(https://img.blog.csdn.net/20170428102439876?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2F0X3Bw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)   

这里我们修改文件中的内容即可:




    
    1.0
    
    cat
    
    com.catpp.tag

    
    
        
        showIp
        
        com.catpp.tag.Tag
        
        scriptless
    

    
    
    
        if
        com.catpp.tag.IfTag
        scriptless
        
        
            
            test
            
            true
            
            true
        
    

    
    
        choose
        com.catpp.ChooseTag
        scriptless
    
    
    
        when
        com.catpp.WhenTag
        scriptless
        
            test
            true
            true
        
    
    
    
        otherwise
        com.catpp.OhterwiseTag
        scriptless
    

    
    
        forEach
        com.catpp.ForEachTag
        scriptless
        
            items
            true
            true
        
        
            var
            true
            false
        
    

2、实现标签处理器类Tag.java

标签处理器类一定要继承SimpleTagSupport类,重写doTage()方法

public class Tag extends SimpleTagSupport{
    @Override
    public void doTag() throws JspException, IOException{
        /**
         * 输出本地IP地址
         */
        PageContext pageContext = (PageContext)this.getJspContext();
        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        String id = request.getRemoteHost();
        JspWriter out = pageContext.getOut();
        out.write("使用自定义标签输出本地IP地址:"+ip);
    }
}

3、在jsp页面使用自定义标签

<%@ page language="java" pageEncoding="utf-8"%>

<%@taglib uri="com.catpp.tag" prefix="cat"%>


自定义标签
显示when标签的内容 显示otherwise标签的内容
自定义forEach标签内容:${data.value}

jsp自定义标签的实现步骤就是这样的,下面介绍几种常见的标签的实现方法,以c标签中的if、choose、forEach标签等为例。
二、常见标签的实现

1、if标签

因为实现的不同之处主要在于标签处理器的不同,所以接下来的关于jsp和tld的内容直接在上面的jsp和tld文件中。
public class IfTag extends SimpleTagSupport{
    private boolean test;
    //属性一定要配置setter方法
    public void setTest(boolean test){
        this.test = test;
    }
    @Override
    public void doTag() throws JspException, IOException{
        //根据test的返回值判断是否输出内容
        if(test){
            //jspContext、jspBody(JspFragment:标签体内容)等都是在SimpleTagSupport类中定义好的,所以不用声明,直接用this调用就可以
            //invoke()方法传入null,把标签体内容输出到浏览器
            //如果传入其他Writer对象也可以
            this.getJspBody().invoke(null);
        }
    }
}

2、choose标签

choose标签要和when、otherwise标签一起使用,先说一下总体的逻辑:
choose里的内容是一定要显示的,然后when标签内有一个test属性,当test返回的值为true时,显示when标签内的内容,当test返回的值为false时,显示otherwise标签内的内容,那么otherwise标签如何判断test的返回值呢,这里用到了父标签的内容,choose标签作为when和otherwise标签的父标签
//Choose标签的处理器类:
public class ChooseTag extends SimpleTagSupport{
    //这个不是属性,只是一个临时变量,用来接收when标签的test属性的返回值,然后交给otherwise标签
    private boolean flag;
    public boolean isFlag(){
        retrun flag;
    }
    public void setFlag(boolean flag){
        this.flag = flag;
    }

    @Override
    public void doTag() throws JspException, IOException{
        //显示标签体的内容
        this.getJspBody().invoke(null);
    }
}
//when标签的处理器类:
public class WhenTag extends SimpleTagSupport{
    private boolean test;
    public void setTest(boolean test){
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException{
        if(test){
            this.getJspBody.invoke(null);
        }
        //为父标签的flag属性赋值
        ChooseTag parent = (ChooseTag)this.getParent();
        parent.setFlag(test);
    }
}
//otherwise标签的处理器类:
public class OtherwiseTag extends SimpleTagSupport{
    @Override
    public void doTag() throws JspException, IOException{
        ChooseTag parent = (ChooseTag)this.getParent();
        boolean test = parent.isFlag();
        //如果父标签的flag属性为false,显示otherwise标签体内容
        if(!test){
            this.getJspBody().invoke(null);
        }
    }
}

3、forEach标签

public class ForEachTag extends SimpleTagSupport{
    private Object items;
    private String var;
    public void setItems(Object items){
        this.items = items;
    }
    public void setVar(String var){
        this.var = var;
    }

    public void doTag() throws JspException, IOException{
        PageContext pageContext = (PageContext)this.getJspContext();
        Collection coll = null;
        if(items instanceof List){
            coll = (List)items;
        }
        if(items instanceof Map){
            Map map = (Map)items;
            coll = map.entrySet();
        }
        for(Object obj : items){
            pageContext.setAttribute(var,object);
            this.getJspBody().invoke(null);
        }
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网