自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談Struts標(biāo)簽logic:iterate 

開發(fā) 后端
<logic:iterate> 是Logic 標(biāo)簽庫中最復(fù)雜的標(biāo)簽,也是用途最廣的一個標(biāo)簽,本文將為大家簡單談一談這一Struts標(biāo)簽的用法。

< logic:iterate >是Logic Struts標(biāo)簽庫中最復(fù)雜的標(biāo)簽,也是用途最廣的一個標(biāo)簽,它能夠在一個循環(huán)中遍歷數(shù)組、Collection、Enumeration、Iterator 或 Map 中的所有元素。

1. 遍歷集合

的 name 屬性指定需要進(jìn)行遍歷的集合對象, 它每次從集合中檢索出一個元素, 然后把它放在page 范圍內(nèi), 并以id 屬性指定的字符串來命名這個元素, 例如:

  < % Vector animals = new Vector(); 
animals.addElement("Dog"); 
animals.addElement("Cat"); 
animals.addElement("Bird");
animals.addElement("Chick"); 
request.setAttribute("Animals", animals);
%> 


以上代碼先定義了一個Vector 類型的集合變量 Animals, 它存放在request 范圍內(nèi). 接下來 標(biāo)簽在一個循環(huán)中遍歷Animals 集合(這個集合名就是在標(biāo)簽中的name 屬性的值)中所有元素, 每次檢索到一個元素, 就把它命名為"element"(標(biāo)簽id 屬性的值), 并存放在page 范圍內(nèi).

中, 還嵌套了一個< ?xml:namespace prefix = bean / >< bean:write> 標(biāo)簽, 它用于輸出每個元素的內(nèi)容. 以上代碼的輸出內(nèi)容如下:

Dog

Cat

Bird

Chick

length 屬性指定需要遍歷的元素的數(shù)目, 如果沒有設(shè)置length 屬性, 就遍歷集合中的所有元素.

offset 屬性指定開始遍歷的起始位置, 默認(rèn)值為 "0" , 表示從集合的第一個元素開始遍歷.

indexId 屬性定義一個代表當(dāng)前遍歷元素序號的變量, 這個變量被存放在 page 范圍內(nèi), 可以被標(biāo)簽主體的

標(biāo)簽訪問. 例如:

id="element" // 指定輸出元素的名 與  中name 屬性一致 
indexId="index" // 遍歷元素序號的變量, 這個變量放在page 范圍內(nèi)
name="Animals" // request 中的集合名, 從中取循環(huán)取出元素
offset="1" // 從集合的第 2 條記錄開始取數(shù)
length="2"> // 取出 2 個元素
< / bean:write>. // 輸出每個元素的序號, 與indexId 的屬性一致
< bean:write name="element" >< / bean:write >
// 輸出每個元素的內(nèi)容, 與id 的屬性一致
< / logic:iterate>

2. 遍歷Map

 標(biāo)簽還可以遍歷HashMap 中的元素, 例如: 

<%
HashMap months = new HashMap();

months.put("Jan","January");
months.put("Feb","February");
months.put("Mar","March");

request.setAttribute("month", months);
%>
< logic:iterate id=element name="months" indexId="ind" >
< bean:write name="ind">. // 序號
< bean:write name="element" property="key">: // 鍵名
< bean:write name="element" property="value"> // 鍵值


以上代碼先定義一個名為"months" 的HashMap, 存放在request 范圍內(nèi). 接下來在 標(biāo)簽遍歷months 對象的每一個元素, 每一個元素包含一對 key/value . 在 標(biāo)簽主體中包含三個 標(biāo)簽, 分別輸出每個元素的序號、key 和 value. 以上代碼的輸出內(nèi)容如下:

0.Mar: March

1.Feb: February

2.Jan: January

如果HashMap 中的每個元素的 value 是集合對象, 則可以采用嵌套的標(biāo)簽遍歷集合中的所有對象, 例如:

<% 
HashMap h = new HashMap();
String vegetables[] = {"pepper","cucumber"};
String fruits[] = {"apple","orange","banana","cherry","watermelon"};
String flowers[] = {"chrysanthemum","rose"};
String trees[] = {"willow"};

h.put("Vegetables", vegetables);
h.put("Fruits",fruits);
h.put("Flowers",flowers);
h.put("Trees",trees);

request.setAttribute("catalog",h);
%>
中的name 屬性對應(yīng), 輸出內(nèi)容
indexId="ind" // 與 中的name 屬性對應(yīng), 輸出序號
name="catelog"> // 指定輸出元素的名稱
. // 輸出序號
中id 屬性對應(yīng)
property="key"/> // 集合中的鍵名
id="elementValue" // 與 中的name 屬性對應(yīng)
name="element" // 指定輸出元素的名稱
property="value" // 集合中的鍵值
length="3" // 取3 個元素
offset="1"> // 從第 2 個位置取
-------


以上代碼先定義一個名為"catelog" 的HashMap , 存放在request 范圍內(nèi), 它的每個元素的value 為字符串?dāng)?shù)組.
接下來外層的標(biāo)簽遍歷HashMap 中的所有元素, 內(nèi)層的標(biāo)簽訪問每個元素的value 屬性, 遍歷value 屬性引用的字符串?dāng)?shù)組中的所有元素.

#p#

3. 設(shè)置被遍歷的變量

可以通過以下方式來設(shè)置需要遍歷的變量

(1) 設(shè)置name 屬性, name 屬性指定需要遍歷的集合或Map, 例如:

< logic:iterate id=element name="Animals" > 
< bean:write name="element">< /bean:write>
< / logic:iterate >

(2) 設(shè)置name 屬性和property 屬性, name 屬性指定一個JavaBean, property 屬性指定JavaBean 的一個屬性, 這個屬性為需要遍歷的集合或Map, 例如:

< logic:iterate id=element name="catelog" indexId="ind"> 
< bean:write name="ind">< /bean:write>
< bean:write name="element" property="key">< /bean:write>
< logic:iterate id=elementValue name="element" property="value" offset="1" length="3" >
--------< /bean:write>
< /logic:iterate>

(3) 設(shè)置collection 屬性, collection 屬性指定一個運(yùn)行時表達(dá)式, 表達(dá)式的運(yùn)算結(jié)果為需要遍歷的集合或Map, 例如:

< logic:iterate id=header collection?< % ="request.getHeaderNames()%">"> 
< /bean:write>
< /logic:iterate>

4. 讀取JavaBean 中的數(shù)據(jù)

(1) 在Jsp 頁面中加入JavaBean 如:

< ? xml:namespace prefix = jsp />< jsp:useBean id=articleClasses class=com.GetArticleClasses>
< / jsp:useBean> 

上面這個JavaBean 要求必須存在一個集合數(shù)組對象,如Vector,Collection,ArrayList 等;在這個JavaBean 的構(gòu)造函數(shù)中,取得數(shù)據(jù)庫中的數(shù)據(jù),并將其存入數(shù)組對象中。

(2) 使用 標(biāo)簽,取出JavaBean 中存放的數(shù)組對象中的數(shù)據(jù)




id="aClasses" // id : 給檢索出的元素所命的名. 
name="articleClasses" // name : JavaBean 在頁面中所設(shè)置的引用ID.
property="coll"> // coll : JavaBean 中的集合數(shù)組屬性名稱.

< ?xml:namespace prefix = html />< html:link
paramId="classId"
paramName="aClasses"
paramProperty="classId">
< bean:write name="aClasses" 與< logic:iterate>標(biāo)簽中的id 屬性相對應(yīng)
property="className" /> // 取出JavaBean中, 存放在集合對象中的,對象的className 屬性值


(3) 在JavaBean 中的集合對象中存放實(shí)體對象的語句如下:

......

public class GetArticleClasses 
{
// 數(shù)據(jù)集合
private Collection coll;

// 返回數(shù)據(jù)集合
public Collection getColl()
{
return coll;
}
// 構(gòu)造函數(shù), 取出數(shù)據(jù),存入集合中
public GetArticleClasses()
{
coll = new ArrayList();
try{
// 數(shù)據(jù)庫連接
Connection connection = DBConnection.getConnection();
if(connection != null)
{
Statement statement = connection.createStatement();
ResultSet resultset;
ArticleClass articleclass;
resultset = statement.executeQuery("SELECT * FROM table ORDER BY id");
while( resultset.next())
{
articleclass = new ArticleClass();
articleclass.setId(resultset.getInt("id"));
articleclass.setClassId(resultset.getString("class"));
articleclass.setClassName(resultset.getString("name"));

coll.add(articleclass))
}
resultset.close();
connection.close();
} else {
coll = null;
}
} catch(Exception exception) {
coll = null;
}
}
}


您正在閱讀的是“淺談Struts標(biāo)簽 logic:iterate

【編輯推薦】

  1. 在Eclipse中開發(fā)struts應(yīng)用程序
  2. 手把手教你在Eclipse中配置開發(fā)Struts
  3. Eclipse下開發(fā)struts完整解決亂碼問題
  4. 淺談如何學(xué)習(xí)新版Struts 不變應(yīng)萬變
  5. 實(shí)例說明如何集成Spring和Struts
責(zé)任編輯:張燕妮 來源: 網(wǎng)易博客
相關(guān)推薦

2009-06-04 08:21:13

struts標(biāo)簽logic:itera

2009-06-05 10:46:12

struts logilogic標(biāo)簽

2009-07-15 11:43:13

<iterate>標(biāo)簽

2009-06-05 10:12:36

Struts標(biāo)簽庫HTML標(biāo)簽

2009-06-08 16:57:00

Struts HTML

2009-06-04 07:55:08

Struts框架簡介Struts

2009-06-05 11:01:23

struts mvcMVC工作原理

2009-06-04 09:20:19

struts2 if標(biāo)使用

2009-07-02 11:25:33

JSP課程

2009-06-04 10:41:52

Struts工作原理

2009-06-05 09:52:25

struts分頁Hibernate

2009-06-04 09:11:34

學(xué)習(xí)strutsstruts框架

2009-03-06 14:34:31

StrutsHibernateSpring

2009-06-08 16:44:00

Struts配置文件

2009-06-05 09:58:20

struts2驗(yàn)證用戶注冊

2009-06-04 09:41:50

struts2上傳文件

2009-06-05 09:24:53

struts標(biāo)簽jsp

2009-06-08 16:44:00

Struts2文件上傳

2009-07-06 16:20:50

JSP自定義標(biāo)簽

2009-06-04 08:01:25

Struts2攔截器原理
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號