当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

Struts的标记完成复杂循环


  使用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>标记。
相关内容
赞助商链接