You can try Map-backed ActionForms:
The DynaActionForm classes offer the ability to create ActionForm beans at initialization time, based on a list of properties enumerated in the Struts configuration file. However, many HTML forms are generated dynamically at request time. Since the properties of these forms' ActionForm beans are not all known ahead of time, we need a new approach. Struts allows you to make one or more of your ActionForm's properties' values a Map instead of a traditional atomic object. You can then store the data from your form's dynamic fields in that Map. Here is an example of a map-backed ActionForm class: public FooForm extends ActionForm {    private final Map values = new HashMap();    public void setValue(String key, Object value) {
        values.put(key, value);
    }    public Object getValue(String key) {
        return values.get(key);
    }}In its corresponding JSP page, you can access objects stored in the values map using a special notation: mapname(keyname). The parentheses in the bean property name indicate that: The bean property named mapname is indexed using Strings (probably backed by a Map), and that 
Struts should look for get/set methods that take a String key parameter to find the correct sub-property value. Struts will, of course, use the keyname value from the parentheses when it calls the get/set methods. 
Here is a simple example: <html:text property="value(foo)"/>
This will call the getValue method on FooForm with a key value of "foo" to find the property value. To create a form with dynamic field names, you could do the following: 
<% 
for (int i = 0; i < 10; i++) {
String name = "value(foo-" + i + ")";
%>
<html:text property="<%= name %>"/>
<br/>
<%
}
%>Note that there is nothing special about the name value. Your map-backed property could instead be named property, thingy, or any other bean property name you prefer. You can even have multiple map-backed properties on the same bean. In addition to map-backed properties, you can also create list-backed properties. You do so by creating indexed get/set methods on your bean: public FooForm extends ActionForm {    private final List values = new ArrayList();    public void setValue(int key, Object value) {
        values.set(key, value);
    }    public Object getValue(int key) {
        return values.get(key);
    }
}In your presentation pages, you access individual entries in a list-backed property by using a different special notation: listname[index]. The braces in the bean property name indicate that the bean property named listname is indexed (probably backed by a List), and that Struts should look for get/set methods that take an index parameter in order to find the correct sub-property value. 

解决方案 »

  1.   

    public class ShowAction extends Action {
      public ActionForward execute(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {
      

  2.   

    可以自己设计一个类,配合struts logic tag 来显示。
    这个类包括一个collection类的valueList 用logic:iterate  标签
      

  3.   

    我正在实验yaoliang1981(yaoliang) 的方法,如果成了把结果公布.
      

  4.   

    OK,我的实验成功了,源代码如下:
    JSP文件:
    testMapActionInput.jsp:
    <%@ page contentType="text/html; charset=GBK" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <html>
    <head>
    <title>
    testDynaActionInput
    </title>
    </head>
    <body>
    <h1>试一下表单动态添加</h1>
    <p>
    <html:form action="/testMapAction.do">
    <% 
    for (int i = 0; i < 15; i++) {
    String name = "fields(field" + i + ")";
    %>
    <html:text property="<%= name %>"/>
    <br/>
    <%
    }
    %>
    <html:submit property="submit" value="Submit"/><br>
    <html:reset value ="Reset"/>
    </html:form>
    </body>
    </html>struts-config.xml:
    .
    .
    <form-bean name="testMapForm" type="org.apache.struts.action.DynaActionForm">
          <form-property name="fields" type="java.util.HashMap" />
    </form-bean>
    .
    .
    <action name="testMapForm" path="/testMapAction" scope="session" type="test.example.action.TestMapAction">
          <forward name="success" path="/testMapActionOutput.jsp"/>
    </action>  
    .
    .
    TestMapAction.java:
    package test.example.action;import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionForm;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.DynaActionForm;
    import org.apache.struts.action.Action;
    import java.util.HashMap;public class TestMapAction
        extends Action {
      public ActionForward execute(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest servletRequest,
                                   HttpServletResponse servletResponse) throws Exception{
        //DynaActionForm dynaActionForm = (DynaActionForm) actionForm;
        //获取属性
        HashMap hmFields=(HashMap)((DynaActionForm)actionForm).get("fields");
        //控制台输出一下看获取成功没有
        System.out.println("Map内容:"+hmFields);
        servletRequest.setAttribute("fieldsMap",hmFields);
        return actionMapping.findForward("success");
      }
    }
    .
    .
    输出结果的JSP:
    <%@ page contentType="text/html; charset=GBK" %>
    <%@ page import="java.util.HashMap" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <html>
    <head>
    <title>
    testDynaActionOutput
    </title>
    </head>
    <body bgcolor="#ffffff">
      <%
      HashMap hmFields=(HashMap)request.getAttribute("fieldsMap");
      %>
    <h1>
      <%=hmFields %>
    </h1>
    </body>
    </html>
    楼主试一下!!!!
      

  5.   

    忘了说了,输出结果的JSP就是testMapActionOutput.jsp.
    显示表单时,你把for循环变成对你的数据库的结果集的遍历就可以了。
    你是不是已经做出来了,贴出来,看看有没有不同。
      

  6.   

    DynaActionForm的代码呢????
      

  7.   

    最关键的代码居然不贴出来!!!你的<html:text property=fields(fieldi)/>对应DynaActionForm中的什么字段啊 ???
      

  8.   

    Action类的写法
    public class CustomerAction extends MyjohoyaAction {
      public ActionForward execute(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {
    show(httpServletRequest);
      }
      public void show(HttpServletRequest request){
    ArrayList fa=null;
            conn = getConnection(request);//获得一个连接
    fa=getShow(conn);
    request.setAttribute("show_list",fa);
      }    protected Connection getConnection(HttpServletRequest httpServletRequest) {
            DataSource dataSource = null;
            Connection conn = null;
            try {
                dataSource = getDataSource(httpServletRequest);
                conn = dataSource.getConnection();
            }
            catch (SQLException ex) {
                ex.printStackTrace();
            }
            return conn;
        }  public Arraylist getShow(conn){
    ArrayList al=new ArrayList();
         Statement st = conn.createStatement();
         ResultSet rs = st.executeQuery(sql语句);
    while(rs.next()){
    al.add(rs.getString(1));
    }
    return al;
     }jsp页面的部分代码
    <logic:iterate id="list" name="show_list" scope="request">
    <td>
           <html:text property="<bean:write name='list'/>" maxlength="16" size="18"/>
    </td>
    </logic:iterate>
    大概就是这样,一些细节还需要你自己改一下.
      

  9.   

    TO sboom(+-LingCh-+)(怪你过分美丽) 
    DynaActionForm的代码没有代码啊???是动态Form啊,在XML里配置的呀!
      

  10.   

    这就是DynaActionForm的代码,已经贴出来了.
    <form-bean name="testMapForm" type="org.apache.struts.action.DynaActionForm">
          <form-property name="fields" type="java.util.HashMap" />
    </form-bean>
      

  11.   

    啊!我明白了!
    public FooForm extends ActionForm {    private final List values = new ArrayList();    public void setValue(int key, Object value) {
            values.set(key, value);
        }    public Object getValue(int key) {
            return values.get(key);
        }
    }我说的actionform就是这个代码,原来上面有。哈哈,谢谢!!!
      

  12.   

    你用动态的Form,都不用写代码,连Java文件都没有,多省事