天天看点

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

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

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的命名空间及标签前缀的声明,然后进行调用自定义的简单标签

第一步:创建实现类:

在tld文件中进行相关约束项的配置:

第三步:在jsp页面中进行声明然后调用:

总结:

简单标签可以替代BodyTag接口完成同样的操作,但是有更加的简单和轻便

简单标签lifeCycle逻辑清晰,调用规则明确

使用相关流对象就可以完成对标签体的操控maniplate