在struts2:OGNL表达式,遍历List、Map集合;投影的使用一文中已经讲述了OGNL遍历List、Map集合等功能。
本文简单写一个遍历数组的示范程序。
1. 遍历自定义字符串数组
<%
String[] myArray = new String[]{"你好","我好","大家好"};
request.setAttribute("myArray", myArray);
%>
<s:iterator value="#request.myArray" status="st">
<s:iterator value="#request.myArray[#st.index]">
<s:property />
<br/>
</s:iterator>
</s:iterator>
输出:
2. 遍历Action实例所引用对象中的数组
Action类UploadTest2Action,引用FileUploadTools类;FileUploadTools类中包含uploadFileFileName的数组,要求在JSP中遍历此数组。
UploadTest2Action.java
public class UploadTest2Action extends ActionSupport {
private FileUploadTools fileUploadTools = new FileUploadTools();
// ......
}
FileUploadTools.java
public class FileUploadTools {
private String uploadFileFileName[];// 文件名是数组类型
public String[] getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String[] uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
// ......
}
JSP中引用
<s:iterator value="fileUploadTools.uploadFileFileName" status="st">
<s:iterator value="fileUploadTools.uploadFileFileName[#st.index]">
<s:property />
<br/>
</s:iterator>
</s:iterator>
输出:
略