Jsp自定义标签

自定义标签

引入

        需求: 向浏览器输出当前客户的IP地址 (只能使用jsp标签)

第一个自定义标签开发步骤

        1)编写一个普通的java类,继承SimpleTagSupport类,叫标签处理器类
/**
 * 标签处理器类
 * @author APPle
 * 1)继承SimpleTagSupport
 *
 */
public class ShowIpTag extends SimpleTagSupport{
    private JspContext context;

    /**
     * 传入pageContext
     */
    @Override
    public void setJspContext(JspContext pc) {
        this.context = pc;
    }

    /**
     * 2)覆盖doTag方法
     */
    @Override
    public void doTag() throws JspException, IOException {
        //向浏览器输出客户的ip地址
        PageContext pageContext = (PageContext)context;

        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();

        String ip = request.getRemoteHost();

        JspWriter out = pageContext.getOut();

        out.write("使用自定义标签输出客户的IP地址:"+ip);

    }
}

以上代码可以优化为以下的代码(原因见代码内)

/**
 * 标签处理器类
 * @author APPle
 * 1)继承SimpleTagSupport
 *
 */
public class ShowIpTag extends SimpleTagSupport{
    /**
     * 以下屏蔽的代码在SimpleTagSupport代码中已经做了!这里不需要重复再做!
     */
    /*private JspContext context;

    *//**
     * 传入pageContext
     *//*
    @Override
    public void setJspContext(JspContext pc) {
        this.context = pc;
    }*/

    /**
     * 2)覆盖doTag方法
     */
    @Override
    public void doTag() throws JspException, IOException {
        //向浏览器输出客户的ip地址
        PageContext pageContext = (PageContext)this.getJspContext();

        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();

        String ip = request.getRemoteHost();

        JspWriter out = pageContext.getOut();

        out.write("使用自定义标签输出客户的IP地址:"+ip);

    }
}
    2)在web项目的WEB-INF目录下建立itcast.tld文件,这个tld叫标签库的声明文件。(参考核心标签库的tld文件)
<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
  <!-- 标签库的版本 -->
  <tlib-version>1.1</tlib-version>
  <!-- 标签库前缀 -->
  <short-name>itcast</short-name>
  <!-- tld文件的唯一标记 -->
  <uri>http://gz.itcast.cn</uri>

  <!-- 一个标签的声明 -->
  <tag>
    <!-- 标签名称 -->
    <name>showIp</name>
    <!-- 标签处理器类的全名 -->
    <tag-class>gz.itcast.a_tag.ShowIpTag</tag-class>
    <!-- 输出标签体内容格式 -->
    <body-content>scriptless</body-content>
  </tag>

</taglib>
    3) 在jsp页面的头部导入自定义标签库
                <%@taglib uri="http://gz.itcast.cn" prefix="itcast"%>

    4) 在jsp中使用自定义标签
                <itcast:showIp></itcast:showIp>

自定义标签的执行过程

    问题: http://localhost:8080/day14/01.hellotag.jsp  如何访问到自定义标签?

    前提: tomcat服务器启动时,加载到每个web应用,加载每个web应用的WEB-INF目录下的所有文件!!!例如。web.xml, tld文件!!!

        1)访问01.hellotag.jsp资源
        2)tomcat服务器把jsp文件翻译成java源文件->编译class->构造类对象->调用_jspService()方法
        3)检查jsp文件的taglib指令,是否存在一个名为http://gz.itcast.cn的tld文件。如果没有,则报错
        4)上一步已经读到itcast.tld文件
        5)读到<itcast:showIp> 到itcast.tld文件中查询是否存在<name>为showIp的<tag>标签
        6)找到对应的<tag>标签,则读到<tag-class>内容
        7)得到 gz.itcast.a_tag.ShowIpTag

    构造ShowIpTag对象,然后调用ShowIpTag里面的方法

自定义标签处理器类的生命周期

SimpleTag接口: 
        void    setJspContext(JspContext pc)    --设置pageContext对象,传入pageContext(一定调用)通过getJspCotext()方法得到pageContext对象
        void    setParent(JspTag parent)        --设置父标签对象,传入父标签对象,如果没有父标签,则不    调用此方法。通过getParent()方法得到父标签对象。
        void    setXXX(值)                     --设置属性值。
        void    setJspBody(JspFragment jspBody) --设置标签体内容。标签体内容封装到JspFragment对象中,然后传入JspFragment对象。通过getJspBody()方法                                                     得到标签体内容。如果没有标签体内容,则不会调用此方法
        void    doTag()                         --执行标签时调用的方法。(一定调用)
/**
 * 标签处理器类
 * @author APPle
 *
 */
public class DemoTag extends SimpleTagSupport{
    //1.声明属性的成员变量
    private Integer num;

    //2.关键点: 必须提供公开的setter方法,用于给属性赋值
    public void setNum(Integer num) {
        this.num = num;
    }


    @Override
    public void doTag() throws JspException, IOException {
        System.out.println("执行了标签");

        /**
         * 1)控制标签内容是否输出
         *    输出: 调用jspFrament.invoke();
         *    不输出: 不调用jspFrament.invoke();
         */
        //1.1 得到标签体内容
        JspFragment jspBody = this.getJspBody();

        /**
         * 执行invoke方法: 把标签体内容输出到指定的Writer对象中
         */
        //1.2 往浏览器输出内容,writer为null就是默认往浏览器输出
        //JspWriter out = this.getJspContext().getOut();
        //jspBody.invoke(out);
        jspBody.invoke(null);//等价于上面的代码

        /**
         * 3)控制重复输出标签体内容
         *     方法: 执行多次jspBody.invoke()方法
         */
        /*for(int i=1;i<=num;i++){
            jspBody.invoke(null);
        }*/

        /**
         * 4)改变标签体内容
         */
        //4.1 创建StringWriter临时容器
        /*StringWriter sw = new StringWriter();
        //4.2 把标签体拷贝到临时容器
        jspBody.invoke(sw);
        //4.3 从临时容器中得到标签体内容
        String content = sw.toString();
        //4.4 改变内容
        content = content.toLowerCase();
        //System.out.println(content);
        //4.5 把改变的内容输出到浏览器
        //jspBody.invoke(null); 不能使用此方式输出,因为jsbBody没有改变过
        this.getJspContext().getOut().write(content);*/

        /**
         * 2)控制标签余下内容是否输出
         *   输出: 什么都不干!
         *   不输出: 抛出SkipPageException异常
         */
        throw new SkipPageException();

    }
}
  <tag>
    <name>demoTag</name>
    <tag-class>gz.itcast.a_tag.DemoTag</tag-class>
    <body-content>scriptless</body-content>
    <!-- 属性声明 -->
    <attribute>
        <!-- 属性名称 -->
        <name>num</name>
        <!-- 是否必填 -->
        <required>true</required>
        <!-- 是否支持EL表达式 -->
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false"%>
<%@taglib uri="http://gz.itcast.cn" prefix="itcast"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>第二个自定义标签</title>  
  </head>

  <body>
    <itcast:demoTag num="2">xxxx${10+5}</itcast:demoTag>
          标签余下内容
  </body>
</html>

自定义标签的作用

        1)控制标签体内容是否输出
        2)控制标签余下内容是否输出
        3)控制重复输出标签体内容
        4)改变标签体内容
        5)带属性的标签
            步骤: 
                5.1 在标签处理器中添加一个成语变量和setter方法
    //1.声明属性的成员变量
    private Integer num;

    //2.关键点: 必须提供公开的setter方法,用于给属性赋值
    public void setNum(Integer num) {
        this.num = num;
    }

输出标签体内容格式

    JSP:   在传统标签中使用的。可以写和执行jsp的java代码。
    scriptless:  标签体不可以写jsp的java代码
    empty:    必须是空标签。
    tagdependent : 标签体内容可以写jsp的java代码,但不会执行。

案例

    核心标签库: c:if   c:choose+c:when+c:otherwise   c:forEach
    高仿核心标签库

java文件

/*IfTag.java*/
public class IfTag extends SimpleTagSupport {
    private boolean test;

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

    @Override
    public void doTag() throws JspException, IOException {
        //根据test的返回值决定是否输出标签体内容
        if(test){
            this.getJspBody().invoke(null);
        }
    }
}
/*ChooseTag.java*/
public class ChooseTag extends SimpleTagSupport {
    //不是属性,而是临时变量
    private boolean flag;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //输出标签体内容
        this.getJspBody().invoke(null);
    }
}
/*WhenTag.java*/
public class WhenTag extends SimpleTagSupport {
    private boolean test;

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

    @Override
    public void doTag() throws JspException, IOException {
        //根据test的返回值决定是否输出标签体内容
        if(test){
            this.getJspBody().invoke(null);
        }

        //获取父标签
        ChooseTag parent = (ChooseTag)this.getParent();
        parent.setFlag(test);   
    }
}
/*OtherwiseTag.java*/
public class OtherwiseTag extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        //通过父标签传递,when标签中test的值
        //获取父标签
        ChooseTag parent = (ChooseTag)this.getParent();
        boolean test = parent.isFlag();

        if(!test){
            this.getJspBody().invoke(null);
        }
    }
}

tld文件

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
  <!-- 标签库的版本 -->
  <tlib-version>1.1</tlib-version>
  <!-- 标签库前缀 -->
  <short-name>itcast</short-name>
  <!-- tld文件的唯一标记 -->
  <uri>http://gz.itcast.cn</uri>

  <tag>
    <name>if</name>
    <tag-class>gz.itcast.b_cases.IfTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <name>choose</name>
    <tag-class>gz.itcast.b_cases.ChooseTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>

  <tag>
    <name>when</name>
    <tag-class>gz.itcast.b_cases.WhenTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <name>otherwise</name>
    <tag-class>gz.itcast.b_cases.OtherwiseTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>

  <tag>
    <name>forEach</name>
    <tag-class>gz.itcast.b_cases.ForEachTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
</taglib>

jsp文件

<%--if.jsp--%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://gz.itcast.cn" prefix="itcast" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>if标签</title>  
  </head>

  <body>
    <itcast:if test="${10>5}">
        条件成立
    </itcast:if>
  </body>
</html>
<%--choose.jsp--%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://gz.itcast.cn" prefix="itcast" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>choose标签</title>  
  </head>

  <body>
    <itcast:choose>
        <itcast:when test="${10<5}">
            条件成立
        </itcast:when>
        <itcast:otherwise>
            条件不成立
        </itcast:otherwise>    
    </itcast:choose>
  </body>
</html>
<%--foreach.jsp--%>
<%@ page language="java" import="java.util.*,gz.itcast.b_cases.*" pageEncoding="utf-8"%>
<%@taglib uri="http://gz.itcast.cn" prefix="itcast" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>forEach标签</title>  
  </head>

  <body>
    <%
       //保存数据
       //List
        List<Student>  list = new ArrayList<Student>();
        list.add(new Student("rose",18));
        list.add(new Student("jack",28));
        list.add(new Student("lucy",38));
        //放入域中
        pageContext.setAttribute("list",list);

        //Map
        Map<String,Student> map = new HashMap<String,Student>();
        map.put("100",new Student("mark",20));
        map.put("101",new Student("maxwell",30));
        map.put("102",new Student("narci",40));
        //放入域中
        pageContext.setAttribute("map",map);
     %>

     <itcast:forEach items="${list}" var="student">
            姓名:${student.name } - 年龄:${student.age }<br/>
     </itcast:forEach>

     <hr/>

     <itcast:forEach items="${map}" var="entry">
          编号:${entry.key} - 姓名:${entry.value.name} - 年龄:${entry.value.age }<br/>
     </itcast:forEach>
  </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值