Struts的Logic標籤可以根據特定的邏輯條件來判斷網頁的內容,或者迴圈遍曆集合元素,它和HTML,Bean標籤是Struts應用種最常用的三個標籤.它的功能主要是比較運算,進行字串的匹配,判斷指定的內容是否存在,迴圈遍曆集合,進行請求轉寄和重新導向,下面具體的說明標籤的用途: <logic:equal>變數等於指定的常量 <logic:notEqual>變數不等於指定的常量 <logic:lessThan>變數小於指定的常量 <logic:lessEqual>變數小於或者等於指定的常量 <logic:geraterThan>變數大於指定的常量 <logic:geraterEqual>變數大於或等於指定的常量 以上這些標籤都很類似 ,有共同的屬性,cookie屬性指定cookie屬性的值,然後用value設定的常量進行比較.header屬性設定header要求標頭的值,也是通過value屬性設定的值進行比較.parameter屬性設定一個請求參數,然後也是通過value屬性設定的值進行比較.name屬性設定一個變數,然後用value比較.如果同時設定了name和property屬性,此時name屬性指定已經存在的JavaBean,property屬性指定Bean的屬性.這個標籤感覺十分簡單,用法也十分單一,下面研究一下進行字串匹配的標籤和判斷特定內容的標籤. <logic:match>標籤是判斷變數種是否包含常量字串的,<logic:netMatch>也判斷是否不包含字串的,用name屬性定義一個字串變數,然後用value屬性的值去判斷變數是否包含這個字串或者是不包含這個字串,判斷成功返回true. <logic:empty>和<logic:netEmpty>標籤可以判斷指定的變數是否為空白的字串,可以通過name屬性來判斷一個字串是否為null.例如是請求參數的字串. <logic:present>和<logic:notPresent>標籤可以判斷指定的對象是否為空白,這個標籤的屬性很多.cookie屬性判斷Cookie是否存在.Header屬性判斷HTPP要求標頭是否存在.role屬性是判斷當前的許可權的使用者是否是指定的資訊安全角色.user屬性是判斷當前通過許可權驗證的使用者是否擁有指定的使用者名稱.parameter屬性是判斷請求的參數是否存在.name屬性是用來判斷指定的Bean是否存在.同時設定name和property屬性就是判斷Bean種的具體屬性是否存在了. <logic:messagesPresent>和<logic:messagesNotPresent>標籤是用來判斷是否在request範圍內的特定的ActionMessages或者是子類ActionErrors對象.name屬性是用來檢索ActionMessages對象的key,property指定從ActionMessages集合對象中檢索某條訊息key也就是具體的ActionMessage對象. 進行迴圈遍曆 Logic標籤是Logic標籤庫中最複雜的標籤,也是用途最廣泛的標籤,它能夠在一個迴圈中遍曆數組,Collection,Enumeration,Irerator或者Map中的所有元素 1) 遍曆集合的標籤 <logic:iterate>的name屬性指定需要進行遍曆的集合對象,它每次從集合中檢索出一個元素,然後存放在page範圍內,並以id屬性指定這個字串來命名這個元素,最好是在裡面嵌套一個<bean:write>標籤,把剛剛遍曆的第一個id指定的字串輸出,然後在迴圈在輸出.length屬性需要遍曆的元素的數目,如果沒有設定length屬性,就遍曆集合中的所有元素.offset屬性指定開始遍曆的其實位置,預設值是0.indexId屬性定義一個代表當前被遍曆元素的系列號,這個變數存放在page範圍內,可以被<bean:write>標籤訪問輸出輸出的是int的數字.例如1.2.3.4等.可以使用者輸出輸出的元素的系列號. 2) 遍曆 Map是通過<logic:iterate>標籤,name屬性指定一個HashMap,存放在request範圍,然後賦給id指定的變數,indexId可以指定索引編號,然後通過<bean:write>標籤輸出出來.在HashMap的每一個元素都是一個集合對象,所以可以嵌套使用.就是說<logic:inerate>這個標籤既可以把Collection的元素弄出來負給一個變數也可以弄Map的.所以說它的功能強大.還有一個collection屬性可以設定一個運算式,這個運算式返回的結果可以作為,<logic:inerate>標籤輸出的集合或者Map.這個屬性類似name屬性. 最後研究一下進行請求轉寄和重新導向的 Logic標籤,<logic:forward>標籤用於請求轉寄,它的name屬性指定轉寄的目標,與Struts設定檔中的<global-forwards>元素和的子項目<forward>元素匹配.簡單的說就是<logic:forward>標籤的name屬性定義的值,要去找到<golbal-forwards>子項目<forward>元素的匹配的name屬性,然後通過path指定的路徑進行轉寄.重新導向用<logic:redierct>標籤它的forward,href和page屬性指定重新導向的目標,這幾個屬性和<html:link>標籤的屬性用法十分相似. 關於標籤的部分我就說到這裡 ,因為我個人覺得在Struts中最常用的無非也就是這三個標籤庫了.我以前還寫過一個Struts的分頁應用,過些天繼續發布到我的Blog上. struts的LOGIC標籤的多層迭代 logic:Iterator 標籤(以下簡稱 “ 該標籤 ” )是 Struts 裡非常常用的一個標籤,其作用在於迴圈顯示給定容器物件中的值。
如此常用的標籤,其原始碼當然需要拿出來研究一下,以下列舉幾條研究成果:
1 、該標籤內部使用 Collection 來表示給定的容器,所有的給定容器物件(如 ArrayList , Map 等)都會被其轉化成為 Collection, Collection 實際就是 Map 和 List 的父類 。
2 、該標籤自己維護迴圈索引,不用程式員管理索引
3 、該標籤常見的幾個屬性如下: name 、 property 、 scope 、 id
對應 Struts 給出的 Api 說明如下:
name: 包括要遍曆 Collection 的 Jsp 頁面的 bean 的名字(如果 property 沒有被定義),或者是那些通過 getter 方法獲得屬性的 Jsp 中的 Bean 的名字,這些 getter 方法返回的是 Collection (如果 property 定義了)。
property: 在 name 命名的 Jsp bean 中定義的屬性的名字,通過 getter 方法返回一個 Collection
scope: 指示到哪裡去尋找 name 為名字的 bean ,如果沒有定義預設為 "any scope"
id: 如果 Collection 非空的話,在每次遍曆時候 Collection 中每個元素的名字。
其中除了 id 每個元素均為 Rt expr ,這兒的 rt expr 的意思就是 Run Time Expression 。明確的說就是,如果你對一個 Attribute 的 <rtexprvalue> 指定為 true ,你就可以在這樣的屬性中使用 <%=%> 之類的東東。這個設定檔在 tld 中。
只有 id 是必須要說明的。
關於 Api 說明的說明:
id 只是一個臨時標識,在下面的 <bean:write 裡面出現的 name 屬性要和 id 一致才能列印出 <bean:write 的 property ,而此 property 就是在 iterator 中的屬性。
舉例說明
以下代碼產生一個階梯狀表格
系統 資源 操作
soft3
res3
opt3
soft12
res12
opt1211
soft11
res11
opt1111
在此之前傳來一個 request.getAttribute("userPurview") ,所以有在第一個 logic 中的 userPurview, 就是在這個 request 裡面尋找 userPurview
返回的是一個
list
<table width="300" border="0">
<tr><td> 系統 </td>
<td> 資源 </td>
<td> 操作 </td>
</tr> <!--- 第一級迭代 --> //request 中的數值為” userPurview ”作用範圍在 request 中 , 取的 ID 名為 targetSys
<logic:iterate id=" targetSys" name="userPurview" scope="request"> // 這個 id 可以隨便起名,但是要注意下文使用的一致性
<tr bgcolor="#cccccc"><td height="21" class="unnamed2">
<bean:write name= "targetSys" property="cn"/> // 此處 name 和上面 id 保持一致, property 就是第一個 list 裡面的元素
</td>
<td height="21" class="unnamed2"> </td>
<td height="21" class="unnamed3"> </td>
</tr> <!--- 第二級迭代 -->
<logic:iterate id="targetRes" name="targetSys" property="purviewResList">
<tr><td height="21" class="unnamed2"> </td><td height="21" class="unnamed5">
<bean:write name= "targetRes" property="cn"/>
</td>
<td height="21" class="unnamed6"> </td>
</tr>
<!--- 第三級迭代 -->
<logic:iterate id="targetOpr" name="targetRes" property="purviewOprList">
<tr><td height="21" class="unnamed4"> </td><td height="21" class="unnamed4"> </td>
<td height="21" class="redzi">
<bean:write name= "targetOpr" property="cn"/></td>
</tr>
</logic:iterate>
</logic:iterate>
</logic:iterate>
</table>
結論:
多級迭代和單層差不多,唯一注意的就是 id 和 <bean:write 中的 name 的對應,上級 logic 的 id 與下級 logic 的 name 對應,並且取出來的要是個 Collection,name 和 id 不一定實際需要這個 bean ,都是虛擬。
Struts的logic標籤
(1)empty標籤
類名:org.apache.struts.taglib.logic.EmptyTag
標籤體:bodycontent=JSP
引用logic:empty
屬性 attribute:name,property,scope
功能:判斷對象的值是否為空白
(2)equal
類名:org.apache.struts.taglib.logic.EqualTag
標籤體:bodycontent=JSP
引用logic:equal
屬性 attribute:cookie,header,name,parameter,property,scope,value
功能:等於比較符
(3) forward
org.apache.struts.taglib.logic.ForwardTag
標籤體:bodycontent=empty
引用logic:forward
屬性 attribute:name
功能:頁面導向,尋找設定檔的全域forward
(4) greaterEqual
類名:org.apache.struts.taglib.logic.GreaterEqualTag
標籤體:bodycontent=JSP
引用logic:greaterEqual
屬性 attribute:cookie,header,name,parameter,property,scope,value
功能:大於等於比較符
(5)greaterThan
類名:org.apache.struts.taglib.logic.GreaterThanTag
標籤體:bodycontent=JSP
引用logic:greaterThan
屬性 attribute:cookie,header,name,parameter,property,scope,value
功能:大於比較符
(6) iterator
類名:org.apache.struts.taglib.logic.IterateTag
標籤體:bodycontent=JSP
引用logic:iterator
屬性 attribute:collection,id,indexId,length,name,offset,property,scope,type
功能:顯示列表為collection的值(List ,ArrayList,HashMap等)
(7)lessEqual
類名org.apache.struts.taglib.logic.LessEqualTag
標籤體:bodycontent=JSP
logic:lessEqual
屬性 attribute:cookie,header,name,parameter,property,scope,value
功能:小於等於比較符
(8)lessThan
類名:org.apache.struts.taglib.logic.LessThanTag
標籤體:bodycontent=JSP
logic:lessThan
屬性 attribute:cookie,header,name,parameter,property,scope,value
功能:小於比較符
(9)match
類名:org.apache.struts.taglib.logic.MatchTag
標籤體:bodycontent=JSP
引用logic:match
屬性 attribute:cookie,header,location,name,parameter,property,scope,value
功能:比較對象
(10)messagesNotPresent
類名:org.apache.struts.taglib.logic.MessagesNotPresentTag
標籤:bodycontent=JSP
引用logic:messagesNotPresent
屬性 attribute:name,property,message
功能:ActionMessages/ActionErrors對象是否不存在
(11)messagePresent
類名:org.apache.struts.taglib.logic.MessagesPresentTag
標籤:bodycontent=JSP
引用logic:messagePresent
屬性 attribute:name,property,message
功能:ActionMessages/ActionErrors對象是否不存在
(12)notEmpty
類名:org.apache.struts.taglib.logic.NotEmptyTag
標籤:bodycontent=JSP
引用logic:notEmpty
屬性 attribute:name,property,scope
功能:比較對象是否不為空白
(13)notEqual
類名:org.apache.struts.taglib.logic.NotEqualTag
標籤:bodycontent=JSP
引用logic:notEqual
屬性 attribute:cookie,header,name,parameter,property,scope,value
(14)notMatch
類名:org.apache.struts.taglib.logic.NotMatchTag
標籤:bodycontent=JSP
引用logic:notMatch
屬性 attribute:cookie,header,location,name,parameter,property,scope,value
功能:比較對象是否不相等
(15)notPresent
類名:org.apache.struts.taglib.logic.NotPresentTag
標籤:bodycontent=JSP
引用logic:notPresent
屬性 attribute:cookie,header,name,parameter,property,role,scope,user
功能:request對象傳遞參數是否不存在
(16)present
類名:org.apache.struts.taglib.logic.PresentTag
標籤:bodycontent=JSP
引用logic:present
屬性 attribute:cookie,header,name,parameter,property,role,scope,user
功能:request對象傳遞參數是否存在
(17)redirect
類名:org.apache.struts.taglib.logic.RedirectTag
標籤:bodycontent=JSP
引用logic:redirect
屬性 attribute:anchor,forward,href,name,page,paramId,paramName,paramProperty,paramScope,property,scope,transaction
功能;頁面轉向,可傳遞參數
例子:
1、<logic:iterate id="row" name="formBean" property="list" indexId="index">
<logic:equal name="row" property="unit" value="defaultValue">
...
</logic:equal>
</logic:iterate>
<logic:notEmpty name="info">
<logic:iterate id="list" name="info">
<bean:define id="curId" name="list" property="id" />
<logic:equal parameter="id" value="<%= curId.toString() %>">
abcde
</logic:equal>
</logic:iterate>
</logic:notEmpty>
parameter要進行比較的HTTP請求參數的名稱
name如果要進行比較的是bean或bean的屬性,則這個屬性代表bean的名稱
property要進行比較的bean屬性的名稱
logic使用
使用struts中的<logic:iterate>標記,可以完成一些複雜的迴圈。
下面用了一個小例子說明這一點:
IterateBean.java:
package beantest;
import org.apache.struts.action.ActionForm;
public class IterateBean extends ActionForm {
private String[] name_coll = null;
public void setName_coll(String[] name_coll) {
this.name_coll = name_coll;
}
public String[] getName_coll() {
return this.name_coll;
}
private String[] name_new = null;
public void setName_new(String[] name_new) {
System.out.println("IterateBean:setName_new(String[])");
this.name_new = name_new;
}
public String[] getName_new() {
return this.name_new;
}
}
IterateAction.java
package beantest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class IterateAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
IterateBean ibean = (IterateBean) form;
String[] temp = {"bitan", "helen"};
ibean.setName_coll(temp);
temp = new String[] {"aaaaa", "bbbbb"};
ibean.setName_new(temp);
return mapping.findForward("iterate-view");
}
}
iterate-view.jsp:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<logic:iterate id="iterate" name="ibean" property="name_coll" scope="request" offset="0" >
<logic:iterate id="iterate_new" name="ibean" property="name_new" scope="request" offset="0" >
name: <font color=red><%=iterate%></font> ; code: <font color=red><%=iterate_new%></font><br>
</logic:iterate>
★〓〓〓〓〓〓〓〓★<br>
<bean:include id="lower" page="/test/lower.jsp" />
<bean:write name="lower" filter="false"/><br>
</logic:iterate>
lower.jsp:
<font color=blue>hello, iterate!</font>
struts-config.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean type="beantest.IterateBean" name="ibean" />
</form-beans>
<action-mappings>
<action path="/iterate-test" type="beantest.IterateAction" name="ibean" scope="request" >
<forward path="/test/iterate-view.jsp" name="iterate-view" />
</action>
</action-mappings>
</struts-config>
web.xml:
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/eye/*</url-pattern>
</servlet-mapping>
</web-app>
輸出效果:
name: bitan ; code: aaaaa
name: bitan ; code: bbbbb
★〓〓〓〓〓〓〓〓★
hello, iterate!
name: helen ; code: aaaaa
name: helen ; code: bbbbb
★〓〓〓〓〓〓〓〓★
hello, iterate!
如上所示,一個迴圈中不能直接將多個變數同時輸出;但在迴圈中嵌套迴圈可以實現一種複雜效果。
要實現在一個迴圈中將多個變數同時輸出,可以引用<bean:include>、<bean:write>標記。