在sun官方文档上有下面这样一段话。

官方文档声明

public interface SimpleTag
extends JspTag
Interface for defining Simple Tag Handlers.
Simple Tag Handlers differ from Classic Tag Handlers in that instead of supporting doStartTag() and doEndTag(), the SimpleTag interface provides a simple doTag() method, which is called once and only once for any given tag invocation. All tag logic, iteration, body evaluations, etc. are to be performed in this single method. Thus, simple tag handlers have the equivalent power of BodyTag, but with a much simpler lifecycle and interface.

To support body content, the setJspBody() method is provided. The container invokes the setJspBody() method with a JspFragment object encapsulating the body of the tag. The tag handler implementation can call invoke() on that fragment to evaluate the body as many times as it needs.

A SimpleTag handler must have a public no-args constructor. Most SimpleTag handlers should extend SimpleTagSupport.

生存周期及调用流程

The following is a non-normative, brief overview of the SimpleTag lifecycle. Refer to the JSP Specification for details.

  • A new tag handler instance is created each time by the container by calling the provided zero-args constructor. Unlike classic tag handlers, simple tag handlers are never cached and reused by the JSP container.
  • The setJspContext() and setParent() methods are called by the container. The setParent() method is only called if the element is nested within another tag invocation.

    The setters for each attribute defined for this tag are called by the container.
  • If a body exists, the setJspBody() method is called by the container to set the body of this tag, as a JspFragment. If the action element is empty in the page, this method is not called at all.
  • The doTag() method is called by the container. All tag logic, iteration, body evaluations, etc. occur in this method.
  • The doTag() method returns and all variables are synchronized.

简单标签使用小案例

必知必会:简单标签也是一个标签,所以声明的过程也Tag的一样,同样是三步。

  • 创建继承SimpleTag类的实现类,重写doTag方法
  • 在tld文件中进行严格的声明
  • 在jsp页面中taglib的命名空间及标签前缀的声明,然后进行调用自定义的简单标签

  • 第一步:创建实现类:
package web.simpletag;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 控制标签体是否执行
 * @author Summer
 *
 */
public class BodyController extends SimpleTagSupport {
    static{
        /*
         * 简单标签整体的执行流程如下:
         * 1.浏览器向web服务器发送请求,然后web服务器调用servlet(jsp)
         * 2.complier解释器进行初始化工作,先是调用setJspContext方法,将pageContext对象传递进去
         * 3.然后是看看此标签的父标签,即setParent方法
         * 4.再就是调用doTag方法了吧?但是要知道doTag内部会使用JspFragment对象,所以就必须先得到它,因此应该是调用setJspBody(JspFragment jspBody)方法
         * 5.最后是调用doTag 方法,执行相关的代码逻辑
         */
    }

    /**
     * 简单标签可以使用这一个方法实现所有的业务逻辑
     */
    @Override
    public void doTag() throws JspException, IOException {
        //代表标签体的对象
        JspFragment fragment = this.getJspBody();
        //fragment.invoke(null);是指将标签中的内容写给谁,null代表浏览器

        //1.修改标签体的内容
//      fragment.invoke(null);

        //2.控制标签体内容的重复输出
//      for(int i=1;i<=5;i++){
//          fragment.invoke(null);//设置为null,默认为向浏览器输出
//      }

        //3.修改标签体的内容
        PageContext context = (PageContext) fragment.getJspContext();
        StringWriter writer = new StringWriter();
        fragment.invoke(writer);
        String content = writer.getBuffer().toString();

        this.getJspContext().getOut().write(content.toUpperCase());

        //4.控制jsp页面的执行与否,只需要掌握一个原理即可
        /*
         * SkipPageException - If the page that (either directly or indirectly) invoked this
         * tag is to cease evaluation. A Simple Tag Handler generated from a tag
         *  file must throw this exception if an invoked Classic Tag Handler
         *   returned SKIP_PAGE or if an invoked Simple Tag Handler threw
         *    SkipPageException or if an invoked Jsp Fragment threw a
         *    SkipPageException.
         */
//      throw new SkipPageException();
    }

}

  • 在tld文件中进行相关约束项的配置:
<?xml version="1.0" encoding="UTF-8" ?>

<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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">

    <description>JSTL 1.1 XML library</description>
    <display-name>JSTL XML</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>x</short-name>
    <uri>/simplesummer</uri>

    <!-- 控制标签体内容的的简单标签的自定义标签 -->
    <tag>
        <name>BodyController</name>
        <tag-class>web.simpletag.BodyController</tag-class>
        <body-content>scriptless</body-content>
    </tag>
</taglib>

  • 第三步:在jsp页面中进行声明然后调用:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="/simplesummer" prefix="summer"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用SimpleTag接口实现的控制标签体内容是否执行的测试页面</title>
</head>
<body>
    <summer:BodyController>Summer</summer:BodyController>

</body>
</html>

  • 总结:

  • 简单标签可以替代BodyTag接口完成同样的操作,但是有更加的简单和轻便
  • 简单标签lifeCycle逻辑清晰,调用规则明确
  • 使用相关流对象就可以完成对标签体的操控maniplate

JSP自定义标签之简单标签入门的更多相关文章

  1. 传智播客JavaWeb day07、day08-自定义标签(传统标签和简单标签)、mvc设计模式、用户注册登录注销

    第七天的课程主要是讲了自定义标签.简单介绍了mvc设计模式.然后做了案例 1. 自定义标签 1.1 为什么要有自定义标签 前面所说的EL.JSTL等技术都是为了提高jsp的可读性.可维护性.方便性而取 ...

  2. JSP自定义tag控件标签

    JSP支持自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件.tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码. 按照 ...

  3. JavaWeb学习记录(十九)——jstl自定义标签之简单标签

    一.简单标签共定义了5个方法: setJspContext方法 setParent和getParent方法 setJspBody方法 doTag方法 二.方法介绍 osetJspContext方法 用 ...

  4. JSP标签编程--简单标签

    javax.servlet.jsp.tagext里的类SimpleTagSupport 使用SimpleTagSupport类一网打尽以往复杂的标签开发,直接使用doTag()方法 java文件: p ...

  5. 3_Jsp标签_简单标签_防盗链和转义标签的实现

    一概念 1防盗链 在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件,通过referer,网站可以检测目标网页访问的来源网页.有了referer跟踪来 ...

  6. struts2 标签为简单标签

    <s:form method="post" action="" theme="simple"> <s:textfield ...

  7. JSP自定义标签就是如此简单

    tags: JSP 为什么要用到简单标签? 上一篇博客中我已经讲解了传统标签,想要开发自定义标签,大多数情况下都要重写doStartTag(),doAfterBody()和doEndTag()方法,并 ...

  8. JSP自定义标签——简单标签(1)

    前面一篇博客介绍了自定义标签的传统标签使用方式,但是我们会发现,使用传统标签非常的麻烦,而且接口还多,现在传统标签基本都没用了,除了一些比较久的框架.Sun公司之后推出了一个新的标签使用方式,称之为简 ...

  9. JSP第七篇【简单标签、应用、DynamicAttribute接口】

    为什么要用到简单标签? 上一篇博客中我已经讲解了传统标签,想要开发自定义标签,大多数情况下都要重写doStartTag(),doAfterBody()和doEndTag()方法,并且还要知道SKIP_ ...

随机推荐

  1. 关于InnoDB的读写锁类型以及加锁方式

    (本文为了方便,英文关键词都都采用小写方式,相关知识点会简单介绍,争取做到可以独立阅读) 文章开始我会先介绍本文需要的知识点如下: innodb的聚簇索引(聚集索引)和非聚簇索引(二级索引.非聚集索引 ...

  2. Ubuntu16 编译源码安装MXNet 可变卷积Deformable-ConvNets GPU版

    [引言]最近接手了公司的关于虫子识别的项目,使用MXNet框架开发,但是实际用的是Deformable-ConvNets. Deformable-ConvNets为微软研究研究院提出的可变卷积网络,可 ...

  3. JVM回收方法区内存

    很多人认为方法区(或者HotSpot虚拟机中的永久代)是没有垃圾收集的,Java虚拟机规范中确实说过可以不要求虚拟机在方法区实现垃圾收集,而且在方法区进行垃圾收集的“性价比”一般比较低:在堆中,尤其是 ...

  4. 项目中BigDecimal与Double使用场景

    金额要用BigDecimal 金额计算不能用doube!!!! 金额计算必须用BigDecimal,下面对比一下用double 跟BigDecimal的区别.先看一个小例子: 请看题: 示例1 问, ...

  5. Python中strip()、lstrip()、rstrip()用法详解

    Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格)lstrip:用来去除开头字符.空白符 ...

  6. 全新 Kali Linux 系统安装指南

    Kali Linux 系统可以说是在安全测试方面最好的开箱即用的 Linux 发行版.Kali 下的很多工具软件都可以安装在大多数的 Linux 发行版中,Offensive Security 团队在 ...

  7. APP自动化框架LazyAndroid使用手册(3)--核心API介绍

    作者:黄书力 概述 在前一篇博文中,简要介绍了一款安卓UI自动化测试框架LazyAndroid (http://blog.csdn.net/kaka1121/article/details/53204 ...

  8. sublime text package control 被墙的解决办法

    似乎没有办法 只能碰运气, 时好时坏. 或者手动安装 趁着好的时候, 下载离线包 https://packagecontrol.io/Package%20Control.sublime-package ...

  9. GCT学习总结

    GCT的一个综合的考试性质,时间紧,题量大,这个时候需要我们快速.准确的答题,把自己的能力展现在其中,十一期间和同学们一起学习.讨论,大家都提高很大,各科谈一下自己的心得 数学: 数学相对来说还是不难 ...

  10. Objective-C 中如何测量代码的效率

    背景 在我们编程的时候,可能经常会有一些疑问: * 我们写的某个方法的执行效率是多少? * 方法 A 和 方法 B 哪个更快? 因此,我们不可避免的要用到一些方法来计算代码的执行效率.计算代码的执行效 ...