论坛首页 Java企业应用论坛

JSTL 详解01

浏览 2709 次
锁定老帖子 主题:JSTL 详解01
该帖已经被评为隐藏帖
作者 正文
   发表时间:2010-08-12   最后修改:2010-09-10

General-Purpose Actions

<c:out>

Without a body

<c:out value=”value” [escapeXml=”{true|false}”]

[default=”defaultValue”] />

With a body

<c:out value=”value” [escapeXml=”{true|false}”]>

default value

</c:out>

参数说明

Value:需要借助EL表达式来进行输出,不能直接写在域中的Key

escapeXml:是否对输出进行XML格式化,如果输出内容是HTML且该属性为true的话则直接输出

default:默认输出值,也可以如实例在标签内写默认值

如:

<c:out value="${abc}" default="123"/>
<c:out value="${abc}">123</c:out>
如果域内有Key为abc的值则输出否则输出默认值 123
<c:out value="${bj}"/>
<c:out value="${bj}" escapeXml="true"/>
<c:out value="${bj}" escapeXml="false"/>
${bj }
request.setAttribute("bj", "<font color='red'>北京欢迎您</font>");

 

 

 

 

直接输出,escapeXml="true"和使用EL表达式输出都会直接输出bj的值,而escapeXml="false"会输出字体为红色的对应值

 

<c:set>

Syntax 1: Set the value of a scoped variable using attribute value

<c:set value=”value

var=”varName” [scope=”{page|request|session|application}”]/>

Syntax 2: Set the value of a scoped variable using body content

<c:set var=”varName” [scope=”{page|request|session|application}”]>

body content

</c:set>

参数说明

Value:要进行赋的值,也可以在标签内赋值

Var:保持的变量的名称

Scope:保持在那个域中

如:

<c:set value="123" var="temp"/>
<c:set var="temp1">
    456
</c:set>   
temp:${temp }
temp1:${temp1 }

 

 

 

 

<c:remove>

<c:remove var=”varName”

[scope=”{page|request|session|application}”]/>

参数说明:

Var:要移除的变量的Key

如:

<c:remove var="temp"/>

 

 

 

 

 

<c:catch>

<c:catch [var=”varName”]>

nested actions

</c:catch>

参数说明:

Var:异常信息

可能出异常的代码放置在标签内即可

如:

<c:catch var="exinfo">
    <%
       Integer.parseInt("asdfsdf");
    %>
</c:catch>
${exinfo }

 

 

 

 

Conditional Actions

<c:if>

Syntax 1: Without body content

<c:if test=”testCondition

var=”varName” [scope=”{page|request|session|application}”]/>

Syntax 2: With body content

<c:if test=”testCondition

[var=”varName”] [scope=”{page|request|session|application}”]>

body content

</c:if>

参数说明:

TestBoolean型的值,用于进行判断

VarTrue False,比较结果

Scope:比较结果的保存域

如:

<c:if test="${v1 lt v2}" var="v">
    v1小于v2
v=${v }
</c:if>
<c:if test="${empty v3}">
    v3为空
</c:if>

 

 

 

 

输出项只有条件成立的时候才会执行

 

<c:choose>

<c:when>

<c:otherwise>

三者结合使用<C:chose>必须有一个<C:when><C:when>必须在<C:otherwise>前面,<C:when>可以有多个,<C:otherwise>只能有一个。

<c:choose>

body content (<when> and <otherwise> subtags)

</c:choose>

<c:when test=”testCondition”>

body content

</c:when>

<c:otherwise>

conditional block

</c:otherwise>

参数说明:

TestBoolean型的值,用于进行判断

如:

<c:choose>
    <c:when test="${v1 lt v2}">
       v1小于v2<br>
    </c:when>
    <c:otherwise>
       v1大于v2<br>
    </c:otherwise>
</c:choose>

 

 

 

 

 

<c:forEach>

Syntax 1: Iterate over a collection of objects

<c:forEach[var=”varName”] items=”collection

[varStatus=”varStatusName”]

[begin=”begin”] [end=”end”] [step=”step”]>

body content

</c:forEach>

Syntax 2: Iterate a fixed number of times

<c:forEach [var=”varName”]

[varStatus=”varStatusName”]

begin=”begin” end=”end” [step=”step”]>

body content

</c:forEach>

参数说明

Items:要输出的集合

Var:保存每次循环的变量

varStatus:状态,如已经输出到多少行

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>

 

 

 

 

 

   发表时间:2010-08-12  
这些已经可以满足是开发中最常用的逻辑了~不错。
0 请登录后投票
   发表时间:2010-08-18  
再大的系统也是常用逻辑架起来的!呵呵呵!
0 请登录后投票
   发表时间:2010-09-02  
文字大大小小的,看着好累啊!“”
0 请登录后投票
   发表时间:2010-09-06  
不好意思,我是先写到Word里的,由于那么多的东西都要往JavaEye里搬,所以好多就没有调格式!
确实不好看哦,呵呵呵,我有时间调一下吧!
0 请登录后投票
   发表时间:2010-09-07  
前面的例子最好输出结果也贴出来
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics