博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP自定义标签之简单标签入门
阅读量:6867 次
发布时间:2019-06-26

本文共 4719 字,大约阅读时间需要 15 分钟。

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

官方文档声明

public interface SimpleTagextends JspTagInterface 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文件中进行相关约束项的配置:
JSTL 1.1 XML library
JSTL XML
1.1
x
/simplesummer
BodyController
web.simpletag.BodyController
scriptless

  • 第三步:在jsp页面中进行声明然后调用:
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib uri="/simplesummer" prefix="summer"%>
用SimpleTag接口实现的控制标签体内容是否执行的测试页面
Summer

  • 总结:

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

转载地址:http://vmkfl.baihongyu.com/

你可能感兴趣的文章
运维知识点
查看>>
linux下定时运行打开网页url链接
查看>>
Mysql知识点
查看>>
Linux网卡高级命令、IP别名及多网卡绑定
查看>>
Git自动部署
查看>>
nginx rewrite 指令last break区别
查看>>
Go语言之类型
查看>>
把网站生成EXE文件运行,可封装网站源码/支持源码多重加密
查看>>
Goldengate单向大事务复制性能测试
查看>>
python 采用 BoundedSemaphore 限制多进程访问qps
查看>>
网络与安全
查看>>
heartbeat 编译安装配置
查看>>
第一次实际配置校园网
查看>>
httpd, php, mariadb分离式的部署在三台主机上测试性能
查看>>
PL/SQL Developer结合oracle精简客户端配置说明
查看>>
每次打开office里的word,自动出现安装配置
查看>>
Skype For Business online混合部署(二)--混合部署配置
查看>>
Dojo AMD介绍
查看>>
Rsync忽略文件夹或目录
查看>>
linux基础命令Ⅱ
查看>>