濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > JSP自定義標(biāo)簽簡單入門教程

JSP自定義標(biāo)簽簡單入門教程

熱門標(biāo)簽:南京新思維電話機(jī)器人 百度地圖添加標(biāo)注圖標(biāo)樣式 南昌市地圖標(biāo)注app 如何用中國地圖標(biāo)注數(shù)字點(diǎn) 聊城智能電銷機(jī)器人外呼 泰州泰興400電話 怎么申請(qǐng) 好操作的電話機(jī)器人廠家 地圖標(biāo)注市場(chǎng)怎么樣 企業(yè)怎么在聯(lián)通申請(qǐng)400電話

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

生存周期及調(diào)用流程

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.

簡單標(biāo)簽使用小案例

必知必會(huì):簡單標(biāo)簽也是一個(gè)標(biāo)簽,所以聲明的過程也Tag的一樣,同樣是三步。

1、建繼承SimpleTag類的實(shí)現(xiàn)類,重寫doTag方法
2、tld文件中進(jìn)行嚴(yán)格的聲明
3、jsp頁面中taglib的命名空間及標(biāo)簽前綴的聲明,然后進(jìn)行調(diào)用自定義的簡單標(biāo)簽

第一步:創(chuàng)建實(shí)現(xiàn)類:

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;


/**
 * 控制標(biāo)簽體是否執(zhí)行
 * @author Summer
 *
 */
public class BodyController extends SimpleTagSupport {
  static{
    /*
     * 簡單標(biāo)簽整體的執(zhí)行流程如下:
     * 1.瀏覽器向web服務(wù)器發(fā)送請(qǐng)求,然后web服務(wù)器調(diào)用servlet(jsp)
     * 2.complier解釋器進(jìn)行初始化工作,先是調(diào)用setJspContext方法,將pageContext對(duì)象傳遞進(jìn)去
     * 3.然后是看看此標(biāo)簽的父標(biāo)簽,即setParent方法
     * 4.再就是調(diào)用doTag方法了吧?但是要知道doTag內(nèi)部會(huì)使用JspFragment對(duì)象,所以就必須先得到它,因此應(yīng)該是調(diào)用setJspBody(JspFragment jspBody)方法
     * 5.最后是調(diào)用doTag 方法,執(zhí)行相關(guān)的代碼邏輯
     */
  }

  /**
   * 簡單標(biāo)簽可以使用這一個(gè)方法實(shí)現(xiàn)所有的業(yè)務(wù)邏輯
   */
  @Override
  public void doTag() throws JspException, IOException {
    //代表標(biāo)簽體的對(duì)象
    JspFragment fragment = this.getJspBody();
    //fragment.invoke(null);是指將標(biāo)簽中的內(nèi)容寫給誰,null代表瀏覽器


    //1.修改標(biāo)簽體的內(nèi)容
//   fragment.invoke(null);


    //2.控制標(biāo)簽體內(nèi)容的重復(fù)輸出
//   for(int i=1;i=5;i++){
//     fragment.invoke(null);//設(shè)置為null,默認(rèn)為向?yàn)g覽器輸出
//   }


    //3.修改標(biāo)簽體的內(nèi)容
    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頁面的執(zhí)行與否,只需要掌握一個(gè)原理即可
    /*
     * 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文件中進(jìn)行相關(guān)約束項(xiàng)的配置:

?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>


  !-- 控制標(biāo)簽體內(nèi)容的的簡單標(biāo)簽的自定義標(biāo)簽 -->
  tag>
    name>BodyController/name>
    tag-class>web.simpletag.BodyController/tag-class>
    body-content>scriptless/body-content>
  /tag>
/taglib>

第三步:在jsp頁面中進(jìn)行聲明然后調(diào)用:

%@ 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接口實(shí)現(xiàn)的控制標(biāo)簽體內(nèi)容是否執(zhí)行的測(cè)試頁面/title>
/head>
body>
  summer:BodyController>Summer/summer:BodyController>


/body>
/html>

總結(jié):
簡單標(biāo)簽可以替代BodyTag接口完成同樣的操作,但是有更加的簡單和輕便
簡單標(biāo)簽lifeCycle邏輯清晰,調(diào)用規(guī)則明確
使用相關(guān)流對(duì)象就可以完成對(duì)標(biāo)簽體的操控maniplate

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

您可能感興趣的文章:
  • jsp struts1 標(biāo)簽實(shí)例詳解
  • JSP自定義標(biāo)簽Taglib實(shí)現(xiàn)過程重點(diǎn)總結(jié)
  • jsp 常用標(biāo)簽的使用
  • jsp自定義標(biāo)簽技術(shù)(實(shí)現(xiàn)原理與代碼以及平臺(tái)搭建步驟)
  • JSP自定義標(biāo)簽獲取用戶IP地址的方法
  • JSP頁面中如何用select標(biāo)簽實(shí)現(xiàn)級(jí)聯(lián)
  • jsp base標(biāo)簽與meta標(biāo)簽學(xué)習(xí)小結(jié)
  • jsp簡單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符示例
  • jsp頁面中如何將時(shí)間戳字符串格式化為時(shí)間標(biāo)簽
  • JSP自定義分頁標(biāo)簽TAG全過程

標(biāo)簽:銅川 自貢 開封 吉林 烏蘭察布 山南 臨汾 白銀

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP自定義標(biāo)簽簡單入門教程》,本文關(guān)鍵詞  JSP,自定義,標(biāo)簽,簡單,入門教程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《JSP自定義標(biāo)簽簡單入門教程》相關(guān)的同類信息!
  • 本頁收集關(guān)于JSP自定義標(biāo)簽簡單入門教程的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    建湖县| 齐河县| 尚义县| 阳新县| 嘉祥县| 民权县| 桃江县| 偏关县| 鲁甸县| 福建省| 洛阳市| 三亚市| 家居| 青龙| 福清市| 晴隆县| 山阴县| 凤城市| 闵行区| 来凤县| 内黄县| 九江市| 灵璧县| 株洲市| 安溪县| 常州市| 广宗县| 囊谦县| 钟山县| 金堂县| 安福县| 丰原市| 南昌县| 静宁县| 新沂市| 江津市| 扎赉特旗| 崇义县| 湄潭县| 甘泉县| 麻栗坡县|