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