首先来认识一下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="">
<tag>设置属性:相当于setAttribute();
<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>在容器中移除var值属性,默认全部容器
<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:容器中的属性值
scope:指定的容器
代码示例:
<!-- c:remove 删除属性,默认作用域:pageScope,request等4个容器中的都会被删除-->四:<c:if test="" scope="" var=""></c:if>
<!-- 下面这一句,若没有scope属性,则前面的所有"aa"都会被清除。写了下面的scope,则只清除指定的request中的那个"aa"属性 -->
<c:remove var="aa" scope="request"/>
${aa}
<tag>判断test属性值真假,是否执行里面的代码
<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:一个返回boolean值得表达式
var:存储test返回值的变量名
scope: 存储var变量的容器
代码示例:
<!-- c:if 展示if/else-->五:<c:choose >
<c:if test="${ lis[0].age > 10 }" var="boo" scope="session">
dfdfdfdf
</c:if>
<c:if test="${!boo}">
<h2>NOooooo</h2>
</c:if>
<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 itscondition 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 thebody 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:forEach begin="" end="" step="" var="" varStatus=""></c:forEach>
<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>
增强for循环: <c:forEach items="" var="" varStatus=""></c:forEach>
<tag>第一套普通循环相当于:for( var=begin; var<=end; var+=step)
<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>
属性:begin:起始值
end:结尾值
step:步长值
var:表示目前的循环值
第二套属性搭配相当于:for( var : items )
属性:items:集合/数组的引用名
var:每一项的变量名
varStatus=""该属性可加可不加:有俩个下级变量:
index:记录当前下标值 items-var型从0开始 begin-end-step型从begin开始
count:记录当前遍历数
代码示例:
<!-- forEach的遍历功能 -->七:分隔符拆分<c:forTokens items="" delims="" var=""></c:forTokens>
<%
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>
从begin-end间,以step长度分割<c:forTokens begin="" end="" step="" var=""></c:forTokens>
<tag><c:forTokens items="" delims="" var=""></c:forTokens>型:
<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>
属性:items:要分割的表达式
delims:分隔符
var:分割的每一项
代码示例:
<!-- c:forTokens 用分隔符去拆分字符串-->八:<c:import url="" charEncoding="" context="" varReader="" scope="" var=""></c:import>相当于<jsp:include>动态导入
<c:forTokens items="aa,bb,cc,ok,jk23" delims="," var="str">
${str}
</c:forTokens>
<tag>属性:url:导入的资源路径(可访问)
<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>
代码示例:
<!-- c:import 导入资源,相当于动态包含,共享同一个request-->九:<c:url context="" scope="" value="" var=""></c:url>链接,具有重写url技术
<c:import url="/jsps/b.jsp"></c:import>
<tag>属性:value:资源路径(可访问)
<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>
代码示例
<a href="<c:url value='/jsps/i18n.jsp'/>">I18N演示</a>十:<c:redirect context="" url=""></c:redirect>重定向
<tag>属性:url:资源路径(可访问)
<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>
代码示例
<!-- c:redirect 重定向,相当于response.sendRedirect(...) -->
<%--
<c:redirect url="/jsps/aa.jsp"></c:redirect>
--%>