JSTL-taglib

JSTL(JSP Standard Tag Lib)

目錄:

Core

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

為jstl的核心標籤庫 c.tld:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    
  <description>JSTL 1.1 core library</description>
  <display-name>JSTL core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>c</short-name>
  <uri>http://java.sun.com/jsp/jstl/core</uri>

  <validator>
    <description>
        Provides core validation features for JSTL tags.
    </description>
    <validator-class>
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    </validator-class>
  </validator>

  <tag>
    <description>
        Catches any Throwable that occurs in its body and optionally
        exposes it.
    </description>
    <name>catch</name>
    <tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Name of the exported scoped variable for the
exception thrown from a nested action. The type of the
scoped variable is the type of the exception thrown.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
    Simple conditional tag that establishes a context for
    mutually exclusive conditional operations, marked by
    &lt;when&gt; and &lt;otherwise&gt;
    </description>
    <name>choose</name>
    <tag-class>org.apache.taglibs.standard.tag.common.core.ChooseTag</tag-class>
    <body-content>JSP</body-content>
  </tag>

  <tag>
    <description>
    Simple conditional tag, which evalutes its body if the
    supplied condition is true and optionally exposes a Boolean
    scripting variable representing the evaluation of this condition
    </description>
    <name>if</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.IfTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
The test condition that determines whether or
not the body content should be processed.
        </description>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    <type>boolean</type>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
resulting value of the test condition. The type
of the scoped variable is Boolean.        
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope for var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Retrieves an absolute or relative URL and exposes its contents
        to either the page, a String in 'var', or a Reader in 'varReader'.
    </description>
    <name>import</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ImportTag</tag-class>
    <tei-class>org.apache.taglibs.standard.tei.ImportTEI</tei-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
The URL of the resource to import.
        </description>
        <name>url</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is String.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope for var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is Reader.
        </description>
        <name>varReader</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the context when accessing a relative
URL resource that belongs to a foreign
context.
        </description>
        <name>context</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Character encoding of the content at the input
resource.
        </description>
        <name>charEncoding</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
    The basic iteration tag, accepting many different
        collection types and supporting subsetting and other
        functionality
    </description>
    <name>forEach</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
    <tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Collection of items to iterate over.
        </description>
    <name>items</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    <type>java.lang.Object</type>
        <deferred-value>
        <type>java.lang.Object</type>
        </deferred-value>
    </attribute>
    <attribute>
        <description>
If items specified:
Iteration begins at the item located at the
specified index. First item of the collection has
index 0.
If items not specified:
Iteration begins with index set at the value
specified.
        </description>
    <name>begin</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    <type>int</type>
    </attribute>
    <attribute>
        <description>
If items specified:
Iteration ends at the item located at the
specified index (inclusive).
If items not specified:
Iteration ends when index reaches the value
specified.
        </description>
    <name>end</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    <type>int</type>
    </attribute>
    <attribute>
        <description>
Iteration will only process every step items of
the collection, starting with the first one.
        </description>
    <name>step</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    <type>int</type>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility. Its type depends
on the object of the underlying collection.
        </description>
    <name>var</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
status of the iteration. Object exported is of type
javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested
visibility.
        </description>
    <name>varStatus</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
    Iterates over tokens, separated by the supplied delimeters
    </description>
    <name>forTokens</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ForTokensTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
String of tokens to iterate over.
        </description>
    <name>items</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    <type>java.lang.String</type>
        <deferred-value>
        <type>java.lang.String</type>
        </deferred-value>
    </attribute>
    <attribute>
        <description>
The set of delimiters (the characters that
separate the tokens in the string).
        </description>
    <name>delims</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    <type>java.lang.String</type>
    </attribute>
    <attribute>
        <description>
Iteration begins at the token located at the
specified index. First token has index 0.
        </description>
    <name>begin</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    <type>int</type>
    </attribute>
    <attribute>
        <description>
Iteration ends at the token located at the
specified index (inclusive).
        </description>
    <name>end</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    <type>int</type>
    </attribute>
    <attribute>
        <description>
Iteration will only process every step tokens
of the string, starting with the first one.
        </description>
    <name>step</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
    <type>int</type>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility.
        </description>
    <name>var</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
status of the iteration. Object exported is of
type
javax.servlet.jsp.jstl.core.LoopTag
Status. This scoped variable has nested
visibility.
        </description>
    <name>varStatus</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Like &lt;%= ... &gt;, but for expressions.
    </description> 
    <name>out</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.OutTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Expression to be evaluated.
        </description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Default value if the resulting value is null.
        </description>
        <name>default</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Determines whether characters &lt;,&gt;,&amp;,'," in the
resulting string should be converted to their
corresponding character entity codes. Default value is
true.
        </description>
        <name>escapeXml</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>


  <tag>
    <description>
        Subtag of &lt;choose&gt; that follows &lt;when&gt; tags
        and runs only if all of the prior conditions evaluated to
        'false'
    </description>
    <name>otherwise</name>
    <tag-class>org.apache.taglibs.standard.tag.common.core.OtherwiseTag</tag-class>
    <body-content>JSP</body-content>
  </tag>

  <tag>
    <description>
        Adds a parameter to a containing 'import' tag's URL.
    </description>
    <name>param</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ParamTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Name of the query string parameter.
        </description>
        <name>name</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Value of the parameter.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Redirects to a new URL.
    </description>
    <name>redirect</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.RedirectTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
The URL of the resource to redirect to.
        </description>
        <name>url</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the context when redirecting to a relative URL
resource that belongs to a foreign context.
        </description>
        <name>context</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Removes a scoped variable (from a particular scope, if specified).
    </description>
    <name>remove</name>
    <tag-class>org.apache.taglibs.standard.tag.common.core.RemoveTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <description>
Name of the scoped variable to be removed.
        </description>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope for var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

 <tag>
    <description>
        Sets the result of an expression evaluation in a 'scope'
    </description>
    <name>set</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.SetTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Name of the exported scoped variable to hold the value
specified in the action. The type of the scoped variable is
whatever type the value expression evaluates to.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Expression to be evaluated.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <deferred-value>
        <type>java.lang.Object</type>
        </deferred-value>
    </attribute>
    <attribute>
        <description>
Target object whose property will be set. Must evaluate to
a JavaBeans object with setter property property, or to a
java.util.Map object.
        </description>
        <name>target</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the property to be set in the target object.
        </description>
        <name>property</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope for var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Creates a URL with optional query parameters.
    </description>
    <name>url</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.UrlTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Name of the exported scoped variable for the
processed url. The type of the scoped variable is
String.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope for var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
URL to be processed.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the context when specifying a relative URL
resource that belongs to a foreign context.
        </description>
        <name>context</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
    Subtag of &lt;choose&gt; that includes its body if its
    condition evalutes to 'true'
    </description>
    <name>when</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.WhenTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
The test condition that determines whether or not the
body content should be processed.
        </description>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    <type>boolean</type>
    </attribute>
  </tag>

</taglib>
c.tld

 

用法:

標籤描述Example
<c:out> 用於在JSP中顯示資料,就像<%= ... > <c:out value="${headerV}" />
<c:set> 用於儲存資料 <c:set var="headerV" value="foooooo" />
<c:remove> 用於刪除資料 <c:remove var="headerV"/>
<c:catch> 用來處理產生錯誤的異常狀況,並且將錯誤資訊儲存起來

<c:catch var="err"><% int i = 5/0; %></c:catch>

${err.message}${err}

<c:if> 與我們在一般程序中用的if一樣 <c:if test="${!empty headerV}">***</c:if>
<c:choose> 本身只當做<c:when>和<c:otherwise>的父標籤

<c:choose>

  <c:when test="">***</c:when>

  <c:when test="">***</c:when>

  <c:otherwise>***</c:otherwise>

</c:choose>

<c:when> <c:choose>的子標籤,用來判斷條件是否成立
<c:otherwise> <c:choose>的子標籤,接在<c:when>標籤後,當<c:when>標籤判斷為false時被執行
<c:import>

檢索一個絕對或相對 URL,然後將其內容暴露給頁面,和<jsp:include

一樣

<c:import var="source" url="/index.html" charEncoding="UTF-8"/>
${source}

<c:forEach> 基礎迭代標籤,接受多種集合型別 <c:forEach var="v" items="${userList}" begin="2" end="6" step="1" varStatus="status">
  ${v.name}
  ${status.index} : ${status.count}
</c:forEach>
<c:forTokens> 根據指定的分隔符來分隔內容並迭代輸出 <c:forTokens items="zhangsan,wangwu,lisi" var="v" varStatus="status" delims=",">
  ${v} - ${status.index + 1} : ${status.count}
</c:forTokens>
<c:url> 使用可選的查詢引數來創造一個URL

<c:url var="jstl" value="./JspPage">
  <c:param name="bar" value="張三丰" />
  <c:param name="page" value="myJstl" />
</c:url>

<a href="${jstl}">跳轉到myJstl.page</a>

<c:param> 用來給包含或重定向的頁面傳遞引數
<c:redirect> 重定向至一個新的URL.直接跳轉用在<c:if>中,比如一種情況直接跳轉,另一種留在頁面 <c:redirect url="http://www.baidu.com" />
     

 

 

 

 

 

 



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Fmt

用來格式化並輸出文字、日期、時間、數字。

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

 

fmt.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 i18n-capable formatting library</description>
  <display-name>JSTL fmt</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>fmt</short-name>
  <uri>http://java.sun.com/jsp/jstl/fmt</uri>

  <validator>
    <description>
        Provides core validation features for JSTL tags.
    </description>
    <validator-class>
        org.apache.taglibs.standard.tlv.JstlFmtTLV
    </validator-class>
  </validator>

  <tag>
    <description>
        Sets the request character encoding
    </description>
    <name>requestEncoding</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.RequestEncodingTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <description>
Name of character encoding to be applied when
decoding request parameters.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Stores the given locale in the locale configuration variable
    </description>
    <name>setLocale</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.SetLocaleTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <description>
A String value is interpreted as the
printable representation of a locale, which
must contain a two-letter (lower-case)
language code (as defined by ISO-639),
and may contain a two-letter (upper-case)
country code (as defined by ISO-3166).
Language and country codes must be
separated by hyphen (-) or underscore
(_).        
    </description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Vendor- or browser-specific variant.
See the java.util.Locale javadocs for
more information on variants.
        </description>
        <name>variant</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of the locale configuration variable.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Specifies the time zone for any time formatting or parsing actions
        nested in its body
    </description>
    <name>timeZone</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.TimeZoneTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
The time zone. A String value is interpreted as
a time zone ID. This may be one of the time zone
IDs supported by the Java platform (such as
"America/Los_Angeles") or a custom time zone
ID (such as "GMT-8"). See
java.util.TimeZone for more information on
supported time zone formats.
        </description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Stores the given time zone in the time zone configuration variable
    </description>
    <name>setTimeZone</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.SetTimeZoneTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <description>
The time zone. A String value is interpreted as
a time zone ID. This may be one of the time zone
IDs supported by the Java platform (such as
"America/Los_Angeles") or a custom time zone
ID (such as "GMT-8"). See java.util.TimeZone for
more information on supported time zone
formats.
        </description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable which
stores the time zone of type
java.util.TimeZone.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of var or the time zone configuration
variable.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Loads a resource bundle to be used by its tag body
    </description>
    <name>bundle</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.BundleTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Resource bundle base name. This is the bundle's
fully-qualified resource name, which has the same
form as a fully-qualified class name, that is, it uses
"." as the package component separator and does not
have any file type (such as ".class" or ".properties")
suffix.
        </description>
        <name>basename</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Prefix to be prepended to the value of the message
key of any nested &lt;fmt:message&gt; action.
        </description>
        <name>prefix</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Loads a resource bundle and stores it in the named scoped variable or
        the bundle configuration variable
    </description>
    <name>setBundle</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.SetBundleTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <description>
Resource bundle base name. This is the bundle's
fully-qualified resource name, which has the same
form as a fully-qualified class name, that is, it uses
"." as the package component separator and does not
have any file type (such as ".class" or ".properties")
suffix.
        </description>
        <name>basename</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable which stores
the i18n localization context of type
javax.servlet.jsp.jstl.fmt.LocalizationC
ontext.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of var or the localization context
configuration variable.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Maps key to localized message and performs parametric replacement
    </description>
    <name>message</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.MessageTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Message key to be looked up.
        </description>
        <name>key</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Localization context in whose resource
bundle the message key is looked up.
        </description>
        <name>bundle</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable
which stores the localized message.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Supplies an argument for parametric replacement to a containing
        &lt;message&gt; tag
    </description>
    <name>param</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.ParamTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Argument used for parametric replacement.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Formats a numeric value as a number, currency, or percentage
    </description>
    <name>formatNumber</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.FormatNumberTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Numeric value to be formatted.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Specifies whether the value is to be
formatted as number, currency, or
percentage.
        </description>
        <name>type</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Custom formatting pattern.
        </description>
        <name>pattern</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
ISO 4217 currency code. Applied only
when formatting currencies (i.e. if type is
equal to "currency"); ignored otherwise.
        </description>
        <name>currencyCode</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Currency symbol. Applied only when
formatting currencies (i.e. if type is equal
to "currency"); ignored otherwise.
        </description>
        <name>currencySymbol</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Specifies whether the formatted output
will contain any grouping separators.
        </description>
        <name>groupingUsed</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Maximum number of digits in the integer
portion of the formatted output.
        </description>
        <name>maxIntegerDigits</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Minimum number of digits in the integer
portion of the formatted output.
        </description>
        <name>minIntegerDigits</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Maximum number of digits in the
fractional portion of the formatted output.
        </description>
        <name>maxFractionDigits</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Minimum number of digits in the
fractional portion of the formatted output.
        </description>
        <name>minFractionDigits</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable
which stores the formatted result as a
String.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Parses the string representation of a number, currency, or percentage
    </description>
    <name>parseNumber</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.ParseNumberTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
String to be parsed.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Specifies whether the string in the value
attribute should be parsed as a number,
currency, or percentage.
        </description>
        <name>type</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Custom formatting pattern that determines
how the string in the value attribute is to be
parsed.
        </description>
        <name>pattern</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Locale whose default formatting pattern (for
numbers, currencies, or percentages,
respectively) is to be used during the parse
operation, or to which the pattern specified
via the pattern attribute (if present) is
applied.
        </description>
        <name>parseLocale</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Specifies whether just the integer portion of
the given value should be parsed.
        </description>
        <name>integerOnly</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable which
stores the parsed result (of type
java.lang.Number).
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Formats a date and/or time using the supplied styles and pattern
    </description>
    <name>formatDate</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.FormatDateTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <description>
Date and/or time to be formatted.
        </description>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Specifies whether the time, the date, or both
the time and date components of the given
date are to be formatted. 
        </description>
        <name>type</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Predefined formatting style for dates. Follows
the semantics defined in class
java.text.DateFormat. Applied only
when formatting a date or both a date and
time (i.e. if type is missing or is equal to
"date" or "both"); ignored otherwise.
        </description>
        <name>dateStyle</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Predefined formatting style for times. Follows
the semantics defined in class
java.text.DateFormat. Applied only
when formatting a time or both a date and
time (i.e. if type is equal to "time" or "both");
ignored otherwise.
        </description>
        <name>timeStyle</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Custom formatting style for dates and times.
        </description>
        <name>pattern</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Time zone in which to represent the formatted
time.
        </description>
        <name>timeZone</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable which
stores the formatted result as a String.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <description>
        Parses the string representation of a date and/or time
    </description>
    <name>parseDate</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.ParseDateTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Date string to be parsed.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Specifies whether the date string in the
value attribute is supposed to contain a
time, a date, or both.
        </description>
        <name>type</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Predefined formatting style for days
which determines how the date
component of the date string is to be
parsed. Applied only when formatting a
date or both a date and time (i.e. if type
is missing or is equal to "date" or "both");
ignored otherwise.
        </description>
        <name>dateStyle</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Predefined formatting styles for times
which determines how the time
component in the date string is to be
parsed. Applied only when formatting a
time or both a date and time (i.e. if type
is equal to "time" or "both"); ignored
otherwise.
        </description>
        <name>timeStyle</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Custom formatting pattern which
determines how the date string is to be
parsed.
        </description>
        <name>pattern</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Time zone in which to interpret any time
information in the date string.
        </description>
        <name>timeZone</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Locale whose predefined formatting styles
for dates and times are to be used during
the parse operation, or to which the
pattern specified via the pattern
attribute (if present) is applied.
        </description>
        <name>parseLocale</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable in
which the parsing result (of type
java.util.Date) is stored.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope of var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

</taglib>
fmt.tld

 

用法:

標籤描述Example
<fmt:formatNumber> 使用指定的格式或精度格式化數字  

<fmt:formatNumber var="cur" value="1235.6699" type="currency"/>

<fmt:formatNumber var="cur" value="56" type="percent" />
<fmt:formatNumber var="cur" value="000056" type="number" maxIntegerDigits="3" />

<fmt:parseNumber> 解析一個代表著數字,貨幣或百分比的字串  
<fmt:formatDate> 使用指定的風格或模式格式化日期和時間  <fmt:formatDate value="${yearMonth}" pattern="yyyy-MM" />
<fmt:parseDate> 解析一個代表著日期或時間的字串  <fmt:parseDate value="${item.expDate}" var="yearMonth" pattern="yyyy-MM-dd"/>
<fmt:bundle> 繫結資源  
<fmt:setLocale> 指定地區
<fmt:setLocale value="en_US"/>
<fmt:setBundle> 繫結資源  
<fmt:timeZone> 指定時區  
<fmt:setTimeZone> 指定時區  
<fmt:message> 顯示資源配置文件資訊  
<fmt:requestEncoding> 設定request的字元編碼  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Function

JSTLFn方法庫

該庫中提供了許多方法和函式,主要用於對字串的處理。fn庫中的方法和函式不是標籤,所以用法和標籤不同。這些方法通常在EL表示式中使用

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

 具體方法的使用方式參照fn.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 functions library</description>
  <display-name>JSTL functions</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>fn</short-name>
  <uri>http://java.sun.com/jsp/jstl/functions</uri>

  <function>
    <description>
      Tests if an input string contains the specified substring.
    </description>
    <name>contains</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>boolean contains(java.lang.String, java.lang.String)</function-signature>
    <example>
      &lt;c:if test="${fn:contains(name, searchString)}">
    </example>
  </function>

  <function>
    <description>
      Tests if an input string contains the specified substring in a case insensitive way.
    </description>
    <name>containsIgnoreCase</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>boolean containsIgnoreCase(java.lang.String, java.lang.String)</function-signature>
    <example>
      &lt;c:if test="${fn:containsIgnoreCase(name, searchString)}">
    </example>
  </function>

  <function>
    <description>
      Tests if an input string ends with the specified suffix.
    </description>
    <name>endsWith</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>boolean endsWith(java.lang.String, java.lang.String)</function-signature>
    <example>
      &lt;c:if test="${fn:endsWith(filename, ".txt")}">
    </example>
  </function>

  <function>
    <description>
      Escapes characters that could be interpreted as XML markup.
    </description>
    <name>escapeXml</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String escapeXml(java.lang.String)</function-signature>
    <example>
      ${fn:escapeXml(param:info)}
    </example>
  </function>

  <function>
    <description>
      Returns the index withing a string of the first occurrence of a specified substring.
    </description>
    <name>indexOf</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>int indexOf(java.lang.String, java.lang.String)</function-signature>
    <example>
      ${fn:indexOf(name, "-")}
    </example>
  </function>

  <function>
    <description>
      Joins all elements of an array into a string.
    </description>
    <name>join</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String join(java.lang.String[], java.lang.String)</function-signature>
    <example>
      ${fn:join(array, ";")}
    </example>
  </function>

  <function>
    <description>
      Returns the number of items in a collection, or the number of characters in a string.
    </description>
    <name>length</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>int length(java.lang.Object)</function-signature>
    <example>
      You have ${fn:length(shoppingCart.products)} in your shopping cart.
    </example>
  </function>

  <function>
    <description>
      Returns a string resulting from replacing in an input string all occurrences
      of a "before" string into an "after" substring.
    </description>
    <name>replace</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String replace(java.lang.String, java.lang.String, java.lang.String)</function-signature>
    <example>
      ${fn:replace(text, "-", "&#149;")}
    </example>
  </function>

  <function>
    <description>
      Splits a string into an array of substrings.
    </description>
    <name>split</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String[] split(java.lang.String, java.lang.String)</function-signature>
    <example>
      ${fn:split(customerNames, ";")}
    </example>
  </function>

  <function>
    <description>
      Tests if an input string starts with the specified prefix.
    </description>
    <name>startsWith</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>boolean startsWith(java.lang.String, java.lang.String)</function-signature>
    <example>
      &lt;c:if test="${fn:startsWith(product.id, "100-")}">
    </example>
  </function>

  <function>
    <description>
      Returns a subset of a string.
    </description>
    <name>substring</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String substring(java.lang.String, int, int)</function-signature>
    <example>
      P.O. Box: ${fn:substring(zip, 6, -1)}
    </example>
  </function>

  <function>
    <description>
      Returns a subset of a string following a specific substring.
    </description>
    <name>substringAfter</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String substringAfter(java.lang.String, java.lang.String)</function-signature>
    <example>
      P.O. Box: ${fn:substringAfter(zip, "-")}
    </example>
  </function>

  <function>
    <description>
      Returns a subset of a string before a specific substring.
    </description>
    <name>substringBefore</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String substringBefore(java.lang.String, java.lang.String)</function-signature>
    <example>
      Zip (without P.O. Box): ${fn:substringBefore(zip, "-")}
    </example>
  </function>

  <function>
    <description>
      Converts all of the characters of a string to lower case.
    </description>
    <name>toLowerCase</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String toLowerCase(java.lang.String)</function-signature>
    <example>
      Product name: ${fn.toLowerCase(product.name)}
    </example>
  </function>

  <function>
    <description>
      Converts all of the characters of a string to upper case.
    </description>
    <name>toUpperCase</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature>
    <example>
      Product name: ${fn.UpperCase(product.name)}
    </example>
  </function>

  <function>
    <description>
      Removes white spaces from both ends of a string.
    </description>
    <name>trim</name>
    <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
    <function-signature>java.lang.String trim(java.lang.String)</function-signature>
    <example>
      Name: ${fn.trim(name)}
    </example>  
  </function>

</taglib>
fn.tld

 

有時現有的function並不能滿足我們的需求,需要自定義function

 1.建立java的靜態方法

package jsp2.el;

import java.util.Locale;

public class FunctionsTag {

    /**
     * 翻轉字串
     * @param obj
     * @return
     */
    public static String reverse(Object obj) {
        String str = String.valueOf(obj);
        char[] charArray = str.toCharArray();
        int n = charArray.length - 1;
        for(int i=0;i <= n >> 1; i++) {
            char temp = charArray[i];
            charArray[i] = charArray[n - i];
            charArray[n - i] = temp;
        }
        str = new String(charArray);
        // new StringBuffer(str).reverse();推薦這種方式
        return str;
    }
    
    /**
     * 獲取母音的個數
     * @param str
     * @return
     */
    public static int numVowels(String str) {
        String substr = str.replaceAll("[aoeiuAOEIU]", "");
        return str.length() - substr.length();
        
        //char[] charArray = str.toCharArray();
        //int num = 0;
        //for(char cha : charArray) {
        //    if("aoeiuAOEIU".indexOf(cha) > -1) {
        //        num++;
        //    }
        //}
        //return num;
    }
    
    /**
     * 字母大寫
     * @param str
     * @return
     */
    public static String toCapital(String str) {
        return str.toUpperCase(Locale.ENGLISH);
    }
    
    
    public static void main(String[] args) {
        System.out.println(reverse("z"));
        System.out.println(numVowels("stieohoj"));
        System.out.println(toCapital("stieohoj2342"));
    }
}
MyFunction

 

2. 在WEB-INF下建立tlds文件夾,建立tld文件(xml文件建立後,字尾變為tld即可,標籤庫文件)

頭資訊將fn.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">
    
    <short-name>myFn</short-name>
    <uri>/myFunction</uri>
    <tlib-version>1.0.0</tlib-version>
    
    <function>
        <description>string reverse</description>
        <name>reverse</name>
        <function-class>jsp2.el.FunctionsTag</function-class>
        <function-signature>java.lang.String reverse(java.lang.Object)</function-signature>
    </function>
    <function>
        <description>get count of vowels(a o e i u)</description>
        <name>numVowels</name>
        <function-class>jsp2.el.FunctionsTag</function-class>
        <function-signature>int numVowels(java.lang.String)</function-signature>
    </function>
    <function>
        <description>upperCase x- X </description>
        <name>toCapital</name>
        <function-class>jsp2.el.FunctionsTag</function-class>
        <function-signature>java.lang.String toCapital(java.lang.String)</function-signature>
    </function>
    </taglib>
myFn.tld

 

※注意: <function-signature> 引數需要寫完整的類名,假如是String型別的話就必須寫java.lang.String,基本資料型別直接寫,不支援泛型的定義。呼叫方法的時候必須型別要傳入相同型別的值,否則的話會報錯,不過對於方法體是String的話,是可以傳入list,set,map那些,因為傳入後會直接呼叫list的.toString()方法直接當字串輸出。

應用頁面:

<%@ taglib prefix="myFn" uri="/WEB-INF/tlds/myFunction.tld"%>

    ${myFn:reverse('zhangsan')}
    ${myFn:numVowels('zhangsan')}
    ${myFn:toCapital('zhangsan')}

 

頭部資訊除了可以直接指定文件位置,還可以通過在tld文件中指定url的形式訪問。

myFn.tld新增: <uri>/jsp/jstl/myFunction</uri>

 page頁面:      <%@ taglib prefix="myFn" uri="/jsp/jstl/myFunction"%>

※注意: uri地址更改後需要重新啟動服務才可生效。

 

SimpleTagSupport(jsp 自定義標籤)

jsp2.0早期 JSP 自定義標籤類(TagSupport和BodyTagSupport之後)開發過程略微複雜一些,但 JSP 2 已經簡化了這個過程,它只要自定義標籤類都必須繼承一個父類:javax.servlet.jsp.tagext.SimpleTagSupport。

<?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">
    
    <short-name>myFn</short-name>
    <uri>/jsp/jstl/myTags</uri>
    <tlib-version>1.0.0</tlib-version>
    
    <tag>
        <description>a simple tag, no body hello world.</description>
        <name>TagHello</name>
        <tag-class>jsp2.tag.TagHello</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>hello</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>world</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

</taglib>
myTags.tld

 

package jsp2.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 簡單tag編寫
 * 
 * @author DennyZhao
 * @date 2017年11月9日
 * @version 1.0
 */
public class TagHello extends SimpleTagSupport {
    
    private String hello;
    private String world;

    @Override
    public void doTag() throws JspException, IOException {
        String str = String.format("%2$s,This %1$s", hello, world);
        getJspContext().getOut().append(str);
    }
    
    public String getHello() {
        return hello;
    }

    public void setHello(String hello) {
        this.hello = hello;
    }

    public String getWorld() {
        return world;
    }

    public void setWorld(String world) {
        this.world = world;
    }
}
TagHello.java
頁面:
<%@ taglib prefix="myTag" uri="/jsp/jstl/myTags"%> <myTag:TagHello hello="hello" world="world"/> -------------------------------------------------------------- 結果:
world,This hello

重複輸出:

    <tag>
        <description>repeat the content html</description>
        <name>TagRepeat</name>
        <tag-class>jsp2.tag.TagRepeat</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>count</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
        for(int i = 0;i < count;i++) {
            getJspContext().getOut().append("<span style='color:red;'>index----:"+ i + "</span></br>");
            getJspContext().setAttribute("index", i);
            getJspBody().invoke(null);
        }

 

    <myTag:TagRepeat count="8">
        ${index} out:
    </myTag:TagRepeat>
--------------------------------------------------------
結果:
index----:0
0 out: index----:1
1 out: index----:2
2 out: index----:3
3 out: index----:4
4 out: index----:5
5 out: index----:6
6 out: index----:7
7 out:

Bean物件的植入:

        Book book = new Book(123, "hello", "Denny", "hello this world!");
        getJspContext().setAttribute("book", book);

--------

        <tr><td>編號</td><td>${book.bookId}</td></tr>
        <tr><td>名稱</td><td>${book.bookName}</td></tr>
        <tr><td>作者</td><td>${book.bookAuthor}</td></tr>
        <tr><td>描述</td><td>${book.bookDes}</td></tr>

------
結果:

編號    123
名稱    hello
作者    Denny
描述    hello this world!

 

tag中巢狀tag:

    <myTag:TagRepeat count="8">
        ${index} out:
        <myTag:TagHello hello="hello" world="world"/>
    </myTag:TagRepeat>

-----------------------------------------
結果:

index----:0
0 out: world,This hello index----:1
1 out: world,This hello index----:2
2 out: world,This hello index----:3
3 out: world,This hello index----:4
4 out: world,This hello index----:5
5 out: world,This hello index----:6
6 out: world,This hello index----:7
7 out: world,This hello

 

tld文件內容說明:

body-content:這個屬性也很重要,它指定標籤體內容。該元素的值可以是如下幾個:

  • tagdependent:標籤體內容直接被寫入BodyContent,由自定義標籤類來進行處理,而不被JSP容器解釋。即EL表示式內容不被識別直接輸出到頁面${EL}
  • empty:指定該標籤只能作用空標籤使用。
  • scriptless:指定該標籤的標籤體可以是靜態 HTML 元素,表示式語言,但不允許出現 JSP 指令碼。
  • jsp: 已不再推薦使用 JSP 指令碼,所以 JSP 2 自定義標籤的標籤體中不能包含 JSP 指令碼。

rtexprvalue: 全稱是 Run-time Expression Value, 它用於表示是否能夠利用JSP表白式,在使用標籤時能不能使用表示式來動態指定資料。true是可以,false是不可以。

即是否允許引數中存在:<myTag:TagHello hello="<c:out value='hello'/>" world="${world}"  he="<%=he %>"/>

fragment:   使用frag片段,在引數定義和使用以下形式。

---------jsp頁面:    
<myTag:TagRepeat count="8">
        <jsp:attribute name="frag1">
            ${index} out:
            <myTag:TagHello hello="hello" world="world"/>
        </jsp:attribute>
</myTag:TagRepeat>

---------tld頁面
        <attribute>
            <name>frag1</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <fragment>true</fragment>
        </attribute>

---------java後臺:

private JspFragment frag1;

frag1.invoke(***);

 

※ 注意:1. 使用fragment時,fragment引數不能寫在屬性上,不然會被當做String,出現ClassCast錯誤。

     2. 同時如果使用內嵌<jsp:attribute 這種形式,在標籤中最好不要再有任何內容,會出現某個屬性出現空指標異常。

     3.rtexprvalue,fragment 不能同時存在在一個屬性上。

     4. fragment操作不當會使得服務Server陷入卡頓狀態,需要殺掉程序才可以,因此使用時需謹慎。

<myTag:TagRepeat count="8">
       234234324324
        <jsp:attribute name="frag1">
            ${index} out:
            <myTag:TagHello hello="hello" world="world"/>
        </jsp:attribute>
</myTag:TagRepeat>

 

  這種就會出現2的問題。

Tag File

tag File文件位置要求必須在WEB-INF/tags/下才可以。

文件建立方式: new -》 JSP Tag 文件

-----jsp Page
<
%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

<tags:hello/>

 

----hello.tag:
<
%@ tag language="java" pageEncoding="UTF-8"%> Hello This World!

 

新增引數使用:

------jsp頁面:
<tags:hello hello="hi" world="helloWorld" color="red">
<div>I'm a Panel Test</div>
</tags:hello>

-----tag頁面:
<%@ attribute name="hello" required="true" rtexprvalue="true" %>
<%@ attribute name="world" required="true" rtexprvalue="true" %>
<%@ attribute name="color" required="false" rtexprvalue="true" %>

<div style="color:${color}">${hello} This ${world}!</div>
<jsp:doBody/>


------結果:
hi This helloWorld!
I'm a Panel Test

 

※注意:<jsp:doBody></jsp:doBody>之間不能新增任何內容即應該是<jsp:doBody />.不然會出現屬性異常的情況。

新增 fragment使用:

-----jsp 頁面:
<tags:hello hello="hi" world="helloWorld" color="red">
<jsp:attribute name="header">
    header
</jsp:attribute>
<jsp:attribute name="body">
    <div>I'm a Panel Test</div>
</jsp:attribute>
<jsp:attribute name="footer">
    footer
</jsp:attribute>
</tags:hello>

-------tag文件
<%@ attribute name="header" fragment="true" %>
<%@ attribute name="body" fragment="true" %>
<%@ attribute name="footer" fragment="true" %> 
<div style="color:${color}">${hello} This ${world}!</div>
<div>Header
<jsp:invoke fragment="header" />
</div>
<div>BOdy
<jsp:invoke fragment="body" />
</div>
<div>FOOTER
<jsp:invoke fragment="footer" />
</div>

----------------結果:
hi This helloWorld!
Header header
BOdy
I'm a Panel Test
FOOTER footer 

 

 ※ 注意rtexprvalue,fragment 不能同時存在在一個屬性上。

variable 使用:

  tag 文件通過使用 variable 指令可以將 tag 文件中的物件返回給呼叫該 tag 文件的 JSP頁面.

  引數說明:     

   Scope :可選項 AT_BEGIN(標籤後jsp page內有效), AT_END(標籤後jsp page內有效) and NESTED. NESTED(預設,標籤內有效)

  variable-class:可選項 預設java.lang.String

  alias:可選項 別名,(name-from-attribute 選擇時為必須項)

  name-given:(必選項與 name-from-attribute 選一)

  name-from-attribute: (必選項與 name-given 選一,使用某個attribute的值作為name)。

  因tag文件多用於共同jsp設定,因此有可能會出現tag定義的變數輸出,和jsp原定義的輸出名字衝突,因此可通過在jsp定義輸出名實現(name-from-attribute)。

 

----tag文件:
<
%@ variable name-given="headerV" scope="NESTED" variable-class="java.lang.String"%> <%@ variable name-given="bodyV"%> <%@ variable name-from-attribute="hello" alias="footerV"%> <c:set var="headerV" value="我是頭部"></c:set> <c:set var="bodyV" value="我是身子"></c:set>

---- jsp頁面:

<jsp:attribute name="header">
${headerV}
</jsp:attribute>
<jsp:attribute name="body">
<div>I'm a Panel Test</div>${bodyV}
</jsp:attribute>
<jsp:attribute name="footer">
footer${footerV}
</jsp:attribute>

 

 

 

 

以下為name-from-attribute設定:

 

--------jsp頁面:
<c:set  var="headerV" value="foooooo"></c:set>
<tags:hello hello="hi" world="helloWorld" color="red">
<jsp:attribute name="header">
    ${headerV}
</jsp:attribute>
.........

------Tag文件:
<%@ variable name-given="headerV" scope="NESTED" variable-class="java.lang.String"%>
<%@ variable name-given="bodyV"%>
<%@ variable name-from-attribute="hello" alias="v"%>
<c:set var="headerV" value="我是頭部"></c:set>

--------------說明:
從以上可以看出,我在jsp頁面定義了headerV,本來想輸出${headerV} =foooooo
但在tag中又存在相同名字的headerV,導致結果為 我是頭部

 

 

改為以下方式,輸出的變數名在jsp定義:

---Tag文件:
<%@ attribute name="hello" required="true" rtexprvalue="false" %>
<%@ variable name-from-attribute="hello" alias="foo"%>
<c:set var="foo" value="我是頭部"></c:set>

---jsp頁面:
<c:set  var="headerV" value="foooooo"></c:set>
<tags:hello hello="headerB" world="helloWorld" color="red">
<jsp:attribute name="header">
    ${headerV}
    ${headerB}

※注意:

  1. variable使用【name-from-attribute】時,【alias】為必須項

  2.  name-from-attribute為某個屬性時,attribute的屬性rtexprvalue=false,required=true,type=java.lang.string才可。

參考文章: 

jstl標籤庫fn函式使用(http://blog.csdn.net/javaloveiphone/article/details/50216193)

[教程]自定義JSP中的Taglib標籤之四自定義標籤中的Function函式(https://www.cnblogs.com/edwardlauxh/archive/2010/05/19/1918589.html)

[教程]自定義JSP中的Taglib標籤之五自定義標籤之SimpleTagSupport使用(http://www.cnblogs.com/edwardlauxh/archive/2010/05/20/1918588.html)

利用 JSP 2 提供的 SimpleTagSupport 開發自定義標籤(https://www.ibm.com/developerworks/cn/java/j-lo-jsp2tag/)

JSP2.0自定義標籤(http://blog.csdn.net/dyyaries/article/details/9960987

 

關鍵詞:name description attribute rtexprvalue required the tag function false class

相關推薦:

ZABBIX 3.4 監控伺服器TCP連線狀態(六)

配置WEB錯誤頁面

html標籤學習1

【練習】Html

maven runnable jar

WebService--CXF與Spring的整合(jaxws:endpoint形式配置)以及客戶端呼叫(spring配置文件形式,不需要生成客戶端程式碼)

常用框架(一):spring+springMvc+mybatis+maven

Hbuilder護眼主題分享