Struts Logic标签库(三)

来源:互联网 发布:开淘宝店取什么名字好 编辑:程序博客网 时间:2024/05/16 12:13

进行循环遍历的Logic标签
<logic:iterate>是Logic标签库中最复杂的标签,它能够在一个循环中遍历数组,Collection,

Enumeration,Iterator或Map中的所有元素。
 1、遍历集合
    <logic:iterate>的name属性指定需要进行遍历的集合对象,它每次从集合中检索出一个元素,然后

把它存放在page范围内,并以id属性指定的字符串来命名这个元素,例如:
 <%
 Vector animals=new Vector();
 animals.addElement("Dog");
 animals.addElement("Cat");
 animals.addElement("Bird");
 animals.addElement("Chick");
 request.setAttribute("Animals", animals);
 %>
<logic:iterate id="element" name="Animals">
   <bean:write name="element"/><BR>
</logic:iterate><p>
以上代码的输出内容为:
      Dog
      Cat
      Bird
      Chick
length属性指定需要遍历的元素的数目,如果没有设置length属性,就遍历集合中的所有元素,offset属

性指定开始遍历的起始位置,默认值“0”,表示从一个集合的第一个元素开始遍历,indexId属性定义一

个代表当前被遍历元素序号的变量,这个变量被存放在page范围内,可以被标签主体的<bean:write>标签

访问,例如:
  <logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
   <bean:write name="index"/>.<bean:write name="element"/><BR>
  </logic:iterate><p>
  以上标签的length属性为2,表示只需要遍历Animals集合中的两个元素,offset为1,表示从第二个元

素开始遍历,输出内容为:
       1、Cat
       2、Bird
2、遍历Map
   <logic:iterate>标签还可以遍历HashMap中的元素,例如:
   <%
HashMap months = new HashMap();
months.put("Jan.", "January");
months.put("Feb.", "February");
months.put("Mar.", "March");
request.setAttribute("months", months);
%>
<%--
The following section shows iterate.
--%>
<logic:iterate id="element" indexId="ind" name="months">
  <bean:write name="ind"/>.
  <bean:write name="element" property="key"/>:
  <bean:write name="element" property="value"/><BR>
</logic:iterate><P>
  标签遍历months对象的每一个元素,每一个元素都包含一对key/value,以上代码输出:
   0、Mar:March
   1、Feb:Feberuary
   2、Jan:January
如果HashMap中的每个元素的value是集合对象,则可以采用嵌套的<logic:iterate>标签遍历集合中的所

有对象,例如:
 <%
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);
%>
<%--
The following section shows iterate.
--%>

<logic:iterate id="element" indexId="ind" name="catalog">
  <bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
  <logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
      -----<bean:write name="elementValue"/><BR>
  </logic:iterate>
</logic:iterate><P>
以上代码下定义了一个名为'catalog'的HashMap,存放在request范围,它的每个元素的value为字符串数

组,接下来外层的标签遍历HashMap中的所有元素,内层的标签访问每个元素的value属性,遍历value属

性引用的字符串数组中的所有元素,输出内容为:
    0. Vegetables
    ----cucumber
    1、Flowers
    ----rose
    2.Fruits
    ----orange
    ----banana
    ----cherry
    3.Trees
3、设置被遍历的变量
可以通过以下方式来设置需要遍历的变量。
a、设置name属性,name属性指定需要遍历的集合或Map,例如:
  <logic:iterate id="element" name="Animals">
   <bean:write name="element"/><BR>
</logic:iterate><p>
b、设置name属性和property属性,name属性指定一个JavaBean,property属性指定JavaBean的一个属性

,这个属性为需要遍历的集合或Map,例如:
<logic:iterate id="element" indexId="ind" name="catalog">
  <bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
  <logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
      -----<bean:write name="elementValue"/><BR>
  </logic:iterate>
</logic:iterate><P>
c、设置collection属性,collection属性指定一个运行时表达式,表达式的运算结果为需要遍历的集合

或Map,例如:
  <logic:iterate id="header" collection="<%= request.getHeaderNames() %>">
   <bean:write name="header"/><BR>
</logic:iterate>
 

在logic:iterate嵌入html:link标签:

<logic:iterate id="display" name="result">

     <html:link paramId="id" paramProperty="wzxw" paramName="display">

          <bean:write name="display" property="wzxw" />

     </html:link>

</logic:iterate> 

paramId="id"  是指在 url 中的id http://localhost/test.jsp?id=xxx ,如果 paramId="name",那么 url 中应该是 http://localhost/test.jsp?name=xxx

paramProperty="wzxw" 是你在 result 中的属性;

paramName="display" 是 logic:iterate 的 id

进行请求转发或重定向的Logic标签
1、<logic:forward>标签

该标签用于请求转发,它的name属性指定转发目标,於Struts配置文件中的<global-forwards>元素中的

子元素<forward>匹配,例如,在配置文件中:

      <global-forwards>
      <forward   name="index"   path="/index.jsp"/>
      </global-forwards>
这样jsp中通过<logic:forward>标签把请求转发给name属性为“index”的全局转发目标:
<logic:forward name ="index"/>
这样当浏览器访问标签所在jsp时,标签把请求转发给index.jsp,因此浏览器看到的是index.jsp

2、<logic:redirect>标签
该标签用于重定向,它的forward,href和page属性指定重定向目标,这几个属性的用法和<html:link>的

forward,href,page属性类似,例如:
  <logic:redirect href="http://www.apache.org"/>
这样浏览器访问标签所在jsp时,标签把请求重定向到href属性内容,因此浏览器看到的是

http://www.56boke.com