JSTLLearning

1.条件标签

<c: if test="${user.visitCount == 1}">

   This is your first visit. Welcom to this site!

</c:if>

<c: choose>

  <c:when test="${param.name == 'zhangsan'}">

      ${param.name} is manager!

  </c:when>

  <c:when test="${param.name == 'lisi'}">

      ${param.name} is salesman!

   </c:when>

   <c:otherwise>

      ${param.name} is employee!

   </c:otherwise>

</c: choose>

2.迭代标签

用于对包含了对个对象的集合进行迭代,重复执行标签体,或者重复迭代固定的次数

<table>

 <c:forEach var="user" items="<%=arrList%>" carStatus="statues">

    <tr>

    <td>${status.count}</td>

    <td>${status.index}</td>

    <td>${status.first}</td>

    </tr>

</table>

输出100~110之间的整数数字,包括100,110;可以指定step="2",输出100, 102, 104....

<c:forEach var="i" begin="100" end="110">

  ${i}

</c:forEach>

用于迭代字符串中由分隔符分隔的各成员

<c:forTokens items="zhangsan:lisi:wangwu" delims=":" var="name">

  &{name}

</c:forTokens>

3.url相关的标签

用于导入一个基于URL的资源,可以使同一个WEB服务器下的资源,还可以是不同WEB应用程序下的资源,甚至其他网站下的资源。

如果需要跨WEB应用程序访问资源,需要在当前WEB应用程序的<Context path="..." crossContext="true"> 设置crossContext属性为true;

<c:import url=http://www.___.com/>

使用正确的url重写规则构造URL

<c:url value=htp://www.zhang.com/register var="myUrl">

  <c:param name="name" value="${param.name}"/>

  <c:param name="email" value="${param.email}"/>

</c:url>

<a href='<c:out value="&{myUrl}"/>'>Register</a>

将客户端的请求重定向到另一个资源

<c:redirect url="/XXX.jsp" context="/webName">

  <c:param name="param1"  value="X"/>

</c:redirect>

原文地址:https://www.cnblogs.com/CodeMaker/p/Jstl_2.html