提出问题:
现在需要在页面上显示一个table,每一列都对应form中的一个数组属性,现在要在页面上显示,格式如下:
ID | NAME | AGE |
001 | Name1 | 24 |
002 | Name2 | 32 |
003 | Name3 | 22 |
Form的源码如下:
import org.apache.struts.action.ActionForm;
public class TestAForm extends ActionForm {
String login;
String password;
String[] id;
String[] name;
String[] age;
//String strc;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String[] getAge() {
return age;
}
public void setAge(String[] age) {
this.age = age;
}
public String[] getId() {
return id;
}
public void setId(String[] id) {
this.id = id;
}
public String[] getName() {
return name;
}
public void setName(String[] name) {
this.name = name;
}
}
那么在页面上如何遍历多个数组呢?思考中。。。。。。
解决办法:
显示页面,内容如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>testout.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This a struts page. <br>
aForm.login=<bean:write name="aForm" property="login" /><br>
aForm.password=<bean:write name="aForm" property="password" />
<table align='left'>
<th>
<td>ID</td>
<td>NAME</td>
<td>AGE</td>
</th>
<logic:iterate id="item" name="aForm" property="id" indexId="index">
<tr>
<td><bean:write name="item"/></td>
<td><bean:write name="aForm" property='<%="name[" + index + "]"%>'/></td>
<td><bean:write name="aForm" property='<%="age[" + index + "]"%>'/></td>
</tr>
</logic:iterate>
</table>
</body>
</html:html>
配置文件Struts-config.xml,内容如下:
<struts-config>
<data-sources />
<form-beans>
<form-bean name="aForm"
type="com.ffs.test.form.TestAForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/test"
type="com.ffs.test.action.TestAction"
name="aForm"
scope="request"
validate="false"
parameter="test"
input="/jsp/test/testin.jsp">
<forward name ="success" path="/jsp/test/testout.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
入口页面testin.jsp,内容如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>testin.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<html:form action="test.do" method="post" focus="login">
<table border="0">
<tr>
<td>Login:</td>
<td><html:text property="login" /></td>
</tr>
<tr>
<td>Password:</td>
<td><html:password property="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit /></td>
</tr>
</table>
</html:form>
</body>
</html:html>
还差一个Action文件TestAction.java,内容如下:
package com.ffs.test.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import com.ffs.test.form.TestAForm;
public class TestAction extends MappingDispatchAction {
public ActionForward test(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse rsp) {
TestAForm tf = (TestAForm)form;
tf.setLogin("A");
tf.setPassword("B");
String[] id = new String[]{"001", "002", "003", "004"};
String[] name = new String[]{"name1", "name2", "name3", "name4"};
String[] age = new String[]{"21", "22", "32", "24"};
tf.setId(id);
tf.setName(name);
tf.setAge(age);
//return (new ActionForward("success"));
return (mapping.findForward("success"));
}
}