Struts1中ActionForm 的List运用

今天回头看了下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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值