jsp中jstl標籤庫core全解析
首先來認識一下jstl:
下面來介紹一下核心標籤庫:
前言:jstl是用來輔助el表示式,用來在jsp頁面顯示覆雜結構的資料
一:<c:out value="" escapeXml="" default="">:
檢視c.tld可知:
<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>
向介面輸出value值(可輸出特殊字元):
屬性解析: value: 輸出的值(el表示式,字串)
escapeXml:是否將特殊字元轉化,預設true
default: value值為null時輸出
程式碼示例:
out會將<>解析成&qt;>,這樣保證介面原樣輸出,而一般的el表示式不轉化<>,hyml會解析顏色<% pageContext.setAttribute("name", "Tom"); pageContext.setAttribute("name2", "<font color='red'>Tom</font>"); %> <c:out value="${name2}" escapeXml="true"/><br/> ${name2}<br/>
二:<c:set property="" scope="" target="" value="" var="">
設定屬性:相當於setAttribute();<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>
屬性搭配一: var:儲存容器中的屬性變數名
value:屬性值
屬性搭配二:target:容器中的一個物件
property:物件屬性名
value:屬性值
scope:容器的種類(四種,二種搭配可選可不選)
程式碼示例:
<!-- c:set 設定屬性,預設作用域:pageScope-->
<c:set var="aa" value="abc123" />
<c:set var="aa" value="ccc123" scope="request"/>
${aa}, ${requestScope.aa}
<jsp:useBean id="car" class="cn.hncu.elWeb.domain.Car"></jsp:useBean>
<c:set target="${car }" property="carno" value="No.1"></c:set>
<c:out value="${car.carno }" escapeXml="true"></c:out>
三:<c:remove var="" scope=""><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>
在容器中移除var值屬性,預設全部容器
屬性: var:容器中的屬性值
scope:指定的容器
程式碼示例:
<!-- c:remove 刪除屬性,預設作用域:pageScope,request等4個容器中的都會被刪除-->
<!-- 下面這一句,若沒有scope屬性,則前面的所有"aa"都會被清除。寫了下面的scope,則只清除指定的request中的那個"aa"屬性 -->
<c:remove var="aa" scope="request"/>
${aa}
四:<c:if test="" scope="" var=""></c:if>
<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>
判斷test屬性值真假,是否執行裡面的程式碼
屬性: test:一個返回boolean值得表示式
var:儲存test返回值的變數名
scope: 儲存var變數的容器
程式碼示例:
<!-- c:if 展示if/else-->
<c:if test="${ lis[0].age > 10 }" var="boo" scope="session">
dfdfdfdf
</c:if>
<c:if test="${!boo}">
<h2>NOooooo</h2>
</c:if>
五:<c:choose ><c:when test=""></c:when>
<c:otherwise></c:otherwise>
</c:choose>
<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>
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>
<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>
一種類似於swich-case結構
<c:choose>:無屬性,標記開始
<c:when>:相當於case可重複 屬性test:會返回為boolea值得表示式
<c:otherwise>:無屬性,相當於defaule語句
程式碼示例:
<!-- c:choose,c:when,c:otherwise 類似Java中的switch-case-default且每項自動帶break -->
<c:set var="score" value="98" />
<c:choose>
<c:when test="${score>=90}">
優秀
</c:when>
<c:when test="${score>=80}">
良好
</c:when>
<c:when test="${score>=70}">
中等
</c:when>
<c:otherwise>
不行
</c:otherwise>
</c:choose>
六:普通迴圈:<c:forEach begin="" end="" step="" var="" varStatus=""></c:forEach>增強for迴圈: <c:forEach items="" var="" varStatus=""></c:forEach>
<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>
第一套普通迴圈相當於:for( var=begin; var<=end; var+=step)屬性:begin:起始值
end:結尾值
step:步長值
var:表示目前的迴圈值
第二套屬性搭配相當於:for( var : items )
屬性:items:集合/陣列的引用名
var:每一項的變數名
varStatus=""該屬性可加可不加:有倆個下級變數:
index:記錄當前下標值 items-var型從0開始 begin-end-step型從begin開始
count:記錄當前遍歷數
程式碼示例:
<!-- forEach的遍歷功能 -->
<%
List list2 = new ArrayList();
list2.add("aa1111" );
list2.add("bb2222");
list2.add(200);
list2.add(100);
request.setAttribute("lis2", list2);
%>
<c:forEach items="${lis2}" var="aa">
${aa } ,
</c:forEach>
<%
Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "Rose");
map.put("age",55);
map.put("tel", "13566668888");
pageContext.setAttribute("m", map);
%>
<br/>
<c:forEach items="${m}" var="im">
${im.key} = ${im.value } <br/>
</c:forEach>
<%
String strs[] ={"aaa","bbb","111","2222"};
pageContext.setAttribute("strs", strs);
%>
<br/>
<c:forEach items="${strs}" var="str">
${str} ,,
</c:forEach>
<h3>看看ForEach標籤中的varStatus屬性---idx.index是元素的下標(從0開始),idx.count是元素的序號(從1開始計數)</h3>
<c:forEach items="${strs}" var="str" varStatus="idx">
${str} ---- index= ${idx.index} count= ${idx.count} <br/>
</c:forEach>
<!-- forEach的普通迴圈功能 -->
<c:forEach begin="20" end="40" var="i" step="2" varStatus="idx">
${i} --${idx.count} <br/>
</c:forEach>
七:分隔符拆分<c:forTokens items="" delims="" var=""></c:forTokens>
從begin-end間,以step長度分割<c:forTokens begin="" end="" step="" var=""></c:forTokens>
<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>
<c:forTokens items="" delims="" var=""></c:forTokens>型:
屬性:items:要分割的表示式
delims:分隔符
var:分割的每一項
程式碼示例:
<!-- c:forTokens 用分隔符去拆分字串-->
<c:forTokens items="aa,bb,cc,ok,jk23" delims="," var="str">
${str}
</c:forTokens>
八:<c:import url="" charEncoding="" context="" varReader="" scope="" var=""></c:import>相當於<jsp:include>動態匯入
<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>
屬性:url:匯入的資源路徑(可訪問)
程式碼示例:
<!-- c:import 匯入資源,相當於動態包含,共享同一個request-->
<c:import url="/jsps/b.jsp"></c:import>
九:<c:url context="" scope="" value="" var=""></c:url>連結,具有重寫url技術
<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>
屬性:value:資源路徑(可訪問)
程式碼示例
<a href="<c:url value='/jsps/i18n.jsp'/>">I18N演示</a>
十:<c:redirect context="" url=""></c:redirect>重定向
<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>
屬性:url:資源路徑(可訪問)
程式碼示例
<!-- c:redirect 重定向,相當於response.sendRedirect(...) -->
<%--
<c:redirect url="/jsps/aa.jsp"></c:redirect>
--%>