C标签和s标签的对比,特别是里面的if判断条件的写法

来源:互联网 发布:香港域名注册商 编辑:程序博客网 时间:2024/04/30 03:02

struts2标签有if…..else

<s:if></if><s:else></s:else>
  • 1
  • 2
  • 3

如果要用c标签来表示if…..else 的效果

<c:choose>     <c:when test=....></when>   相当于if    <c:otherwise></c:otherwise>  相当于else</c:choose>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

遍历集合 假设遍历personList 看他们的不同写法

<s:iterator  value="personList " var="var">      //这里直接写personList    <s:property value="#var.name" />            //获取这个人的名字 注意这里用的是#号</s:iterator>
  • 1
  • 2
  • 3
  • 4
  • 5

换成c标签

<c:forEach items=${personList }  var="var"> //这里写${personList}     <c:out value="${var.name}" />               //这里用的是$符号</c:forEach>
  • 1
  • 2
  • 3
  • 4
  • 5

似乎c标签都要用${} 来获取值, s标签似乎要简单些。

在看一下<s:set>   <c:set> 之间的用法区别
  • 1
<c:set var="str"  value="${var.proInfo.id }0000"></c:set>      <c:set var="p1" value="${str.substring(0,2) }"></c:set> <c:set var="p2" value="${str.substring(0,4) }"></c:set> <c:set  var="picpath"  value="artwork/${p1 }/${p2 }/${var.proInfo.id }"></c:set>如果c标签要获取上面str的值 直接使用<c:out value="${str}" /> 就能获取到该值。上面p1就使用了str的值看一下s标签的用法<s:set name="str" value="#var.proInfo.id+'0000'"></s:set>      //注意c标签用var  而s标签用的是name  这是个重要的区别点<s:set name="p1" value="#str.substring(0,2)"></s:set><s:set name="p2" value="#str.substring(0,4)"></s:set><s:set name="picpath" value="'artwork/'+#p1+'/'+#p2+'/'+#var.proInfo.id"></s:set>如果s标签要获取上面str的值 直接使用<s:property value="#str" /> 就能获取到该值。 上面p1就使用了str的值还是来看c:if    和 s:if   中test的写法<c:forEach var="art" items="${productInfoList}">var att = "关注", flag = 0;<c:forEach var="atten" items="${productAttentionList}"><c:if test="${atten.productInfo.id==art.id}">att = "已关注"; flag = 1;</c:if>  //遍历嵌套, 看test中的写法</c:forEach></c:forEach>                    回到s:if标签中的test写法<s:iterator var="art" value="productInfoList">var att = "关注", flag = 0;<s:iterator var="atten" value="productAttentionList"><s:if test="#atten.productInfo.id==#art.id">att = "已关注"; flag = 1;</s:if>  //遍历嵌套, 看test中的写法</s:iterator></s:iterator>  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
0 0
原创粉丝点击