[原创]用iterate输出多维数组

此文在“使用struts的标记”一文基础上稍微改动。

iterate-view.jsp:

<logic:iterate id="iterate" name="ibean" property="name" scope="request" offset="0" >
★〓〓〓〓〓〓〓〓★<br>
<%
String[] temp = (String[]) iterate;
%>

<%=temp[0]%> | <%=temp[1]%> | <%=temp[2]%> <p>
</logic:iterate>

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 = {{"a1","a2","a3"},{"b1","b2","b3"}};
ibean.setName(temp);
return mapping.findForward("iterate-view");
}
}

IterateBean.java

package beantest;

import org.apache.struts.action.ActionForm;

public class IterateBean extends ActionForm {
private String[][] name = null;
public void setName(String[][] name) {
this.name = name;
}
public String[][] getName() {
return this.name;
}
}

输出效果:

http://localhost:8080/mystruts/eye/iterate-test

★〓〓〓〓〓〓〓〓★
a1 | a2 | a3

★〓〓〓〓〓〓〓〓★
b1 | b2 | b3

可见,虽然iterate标记本身比较简单,但结合一些技巧,仍然能够实现一些特殊功能。

你可能感兴趣的:(apache,jsp,struts,servlet)