struts2 的web 項目中為了方便的編寫(xiě)jsp,標簽是最好的選擇
1:struts2 標簽庫的定義在**-core-版本號.jar META-INF 路徑下找到struts-tags.tld文件;使用該標簽需要在web 項目里面導入標簽庫:
A:在web.xml文件 (默認 可以省略)
<taglib>
<taglib-uri>/struts-tags</taglib-uri>
<taglib-location>/WEB-INF/lib/*.jar</taglib-location>
<taglib>
B:在jsp 導入標簽的dingyi
<%@ taglib prefix="s" uri="/struts-tags"%>
注意uri要一直,上面定義的是默認寫(xiě)法
2:OGNL struts2 利用了內建的ognl表達式,它基于XWork,增加了對ValueStack的支持
,在jsp里面通過(guò)ognl訪(fǎng)問(wèn)屬性,struts2會(huì )自動(dòng)搜尋棧內的所有實(shí)體。直到找到位置。
如:#person.address.ip 等于 person.getAddress().getIp();翻譯結果為條用get方法
或是jstl的${person.address.ip}
<s:if>用法
A:直接寫(xiě)表達式
<s:set name='china' value='china'>
<s:if test="${china=='china'}">show</s:if>
result: show
<s:set name="count" value="99">
<s:if test="${count>0}">bigger than 0</s:if>
<s:else>not</s:else>
result: bigger than 0
B:在遍歷里面使用判斷:
<s:iterator id="id" value="label">
<s:if test="%{#id.attrValueId!=0}">
<s:property value="#id.attrValue" />
<s:property value="#id.countAll" /> <s:property value="#id.countRequest" />
</s:if>
<s:else>
<s:property value="#id.attrValue" />
</s:else>
</s:iterator>
label是一個(gè)List<Attribu> Attribu 包含屬性attrValueId和countAll
在s:iterator域內這是id的值是"id",使用ognl讀取遍歷對象的方法是 #id
test="%{#id.attrValueId!=0}" 看子對象的屬性attrValueId是否為0
<s:property value="#id.attrValue" /> 打印子對象的attrValue屬性
C:直接讀取對象
<s:if test="request.price==null||request.price<=0">
</s:if>
讀取對象request,判斷price是否小于0;
request 可以是如何的javaBean,也可以是基本屬性
D:直接讀取對象的另一種寫(xiě)法
<s:if test="%{aTransactionSummaryBean!=null}">
E:多個(gè)條件的判斷
<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;
}
<s:if test="choosed"></s:if>
發(fā)現這個(gè)判斷無(wú)法正確運行,也許是ognl 是通過(guò)get方法來(lái)獲取對象的,如果在action 里面有下面的方法;
public String getChoosed(){
return "true";
}
上面那個(gè)s:if可以正確執行
最后注意一點(diǎn):ognl和jstl標簽不能互相嵌套
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。