WEB开发网
开发学院WEB开发Jsp Struts的标记完成复杂循环 阅读

Struts的标记完成复杂循环

 2008-01-05 10:13:50 来源:WEB开发网   
核心提示:使用struts中的<logic:iterate>标记,可以完成一些复杂的循环,下面用了一个小例子说明这一点:IterateBean.java:package beantest;import org.apache.struts.action.ActionForm;public class IterateBe

  使用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>标记。

Tags:Struts 标记 完成

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接