JSTL(core)

来源:互联网 发布:ubuntu timidity 编辑:程序博客网 时间:2024/06/04 18:46

JSTL

*动态的值:java、EL表达式、通过<jsp:attribute>设置的值

core标签库
1、out
计算一个表达式并将结果输出到当前的JspWriter对象
<c:out value="动态的值" escapeXml=“true|false” defualt="defualtValue"/>
<c:out value="动态的值" escapeXml = "true|false">
 defualtValue
</c:out>
escapeXml:结果中的<,>,&等要不要被转换成对应的字符活预定义的实体,默认true


2、set
设置范围变量的值或者javaBean的值
<c:set value="动态的值" var="varName" scope="page|request|session|application"/>
<c:set var="varName" scop="page|request|session|application">
 动态的值
</c:set>
设置键值对;
<c:set value="动态的值" target="target" property="propertyName"/>
<c:set target="target" property="propertyName">
 动态的值(target)
</c:set>
target:要设置属性的对象,必须使有setter方法的javaBean或者Map对象
<c:set var="customerTable" scope="application">
<table border="1">
    <c:forEach var="customer" items="${customers}">
    <tr>
   <td>${customer.lastName}</td>
   <td><c:out value="${customer.address}" default="no address specified"/></td>
   <td>
     <c:out value="${customer.address}">
    <font color="red">no address specified</font>
  </c:out>
      </td>
 </tr>
  </c:forEach>
</table>
</c:set>
居然可用设置一段html代码

3、remove
移除范围变量的值
<c:remove var="varName" scope="page|request|session|application"/>

4、catch
用于捕获在其中嵌套的操作所抛出的异常对象,并将异常信息保存在变量中
<c:catch var="varName">
 action
</c:catch> 
变量是page范围,可用${varName.message}调用

5、if
实现java语言的if语句功能
<c:if test="testCondition" var="varName" scope="page|request|session|application"/>
<c:if test="testCondition" var="varName" scope="page|request|session|application">
 body content
</c:if>
testCondition:测试的条件
var:保存测试结果的变量,Boolean型
<c:if test="${customer.address.country == 'USA'}">
    ${customer}<br>
</c:if>


6、choose
choose、when、otherwise一起实现互斥条件的执行,类似于if/else if/else
<c:choose>
 body content(只能包含<c:when>和<c:otherwise>)
</c:choose>

7、when
为choose的一个子标签,表示一个可选条件
<c:when test="testCondition">
 body content
</c:when>

8、otherwise
为choose的子标签,表示一个最后的选择
<c:otherwise>
 conditional block
</c:otherwise>
EX:  <c:choose>
    <c:when test="${customer.address.country == 'USA'}">
      <font color="blue">
    </c:when>
    <c:when test="${customer.address.country == 'Canada'}">
      <font color="red">
    </c:when>
    <c:otherwise>
      <font color="green"> 
    </c:otherwise> 
  </c:choose>${customer}</font><br>

9、forEach
对包含了多个对象的集合进行迭代,重发行它的标签体,或者重复迭代过顶的次数
多个对象:
<c:forEach var="varName" items="collection" varStatus="varStatus" begin="begin" end="end" step="step">
 body content
</c:forEach>
固定的次数:
<c:forEach var="varName" varStatus="varStatus" begin="begin" end="end" step="step">
 body content
</c:forEach>

var:保存了当前迭代条目的范围变量的名称
varStatus:保存了当前迭代状态的范围变量的名称
javax.servlet.jsp.jstl.core
Interface LoopTagStatus
 Integer getBegin()
          Returns the value of the 'begin' attribute for the associated tag, or null if no 'begin' attribute was specified.
 int getCount()
          Retrieves the "count" of the current round of the iteration.
 Object getCurrent()
          Retrieves the current item in the iteration.
 Integer getEnd()
          Returns the value of the 'end' attribute for the associated tag, or null if no 'end' attribute was specified.
 int getIndex()
          Retrieves the index of the current round of the iteration.
 Integer getStep()
          Returns the value of the 'step' attribute for the associated tag, or null if no 'step' attribute was specified.
 boolean isFirst()
          Returns information about whether the current round of the iteration is the first one.
 boolean isLast()
          Returns information about whether the current round of the iteration is the last one.

EX: 1<c:forEach var="token" items="bleu,blanc,rouge">
    <c:out value="${token}"/><br>
 </c:forEach>

 2<c:forEach var="customer" items="${customers}" varStatus="status">
    <tr>
      <td><c:out value="${status.index}"/></td>
      <td><c:out value="${status.count}"/></td>
      <td><c:out value="${status.current.lastName}"/></td>
      <td><c:out value="${status.current.firstName}"/></td>
      <td><c:out value="${status.first}"/></td>
      <td><c:out value="${status.last}"/></td>
    </tr>
    <c:if test="${status.last}">
      <c:set var="count" value="${status.count}"/>
    </c:if> 
  </c:forEach>


10、forTokens
用于迭代由字符分割的字符串
<c:forTakens items="String" delims="delimiters" var="varName" varStatus="varStatusName" begin="begin" end="end" step="step">
 body content
</c:forTokens>

delims:分割符号
items:字符对象

<c:forTokens var="token" items="bleu,blanc,rouge|vert,jaune|blanc,rouge"
              delims="|">
  <c:out value="${token}"/> •
</c:forTokens>

<h4>String with '|' and ',' delimiters</h4>

<c:forTokens var="token" items="bleu,blanc,rouge|vert,jaune|blanc,rouge"
              delims="|,">------两个字符也可以
  <c:out value="${token}"/> •

url相关标签

11、import
可以导入一个基于url的资源,不仅能导入同一个web应用程序下的资源,还可以导入不同应用程序下的资源,甚至是其他网站的资源
--资源的内容作为String被导出:
<c:import url="url" context="content" var="varName" scope="page|request|session|application" charEncoding="charEncoding">
 <c:param>
</c:import>
var:保存了被导出资源内容的范围变量的名称,String型
--资源的内容作为Reader被导出:
<c:import url="url" context="content" varReader="varReaderName" charEncoding="charEncoding">
</c:import>
varReader只能在<c:import>开始到结束标签之间使用
url参数支持绝对和相对URL,绝对url中当前环境的一切都不能使用

12、url
用正确的rul重写规则构造一个URL
--没有标签体
<c:url value="value" context="context" var="varName" scope="page|request|session|application"/>
--有标签体
<c:url value="value" context="context" var="varName" scope="page|request|session|application">
 <c:param>
</c:url>
只有相对url会被重写,如果要采用url进行会话跟踪就必须使用相对url。默认情况处理得结果会被写入到当前的JspWriter对象中,如果指定了
var属性,则结果将被保存到JSP范围变量中

13、redirect
将客户的请求重定向到另一个资源
<c:redirect url="value" context="context"/>
<c:redirect url="value" context="context">
 <c:param>
</c:redirect>

14、param
为url添加一个参数
<c:param name="name" value="value"/>
<c:param name="name">
 value
</c:param>

原创粉丝点击