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的一样,同样是三步。
1、建继承simpletag类的实现类,重写dotag方法
2、tld文件中进行严格的声明
3、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
以上就是本文的全部内容,希望对大家的学习有所帮助。