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 <when> and <otherwise> </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 <%= ... >, 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 <,>,&,'," 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 <choose> that follows <when> 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 <choose> 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>
用法:
标签 | 描述 | 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"/> |
<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"> <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 <fmt:message> 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 <message> 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>
用法:
标签 | 描述 | Example |
---|---|---|
<fmt:formatNumber> | 使用指定的格式或精度格式化数字 |
<fmt:formatNumber var="cur" value="1235.6699" type="currency"/> <fmt:formatNumber var="cur" value="56" type="percent" /> |
<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
JSTL的Fn方法库
该库中提供了许多方法和函数,主要用于对字符串的处理。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> <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> <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> <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, "-", "•")} </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> <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>
有时现有的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")); } }
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>
※注意: <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>
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; } }
页面:
<%@ 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>
----------------结果:
※ 注意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才可。
参考文章: