Struts的<logic:iterate>标记完成复杂循环
2008-01-05 08:14:33 来源:WEB开发网核心提示:使用struts中的<logic:iterate>标记,可以完成一些复杂的循环,下面用了一个小例子说明这一点:IterateBean.java:package beantest;import org.apache.struts.action.ActionForm;
使用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>标记。进入讨论组讨论。
- ››Struts2配置文件中redirect传递中文参数乱码的解决...
- ››StrutsTestCase 简化开发过程
- ››struts 中一些包的详解
- ››struts1与struts2的区别
- ››struts学习1
- ››struts-2.1.8.1+jquery-1.3.2+spring-framework-2...
- ››Struts优缺点及实施经验总结
- ››Struts优缺点剖析
- ››Struts2中的DMI
- ››Struts2入门介绍1 -- Struts2介绍及运行一个简单...
- ››Struts2 redirectAction转向时默认加pass的问题
- ››Struts2.1.6--想用通配符,不容易
更多精彩
赞助商链接