ITPub博客

首页 > 应用开发 > Java > struts2 标签的使用之一 s:if,siterator使用

struts2 标签的使用之一 s:if,siterator使用

原创 Java 作者:dawn009 时间:2014-09-03 09:26:00 1 删除 编辑

struts2 的web 项目中为了方便的编写jsp,标签是最好的选择

1:struts2 标签库的定义在**-core-版本号.jar META-INF 路径下找到struts-tags.tld文件;使用该标签需要在web 项目里面导入标签库:
    A:在web.xml文件  (默认 可以省略)

  1. <taglib>
  2.     <taglib-uri>/struts-tagstaglib-uri>
  3.     <taglib-location>/WEB-INF/lib/*.jartaglib-location>
  4. <taglib>
    B:在jsp 导入标签的dingyi
  1. <%@ taglib prefix="s" uri="/struts-tags"%>
注意uri要一直,上面定义的是默认写法
 
2:OGNL struts2 利用了内建的ognl表达式,它基于XWork,增加了对ValueStack的支持
,在jsp里面通过ognl访问属性,struts2会自动搜寻栈内的所有实体。直到找到位置。
如:#person.address.ip 等于 person.getAddress().getIp();翻译结果为条用get方法
或是jstl的${person.address.ip}

用法
A:直接写表达式
  1. <s:set name='china' value='china'>
  2. <s:if test="${china=='china'}">shows:if>
  3. result:  show
  4. <s:set name="count" value="99">
  5. <s:if test="${count>0}">bigger than 0s:if>
  6. <s:else>nots:else>
  7. result:  bigger than 0
B:在遍历里面使用判断:
  1. "id" value="label">
  2.     if test="%{#id.attrValueId!=0}">
  3.         "#id.attrValue" />
  4.                 "#id.countAll" />                 "#id.countRequest" /> 
  5.     if>
  6.     else>
  7.         "#id.attrValue" />
  8.     else>
label是一个List  Attribu 包含属性attrValueId和countAll
在s:iterator域内这是id的值是"id",使用ognl读取遍历对象的方法是 #id
test="%{#id.attrValueId!=0}" 看子对象的属性attrValueId是否为0
"#id.attrValue" /> 打印子对象的attrValue属性

C:直接读取对象
  1. if test="request.price==null||request.price<=0">
  2. if>
读取对象request,判断price是否小于0;
request 可以是如何的javaBean,也可以是基本属性

D:直接读取对象的另一种写法
  1.  if test="%{aTransactionSummaryBean!=null}">
E:多个条件的判断
  1. <s:if test='%{isShowAll=="Y"||isShowAll==null||isShowAll==""}'>
  2.     <li class="selected">
  3. s:if>
  4. <s:else>
  5.     <li>else
  6. s:else>
isShowAll 为Action 里面的字符串属性

F:直接拿Action里面的boolean 貌似不xing
Action里面
  1. private boolean choosed = true;
  2. public boolean isChoosed(){
  3.     return choosed;
  4. }
  5. if test="choosed">if>
发现这个判断无法正确运行,也许是ognl 是通过get方法来获取对象的,如果在action 里面有下面的方法;
  1. public String getChoosed(){
  2.     return "true";
  3. }
上面那个s:if可以正确执行 

最后注意一点:ognl和jstl标签不能互相嵌套

------------------------------------------------------------------------------------------>>

struts2中iterator标签的嵌套使用(if/else),并根据内容调整字体颜色

  1. <s:iterator value="MonitorSnInfos" id="MonitorSnInfos" status="s" var="MonitorSnInfos">  
  2. <tr>  
  3. <td><input type="checkbox" name="SN_choice" value="<s:property value="snName"/>" />td>  
  4. <td><s:property value="snName"/>td>  
  5. <td><s:property value="nodeId"/>td>  
  6. <td><s:property value="preNodeId"/>td>  
  7. <td><s:property value="oanFunction"/>td>  
  8. <td><s:property value="snuFunction"/>td>  
  9.   
  10. <s:if test="#MonitorSnInfos.cpuUtilizationRate > #MonitorSnInfos.cpuUtilizationThreshold">  
  11. <td style="color:#FF0000"><s:property value="cpuUtilizationRate"/>td>  
  12. s:if>  
  13. <s:else>  
  14. <td style="color:#0C0"><s:property value="cpuUtilizationRate"/>td>  
  15. s:else>  
  16.   
  17. <s:if test="#MonitorSnInfos.memoryUtilizationRate > #MonitorSnInfos.memoryUtilizationThreshold">  
  18. <td style="color:#FF0000"><s:property value="memoryUtilizationRate"/>td>  
  19. s:if>  
  20. <s:else>  
  21. <td style="color:#0C0"><s:property value="memoryUtilizationRate"/>td>  
  22. s:else>  
  23.   
  24. tr>  
  25. s:iterator> 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29119536/viewspace-1262544/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-08-15

  • 博文量
    595
  • 访问量
    4989195