JSTL与Struts2标签的使用对比==>

JSTL与Struts2标签的使用对比

标签: JSTLStruts2对比
  1178人阅读  评论(0)  收藏  举报
  分类:
 
 
1. 条件标签
    JSTL:  
 1 <c:if test="${user.password == 'hello'}">
 2      <c:choose>
 3          <c:when test="${user.age <= 18}">
 4             <font color="blue" />
 5          </c:when>
 6          <c:when test="${user.age <= 30 && user.age > 18}">
 7              <font color="red" />
 8          </c:when>
 9          <c:otherwise>
10             <font color="green" />
11          </c:otherwise>
12      </c:choose>
13 </c:if>
    Struts2:
1 <s:if test="#user.age <= 18">
    <font color="blue" />
3 </s:if>
4 <s:elseif test="#user.age <= 30 && user.age > 18">
    <font color="red" />
6 </s:elseif>
    <font color="green" />
8 </s:else>

2. 迭代标签
    JSTL:
1 <c:forEach var="user" items="${users}">
    <c:out value="${user.userName}" />
3 </c:forEach>
4 <!-- 迭代固定次数 -->
5 <c:forEach var="i" begin="1" end="10" step="3">
    <c:out value="${i}" />
7 </c:forEach>
8 <!-- 这种循环相当于for(int i=1; i<10; i++), 其中step是指迭代的步长,默认为1.-->
    Struts2:
1 <s:iterator value="#users" status="stuts">                 
2     <s:if test="#stuts.odd == true">  <!-- 判断是否为奇数行 -->
        <s:property  value="userName" />
    </s:if>
    <s:else>
        <s:property  value="passWord" />
    </s:else>
8 </s:iterator>

3. URL相关标签
    JSTL:
 1 <!-- 绝对路径 -->
 2 <c:import url="http://127.0.0.1:8080/hello/hello.jsp" />
 3 <!-- 相对路径 -->
 4 <c:import url="hello.jsp" />
 5 <!-- Encode -->
 6 <a href="<c:url value='hello.jsp'><c:param name='userName' value='cyanbomb' /></c:url>"></a>
 7 <!-- 传递参数到指定的URL -->
 8 <c:import url="hello.jsp" charEncoding="gb2312" >
 9      <c:param name="userName" value="cyanbomb" />
10 </c:import>
11 <!-- URL重定向 -->
12  <c:redirect url="${myurl}" />
13 <!-- 构造URL -->
14 <c:url value="myurl" var="hello.jsp" scope="session">
15      <c:param name="userName" value="cyanbomb" />
16 </c:url>
    Struts2:
1 <a href='<s:url value="/hello.jsp" />'> Hello</a><br />
2 <s:url id="url" value="/hello.jsp">
    <s:param name="name"> cyanbomb</s:param>
4 </s:url>         
5 <s:a href="%{url}"> Hello</s:a>

解除的疑问,list遍历问题

像这样一个list,里面有3条记录,每条记录包含两个对象,我把结果集(lstRooms)request到了页面,想遍历显示RrmRooms里的id,和RrmRoomType里的name.
我用JSTL实现如下:
1 <table>
    <c:forEach var="rm" items="${lstRooms}">
        <tr>
            <td> ${rm[0].id}</td>
            <td> ${rm[1].name}</td>
        <tr>
    </c:forEach>
8 </table>
STRUTS2实现如下:
1 < table >
2       < s:iterator  value ="#lstRooms"  status ="stat" >
3           < tr >
4               < td >< s:property  value ="#lstRooms[#stat.index][0].id"   /></ td >
5               < td >< s:property  value ="#lstRooms[#stat.index][1].name"   /></ td >
6           < tr >
7       </ s:iterator >
8 </ table >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值