案例-开发if标签和if-else标签

开发if标签##

这个例子比较简单,方法跟我之前写的在简单标签里添加属性类似,我就直接上代码了。

1.先写jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="/example" %>


    开发if标签


<%--模拟用户登录,直接存一个session标记--%>
<%
    session.setAttribute("user", "张三");
%>

    用户没有登录


    welcome!!!+${user}



2.标签处理器类

package cn.itcast.web.tagexample;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class IfTag 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);
        }
    }
}

3.在标签描述符文件里添加代码如下:


        if
        cn.itcast.web.tagexample.IfTag
        scriptless

        
            test
            true
            true
        
    

图示是用户不等于空的条件:

image.png

开发if-else标签##

  1. 先写jsp页面(嵌套标签)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="/example" %>


    开发if-else标签


<%--模拟用户登录,直接存一个session标记--%>
<%
    session.setAttribute("user", "撒花花");
%>


    我要${user}


<%--if-else在sun公司的命名规范when-otherwise。一个java类控制执行了标签体,另一个java类控制不要执行另一个标签体,怎么通知?
就是这两个java类共享同一个变量,也就是让两个标签共享同一个“爸爸”--%>


    请您登录。。。。




2.这里有三个标签,意味着要写三个标签处理器类

ChooseTag

package cn.itcast.web.tagexample;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class ChooseTag extends SimpleTagSupport {
    private boolean isDo;

    public boolean isDo() {
        return isDo;
    }

    public void setDo(boolean aDo) {
        isDo = aDo;
    }

    //控制标签体执行
    @Override
    public void doTag() throws JspException, IOException {
        this.getJspBody().invoke(null);
    }

WhenTag

package cn.itcast.web.tagexample;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class WhenTag extends SimpleTagSupport {
    private boolean test;

    public void setTest(boolean test) {
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException {
        /*test属性为true,isDO为false表示没有被执行过,这时要访问父标签
        *在执行doTag方法之前就已经调用setParent方法把“爸爸”传递进来了
        *(在我的简单标签功能文章里有一张图说明了这个过程)*/
        ChooseTag parent = (ChooseTag) this.getParent();
        if (test && !parent.isDo()) {
            this.getJspBody().invoke(null);
            parent.setDo(true);
        }
    }
}

OtherwiseTag

package cn.itcast.web.tagexample;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class OtherwiseTag extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        ChooseTag parent = (ChooseTag) this.getParent();
        if (!parent.isDo()) {
            this.getJspBody().invoke(null);
            parent.setDo(true);
        }
    }
}

3.在标签描述符example.tld里添加代码:


        choose
        cn.itcast.web.tagexample.ChooseTag
        scriptless
    

    
        when
        cn.itcast.web.tagexample.WhenTag
        scriptless

        
            test
            true
            true
        
    

    
        otherwise
        cn.itcast.web.tagexample.OtherwiseTag
        scriptless
    

不多说,结果上图:

image.png

你可能感兴趣的:(案例-开发if标签和if-else标签)