麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JSP > 正文

JSP自定義標簽簡單入門教程

2024-09-05 00:22:28
字體:
來源:轉載
供稿:網友
這篇文章主要為大家詳細介紹了JSP自定義標簽簡單入門教程,感興趣的小伙伴們可以參考一下
 

在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的一樣,同樣是三步。

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

以上就是本文的全部內容,希望對大家的學習有所幫助。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产午夜精品视频免费不卡69堂 | 午夜丰满少妇高清毛片1000部 | 久久国产精品久久久久 | 97中文字幕第一一一页 | 欧美中文字幕在线视频 | 国产成人精品午夜视频' | 99视频有精品视频高清 | 91一级毛片 | 素人视频免费观看 | 史上最强炼体老祖动漫在线观看 | 国产一级片91 | 成人在线精品视频 | 精精国产xxxx视频在线播放7 | 嗯~啊~用力~高h | 一级黄色影院 | 久色视频网站 | 国产福利不卡一区二区三区 | 久久男人天堂 | 香蕉在线看 | 国产女同疯狂激烈互摸 | 成人aaaaa片毛片按摩 | 久久爽精品区穿丝袜 | 国产成人精品一区在线播放 | 永久免费黄色片 | 最新午夜综合福利视频 | 成人福利软件 | 一本视频在线观看 | 免费久久精品 | 欧美城网站地址 | 蜜桃视频日韩 | 国产成人在线网址 | 国产亚洲精品久久久久久久久 | 国产艳妇av视国产精选av一区 | 精品在线免费播放 | 999精品久久久 | 亚洲欧美国产高清 | 久久国产一二三 | 圆产精品久久久久久久久久久 | 成人一级视频 | 黄网站在线免费看 | 免费午夜视频在线观看 |