JSP自定义标签之简单标签入门

在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
时间: 2024-07-31 03:11:16

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

JSP自定义标签简单入门教程_JSP编程

在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 pr

JSP自定义简单标签入门之带有属性

上面写的那个简单标签来控制页面中标签内容的重复次数是固定的,这就是权限"写死了",这是非常不好的行为,因为其灵活性太差.所以下面就介绍一种使用属性来控制标签内容输出次数的方法. 准备工作 创建实现了SimpleTag接口的实现类(或者是继承了SimpleTagSupport类的子类) 在类中为相应的属性字段添加setter方法,注意字段名称一定要一致.否则会出错滴 在tld文件中进行相关约束项的声明,不同于不带属性的简单标签的声明方式,这里多了一个attribute子标签,内有name

JSP自定义标签入门学习_JSP编程

本文为大家分享了JSP自定义标签入门学习教程,希望大家喜欢. 1.JSP自定义标签: 自定义标签是用户定义的JSP语言元素.当JSP页面包含一个自定义标签时将被转化为servlet,标签转化为对被 称为tag handler的对象的操作,即当servlet执行时Web container调用那些操作.JSP标签扩展可以让你创建新的标签并且可以直接插入到一个JSP页面. JSP 2.0规范中引入Simple Tag Handlers来编写这些自定义标记.你可以继承SimpleTagSupport类

jsp自定义标签之ifelse与遍历自定义标签示例

 这篇文章主要介绍了jsp自定义标签之ifelse与遍历自定义标签,需要的朋友可以参考下 第一个示例:  简单的jsp自定标签获取内容:  首先创建一个jsp实例类然后继承SimpleTagSupport类  然后实现父类的doTag()方法  在这个方法里获取标签体里的内容this.getJspBody();  返回的是JspFragment 类,根据这个类对象调用invoke(this.getJspContext().getOut());  这个方法里面也可以写空,所表达的意思也是输出到浏览

Java自定义简单标签实例

Java自定义简单标签可以方便的在页面输出信息,并且对于权限的控制,和对于Jsp标签和servlet代码的分离有着很好的作用   下面将以权限的控制为例自定义一个标签:一.标签类型 复制代码 代码如下: <wxt:per uri="${pageContext.request.contextPath }/privilege/list"></wxt:per> 步骤:1.自定义一个类PerssionTag 继承SimpleTagSupport(自定义标签一般都会继承这

jsp自定义标签-jsp中怎么自定义一个有onclick这样属性的标签啊?

问题描述 jsp中怎么自定义一个有onclick这样属性的标签啊? 哪位大虾可以告诉菜鸟,怎么在自定义的标签中定义一个可以调用jQuery里面方法的属性哇,就像onclick那种. 解决方案 自定义一个很简单的JSP标签 解决方案二: 正所谓自定义,就是在jsp里面定义一套你的规范的标签,在服务端去根据规则去翻译成html代码,再响应给浏览器解析,你定义的标签,在给浏览器的时候都一样的html,那么问题就来了, 你可以定义你的事件,然后翻译成html的onclick事件就可以啊,这是鄙人的理解.

javaweb学习总结—jsp简单标签标签库开发

一.JspFragment类介绍 javax.servlet.jsp.tagext.JspFragment类是在JSP2.0中定义的,它的实例对象代表JSP页面中的一段符合JSP语法规范的JSP片段,这段JSP片段中不能包含JSP脚本元素. WEB容器在处理简单标签的标签体时,会把标签体内容用一个JspFragment对象表示,并调用标签处理器对象的setJspBody方法把JspFragment对象传递给标签处理器对象.JspFragment类中只定义了两个方法,如下所示: getJspCon

基于JSP 自定义标签使用实例介绍_JSP编程

添加JSP自定义标签: 先添加一个tld文件到WEB-INF文件夹中<?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://

jsp自定义标签用法实例详解_JSP编程

本文实例讲述了jsp自定义标签用法.分享给大家供大家参考.具体如下: 在JSP中有一种机制,可以让你在JSP页面中插入与HTML类似的标记.本文介绍JSP定制标记的基本概念和构成,以及如何开发和应用JSP定制标记. 什么是标记 使用HTML语言我们可以这样去编辑我们的网页: <HTML> <HEAD> <TITLE> HELLO WORLD </TITLE> </HEAD> <BODY> HELLO WORLD </BODY&g