JSTL jsp标准标签语言

<c:out>标签
表达形式:
<c:out value="" [escapeXml={true|false}] [default="value"]/>
value:借助el表达式进行输出,不能直接写在域中的key
如<c:out value="${abc}"/> //abc为request.setAttribute的变量
escapeXml:是否对输出进行格式话,如果内容是HTML,且属性是true的话,则直接输出。
如request.setAttribute("hello","<font color='red'>你好</font>")
  <c:out value="${hello}" escapeXml=true/>会直接输出","<font color='red'>你好</font>,如果escapeXml=false则会输出红色的你好。
default表示如果value中没有得到数据的话,会显示default的值。
如<c:out value="${hello}" default="你好defalut"/>
<c:set>标签
表达形式:
<c:set value="value" var="varName" [scope={page|request|session|application}]
说明:
value:要赋予的值,var变量的名称,scope:保存到哪个域中
如:<c:set value="123" var="num" scope="{request}"/>
<c:remove> 标签
表达式 <c:remove var="varName"/>
如:<c:remove var="hello"/>
<c:catch>
表达形式:
<c:catch [var="varName"]>
xxx
</catch>
var异常信息
xxx表示可能出现异常的代码
如:
<c:catch var="exinfo">
<%
Integer.parseInt("asdfsdf");
%>
</c:catch>
==========================
<c:if>
表达式:<c:if test="testCondition" [var="varName"] [scope="{page|request|session|application}"]>xxxx</c:if>
说明:
test表达式,返回boolean值
var表达式结果
scope表达式结果存储范围
xxx只有表达式为真时输出
如:
<c:if test="${a lt b}" var="v">
  a小于b
  v=${"v"} //相当于c:set后取值
</c:if>
<c:if test="${empty a}">
  xxx
</c:if>
<c:choose>,<c:when>,<c:otherwise>标签
三者结合使用<C:chose>必须有一个<C:when>,<C:when>必须在<C:otherwise>前面,<C:when>可以有多个,<C:otherwise>只能有一个。
表达式:
<c:choose>
    <c:when test="">
     aaa
    </c:when>
    <c:when test="">
      bbb
    </c:when>
    <c:otherwise>
     xxx
    </c:otherwise>
</c:choose>
如:
<c:choose>
<c:when test="${v1 lt v2}">
v1小于v2<br>
</c:when>
<c:otherwise>
v1大于v2<br>
</c:otherwise>
</c:choose>
<c:forEach>
表达式:
<c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
xxxx
</c:forEach>
说明:
items要输出的集合
var保存每次循环的变量
varStatus 状态
c:forEach varStatus属性
current当前这次迭代的(集合中的)项
index当前这次迭代从 0 开始的迭代索引
count当前这次迭代从 1 开始的迭代计数
first用来表明当前这轮迭代是否为第一次迭代的标志
last用来表明当前这轮迭代是否为最后一次迭代的标志

begin属性值
end属性值
step属性值

begin从哪条开始输入
end到哪条结束
step增长的幅度
如:
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty userlist}">
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${userlist}" var="user" begin="2" varStatus="vst" end="8" step="1">
<c:choose>
<c:when test="${vst.count % 2 == 0}">
<tr bgcolor="red">
</c:when>
<c:otherwise>
<tr>
</c:otherwise>
</c:choose>
<td>${user.username}</td>
<td>${user.age}</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>

如果输出为Map
<c:forEach  items="${mapvalue}" var="v">
${v.key }=${v.value }
</c:forEach>

<c:url>标签
<c:url value=”value” [context=”context”]
[var=”varName”] [scope=”{page|request|session|application}”]>
<c:param> subtags
</c:url>
Value:要提交参数的页面
Var:最后组成的URL
如:
<c:url value="http://localhost:8080/drp/sysmgr/user_add.jsp" var="v">
<c:param name="username" value="Jack"/>
<c:param name="age" value="20"/>
</c:url>
${v}

猜你喜欢

转载自jameszhao1987.iteye.com/blog/1257456