熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts的標記完成復雜循環

2022-06-13   來源: Java開源技術 

  使用struts中的<logic:iterate>標記可以完成一些復雜的循環
  下面用了一個小例子說明這一點
  
  IterateBeanjava:
  
  package beantest;
  
  import orgapachestrutsactionActionForm;
  
  public class IterateBean extends ActionForm {
  private String[] name_coll = null;
  public void setName_coll(String[] name_coll) {
  thisname_coll = name_coll;
  }
  public String[] getName_coll() {
  return thisname_coll;
  }
  
  private String[] name_new = null;
  public void setName_new(String[] name_new) {
  Systemoutprintln(IterateBean:setName_new(String[]));
  thisname_new = name_new;
  }
  public String[] getName_new() {
  return thisname_new;
  }
  }
  
  IterateActionjava
  
  package beantest;
  
  import javaxservlethttpHttpServletRequest;
  import javaxservlethttpHttpServletResponse;
  
  import orgapachestrutsactionAction;
  import orgapachestrutsactionActionForm;
  import orgapachestrutsactionActionForward;
  import orgapachestrutsactionActionMapping;
  
  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};
  ibeansetName_coll(temp);
  temp = new String[] {aaaaa bbbbb};
  ibeansetName_new(temp);
  return mappingfindForward(iterateview);
  }
  }
  
  iterateviewjsp:
  
  <%@ page contentType=text/html;charset=gb %>
  
  <%@ taglib uri=/WEBINF/strutslogictld prefix=logic %>
  <%@ taglib uri=/WEBINF/strutsbeantld prefix=bean %>
  <logic:iterate id=iterate name=ibean property=name_coll scope=request offset= >
  
  <logic:iterate id=iterate_new name=ibean property=name_new scope=request offset= >
  name: <font color=red><%=iterate%></font> ; code: <font color=red><%=iterate_new%></font><br>
  </logic:iterate>
  
  ★〓〓〓〓〓〓〓〓★<br>
  <bean:include id=lower page=/test/lowerjsp />
  <bean:write name=lower filter=false/><br>
  </logic:iterate>
  
  lowerjsp:
  
  <font color=blue>hello iterate!</font>
  
  strutsconfigxml:
  
  <?xml version= encoding=ISO?>
  <!DOCTYPE strutsconfig PUBLIC
  //Apache Software Foundation//DTD Struts Configuration //EN
  config__dtd>
  <strutsconfig>
  <formbeans>
  <formbean type=beantestIterateBean name=ibean />
  </formbeans>
  <actionmappings>
  <action path=/iteratetest type=beantestIterateAction name=ibean scope=request >
  <forward path=/test/iterateviewjsp name=iterateview />
  </action>
  </actionmappings>
  </strutsconfig>
  
  webxml:
  
  <?xml version= ?>
  <!DOCTYPE webapp PUBLIC //Sun Microsystems Inc//DTD Web Application //EN app__dtd>
  <webapp>
  
  <servlet>
  <servletname>action</servletname>
  <servletclass>orgapachestrutsactionActionServlet</servletclass>
  <initparam>
  <paramname>config</paramname>
  <paramvalue>/WEBINF/strutsconfigxml</paramvalue>
  </initparam>
  <loadonstartup></loadonstartup>
  </servlet>
  <servletmapping>
  <servletname>action</servletname>
  <urlpattern>/eye/*</urlpattern>
  </servletmapping>
  
  </webapp>
  
  輸出效果
  
  name: bitan ; code: aaaaa
  name: bitan ; code: bbbbb
  ★〓〓〓〓〓〓〓〓★
  hello iterate!
  name: helen ; code: aaaaa
  name: helen ; code: bbbbb
  ★〓〓〓〓〓〓〓〓★
  hello iterate!
  
  如上所示一個循環中不能直接將多個變量同時輸出但在循環中嵌套循環可以實現一種復雜效果
  
  要實現在一個循環中將多個變量同時輸出可以引用<bean:include><bean:write>標記

From:http://tw.wingwit.com/Article/program/Java/ky/201311/28010.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.