struts2 的web 项目中为了方便的编写jsp,标签是最好的选择
1:struts2 标签库的定义在**-core-版本号.jar META-INF 路径下找到struts-tags.tld文件;使用该标签需要在web 项目里面导入标签库: A:在web.xml文件 (默认 可以省略)
- <taglib>
- <taglib-uri>/struts-tagstaglib-uri>
- <taglib-location>/WEB-INF/lib/*.jartaglib-location>
- <taglib>
B:在jsp 导入标签的dingyi
- <%@ 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:直接写表达式
- <s:set name='china' value='china'>
- <s:if test="${china=='china'}">shows:if>
- result: show
- <s:set name="count" value="99">
- <s:if test="${count>0}">bigger than 0s:if>
- <s:else>nots:else>
- result: bigger than 0
B:在遍历里面使用判断:
- "id" value="label">
- if test="%{#id.attrValueId!=0}">
- "#id.attrValue" />
- "#id.countAll" /> "#id.countRequest" />
- if>
- else>
- "#id.attrValue" />
- else>
-
label是一个List Attribu 包含属性
attrValueId和countAll 在
s:iterator域内这是id的值是"id",使用ognl读取遍历对象的方法是 #id test="%{#id.attrValueId!=0}" 看子对象的属性 attrValueId是否为0 "#id.attrValue" /> 打印子对象的attrValue属性C:直接读取对象 - if test="request.price==null||request.price<=0">
- if>
读取对象request,判断price是否小于0;
request 可以是如何的javaBean,也可以是基本属性
D:直接读取对象的另一种写法
- if test="%{aTransactionSummaryBean!=null}">
E:多个条件的判断
- <s:if test='%{isShowAll=="Y"||isShowAll==null||isShowAll==""}'>
- <li class="selected">
- s:if>
- <s:else>
- <li>else
- s:else>
isShowAll 为Action 里面的字符串属性 F:直接拿Action里面的boolean 貌似不xing Action里面 - private boolean choosed = true;
- public boolean isChoosed(){
- return choosed;
- }
- if test="choosed">if>
发现这个判断无法正确运行,也许是ognl 是通过get方法来获取对象的,如果在action 里面有下面的方法;
- public String getChoosed(){
- return "true";
- }
上面那个s:if可以正确执行
最后注意一点:ognl和jstl标签不能互相嵌套 ------------------------------------------------------------------------------------------>>
- <s:iterator value="MonitorSnInfos" id="MonitorSnInfos" status="s" var="MonitorSnInfos">
- <tr>
- <td><input type="checkbox" name="SN_choice" value="<s:property value="snName"/>" />td>
- <td><s:property value="snName"/>td>
- <td><s:property value="nodeId"/>td>
- <td><s:property value="preNodeId"/>td>
- <td><s:property value="oanFunction"/>td>
- <td><s:property value="snuFunction"/>td>
-
- <s:if test="#MonitorSnInfos.cpuUtilizationRate > #MonitorSnInfos.cpuUtilizationThreshold">
- <td style="color:#FF0000"><s:property value="cpuUtilizationRate"/>td>
- s:if>
- <s:else>
- <td style="color:#0C0"><s:property value="cpuUtilizationRate"/>td>
- s:else>
-
- <s:if test="#MonitorSnInfos.memoryUtilizationRate > #MonitorSnInfos.memoryUtilizationThreshold">
- <td style="color:#FF0000"><s:property value="memoryUtilizationRate"/>td>
- s:if>
- <s:else>
- <td style="color:#0C0"><s:property value="memoryUtilizationRate"/>td>
- s:else>
-
- tr>
- s:iterator>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29119536/viewspace-1262544/,如需转载,请注明出处,否则将追究法律责任。