今天回头看了下Struts1,觉得ActionForm的List的功能以前没有用到过,现在觉得有点记下来的冲动.
<一>ActionForm的主要功能
1.在显示页面的时候完成页面各种功能的初始化;
2.在用户提交请求的时候ActionForm又代表用户的提交的数据,供Action以及后续业务处理方法使用;
3.提供数据验证;
<二>在ActionForm中是列表属性
常用在这种场合,就是在页面上需要提交一系列相同属性的数据,列如:
1.定义ActionForm
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;
public class ListForm extends ActionForm {
private List addresses = new ArrayList();
private List friends = new ArrayList();
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
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;
}
}
2.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;
public class ListFormAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
ListForm listForm = (ListForm) form;
return mapping.findForward("display");
}
}
3.视图的实现:
<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>
4.输出页面的实现:
<%@ 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>
上面的例子,沒有通過測試.
第一: 加上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="form.ListForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action input="/index.jsp" name="ListForm" path="/ListForm" scope="request" type="action.ListFormAction" validate="true">
<forward name="display" path="/display.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
第二:我用的是ecllipse建立的struts所以,還把form表中的action修改為action="/ListForm.do",
第三:由於我沒有用taglib,所以輸出頁面的display我去掉了它的taglib.去掉了後面用el表達式的輸出。
第三:bean:write所寫的是request,session等所存的bean,所以我在action中加了 request.setAttribute("listForm",listForm);
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/foujoelxq/archive/2008/03/18/2193739.aspx