index.jsp
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html>
<head>
<title>ActionForm Samples</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>ActionForm Sample</h1>
<ul>
<LI><html:link page="/listFormInput.jsp">List-Backed Form</html:link></LI>
</ul>
</body>
</html>
listFormInput.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html>
<head>
<title>List-backed Form Property</title>
</head>
<body>
<h2>List Form Input</h2>
<html:form action="/listForm">
Who are your 3 friends:<br />
Friend 1: <html:text property="friend[0]"/><br />
Friend 2: <html:text property="friend[1]"/><br />
Friend 3: <html:text property="friend[2]"/><br />
Who are your 3 addresses:<br />
Address 1: <html:text property="address[0]"/><br />
Address 2: <html:text property="address[1]"/><br />
Address 3: <html:text property="address[2]"/><br />
<html:submit/>
</html:form>
</body>
</html>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="listForm" type="cn.hxex.actionform.struts.form.ListForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="listForm"
input="/listFormInput.jsp"
name="listForm"
path="/listForm"
scope="request"
type="cn.hxex.actionform.struts.action.ListFormAction">
<forward name="display" path="/listForm.jsp" />
</action>
</action-mappings>
<message-resources parameter="cn.hxex.actionform.struts.ApplicationResources" />
</struts-config>
listForm.java
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl
package cn.hxex.actionform.struts.form;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts Creation date: 05-31-2006
*
* XDoclet definition:
*
* @struts.form name="listForm"
*/
public class ListForm extends ActionForm {
// --------------------------------------------------------- Instance
// Variables
private List addresses = new ArrayList();
private List friends = new ArrayList();
// --------------------------------------------------------- Methods
/**
* Method validate
*
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
*
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
this.addresses.clear();
this.friends.clear();
}
public List getAddresses() {
return addresses;
}
public void setAddresses(List address) {
this.addresses = address;
}
public void setAddress(int index, String address) {
if (this.addresses.size() > index) {
this.addresses.set(index, address);
} else {
while( this.addresses.size() < index ) {
this.addresses.add( null );
}
this.addresses.add(index, address);
}
}
public String getAddress(int index) {
if (this.addresses.size() > index) {
return (String) this.addresses.get(index);
}
return null;
}
public List getFriends() {
return friends;
}
public void setFriends(List friend) {
this.friends = friend;
}
public void setFriend(int index, String friend) {
if (this.friends.size() > index) {
this.friends.set(index, friend);
} else {
while( this.friends.size() < index )
{
this.friends.add( null );
}
this.friends.add(index, friend);
}
}
public String getFriend(int index) {
if (this.friends.size() > index) {
return (String) this.friends.get(index);
}
return null;
}
}
ListFormAction.java
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl
package cn.hxex.actionform.struts.action;
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;
import cn.hxex.actionform.struts.form.ListForm;
/**
* MyEclipse Struts
* Creation date: 05-31-2006
*
* XDoclet definition:
* @struts.action path="/listForm" name="listForm" scope="request" validate="true"
*/
public class ListFormAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
ListForm listForm = (ListForm) form;
return mapping.findForward("display");
}
}
listForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>List-backed Form Property</title>
</head>
<body>
<h2>List Form Output</h2>
Your friends:<br />
Friend 1: <bean:write name="listForm" property="friend[0]"/><br />
Friend 2: <bean:write name="listForm" property="friend[1]"/><br />
Friend 3: <bean:write name="listForm" property="friend[2]"/><br />
Your addresses:<br />
Address 1: ${listForm.addresses[0]}<br />
Address 2: ${listForm.addresses[1]}<br />
Address 3: ${listForm.addresses[2]}<br />
</body>
</html>