property  一一 对应  应该可以的

解决方案 »

  1.   

    请给个简单的例子,现在我显示没有问题,说明property应该是已经一一对应了,但是提交的时候actionform中的数据,确切的说应该是actionform中的list中的记录对象中的对应值并没有被更新。迷惑中,不知道struts中是不是提供了对应的提交处理。
      

  2.   

    通过数组可以做的,
    public class XXXForm ...{
      private String names[];
    }
    页面:
    <html:select property="names" multiple="true">
    ...
    </html:select>
      

  3.   

    struts最后还是要通过Form里的域比如<input type="text"之类进行提交。
    struts能做的只是自动将这些数据收集到Form里对应的变量中。
    能利用的一个对应是Checkbox,Struts做了一个数组可以收集。
    我的建议:将List每一行都加一个checkbox, 其值为键值。
    这样就可以在Form中得到一个键值的数组,一般够用了。
      

  4.   

    checkbox是可以实现的,常用的就是放一个ID在checkbox里。不过如果每一行所有数据都要提交回去的话。
    我想可以用多个hidden对应每个相同的属性,名字一样就行了,提交回去后,就有了多个相同属性的数组。
    不过我没有用过,只是想了想,不知对不?
    学习中~~~~~~~~~~~~~~~~
      

  5.   

    必须用数组么?  用list可以提交么?
      

  6.   

    不可以,这个问题我遇到过,我已经解决了,呵呵
    需要这个样子的:当你提交上来的数据需要在form里面定义需要提交上来的数组,一般都是用String a[]这种形势,这样就可以提交,然后自己写一个方法,把提交上来的数据重新初始化为自己想要得类。嘿嘿,我那个项目就是这样解决的,目前正常运行没有问题
    例如:
    class A {
       private String name;
        private String address;
    }
    class form{
        A[]   test = null;
        String[] name = null;//A类里面需要提交上来的数据
    }
    然后再Action的setFormToBean()方法里面根据上传上来的name重新创建A类的实例数组
      

  7.   

    应该是不可以的,struts如何知道你需要获得的对象类型?
    webwork是通过以下方式解决这个问题的:
    //
    private List listData = new XWorkList(XXXData.class); 
    //
    也许新版本的struts中会有?
      

  8.   

    这样你可以通过两上formbean来处理,一个是专门用来处理你列表中的信息的A,一个专来用来处理其它页面信息B(主从表),在B中用一个属性private ArrayList aList=new AutoArrayList(A.class);
    AutoArrayList是继承了ArrayList的类,其中构造函数的一个参数为AutoArrayList(Class iTem),这样是利用反射了吧!!在其中有一个方法   public Object get(int index) {        try {            while (index >= size()) {                add(itemClass.newInstance());            }        } catch (Exception e) {            e.printStackTrace();        }        return super.get(index);    }
    是用来处理类组的吧!!
    页面上通过
    <logic:iterate id="aList"  name="B" property="aList" indexId="index">
    <html:text name="aList" property="A中的属性" indexed="true"/>
    </logic:iterage>
    这样aList就会与B表中的aList对应了,其中<html:text/>中的名都成了aList[].A表中的属性了,于是也变成了数据处理了!事实上有点象处理Hibernate中的many-to-one那种吧!以上是我的一些小认识大家多讨论啦!!
      

  9.   

    日月你好,已经在考虑作完项目自己通过反射写个类留用了。看了你的回答对简单的通过list处理又燃起了希望,目前有两个问题。1。其中构造函数的一个参数为AutoArrayList(Class iTem),这样是利用反射了吧!!
    ArrayList并没有ArrayList( Class item )这样的构造函数,使自己写的么?如果使自己写的,那么构造函数里面应该写些什么?2。while (index >= size()) {                add(itemClass.newInstance());            }
    为什么要添加以上的处理?以上例子我测试没有成功,你手头有可以成功的简单例子可以提供给我参考一下么?
      

  10.   

    用数组虽然没问题,不过一个数据库里面几十个(n)字段,那么属性名称+set,get方法对就是3*n的机械工作量,这种经常会碰到的问题,用数组个人觉得还是不太好,所以想寻求一下有没有好的解决方案,有兴趣的大家可以一起讨论一下。
      

  11.   

    可以结合javascript来做,把grid的信息组成一个xml或者string,然后以hidden字段的形式提交到server端,再对xml或者string进行解析。
      

  12.   

    likeBean(喜欢吃咖啡豆) 说得是个好办法,现在项目中就这么用。
      

  13.   

    偶系新人
    不知道我的方法好不好用。我以前做过这样的问题。你有没有考虑到用hidden来做做看呢
      

  14.   

    碰巧刚做完一个项目就是list提交的,日月的方法是正确的,那个构造函数不是必需的,改写的arrayList如下public class Sk0007S001Bean extends ArrayList {

    private static FcLoggerInterface mylogger =
    FcLoggerFactory.getLogger("Sk0007S001Bean");

    private Sk0007S000Bean sk0007S000Bean = null; public Sk0007S001Bean(Sk0007S000Bean sk0007S000Bean) {
    String METHOD_NAME = "Sk0007S001Bean";

    mylogger.methodTrace(METHOD_NAME, FcMsg.START);
    this.sk0007S000Bean = sk0007S000Bean;
    mylogger.methodTrace(METHOD_NAME, FcMsg.END);
    }

    public Object get(int index) {
    String METHOD_NAME = "get";

    mylogger.methodTrace(METHOD_NAME, FcMsg.START);
    while (index >= size()) {
    /*初期化*/
    add(new Sk0007S000Bean());
    }
    mylogger.methodTrace(METHOD_NAME, FcMsg.END);
    return super.get(index);
    }
    }
      

  15.   

    DynaActionForm只支持固定长度的collection,所以我是这么做的:
    ==================================================
    struts-config.xml中的formBean<form-bean name="cityInfoActionForm" type="org.apache.struts.action.DynaActionForm">
    <form-property name="set_id" type="java.lang.String[]" />
    <form-property name="setName" type="java.lang.String[]" />
    <form-property name="setType" type="java.lang.String[]" />
    <form-property name="address" type="java.lang.String[]" />
    <form-property name="setTel" type="java.lang.String[]"/>
    <form-property name="i1c_id" type="java.lang.String"/>
    <form-property name="type" type="java.lang.String"/>
    </form-bean>
    =============================================
    actionDynaActionForm cityInfo_af=(DynaActionForm)actionForm;
    String[] setName=cityInfo_af.getStrings("setName");
    String[] setType=cityInfo_af.getStrings("setType");
    String[] address=cityInfo_af.getStrings("address");
    String[] setTel=cityInfo_af.getStrings("setTel");
    for(int i=0;i<setName.length;i++)
    {
    CityInfo cityInfo=new CityInfo();
    cityInfo.setItem1Class(i1c);
    cityInfo.setType(type);
    cityInfo.setLanguage(language);
    cityInfo.setSetName(setName[i]);
    cityInfo.setSetType(setType[i]);
    cityInfo.setAddress(address[i]);
    cityInfo.setSetTel(setTel[i]);
    cityInfoDAOImpl.insert(cityInfo);
    }
    =====================================================
    JSP页面<%@ page contentType="text/html; charset=utf-8" errorPage="/error.jsp" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <logic:notPresent name="adminBean" scope="session">
      <logic:forward name="error"/>
    </logic:notPresent>
    <bean:parameter name="type" id="type"/>
    <bean:parameter name="i1c_id" id="i1c_id"/>
    <html>
      <body>
        <TABLE width="100%" cellPadding=2 cellSpacing=1 bgcolor="#CACADB" id=t136>
          <tr align="center" bgcolor="#E4E7EF">
            <td height="20">
              <bean:message key="city.set.form.setName"/>
            </td>
            <td>
              <logic:equal name="type" value="food">
                <bean:message key="city.set.form.food.setType"/>
              </logic:equal>
              <logic:notEqual name="type" value="food">
                <bean:message key="city.set.form.yule.setType"/>
              </logic:notEqual>
            </td>
            <td>
            <logic:equal name="type" value="traffic">
                <bean:message key="hotel.form.intro"/>
              </logic:equal>
    <logic:notEqual name="type" value="traffic">
              <bean:message key="golf.form.address"/>
    </logic:notEqual>
            </td>
            <td>
              <bean:message key="hotel.form.tel"/>
            </td>
            <td>
              <a href="javascript:void(0)" onclick=addNew()>
                添加项目
              </a>
            </td>
          </tr>
          <form action="/admin/cityInfoAdd.do" method="post">
          <input type="hidden" name="type" value="<bean:write name="type"/>">
          <input type="hidden" name="i1c_id" value="<bean:write name="i1c_id"/>">
            <tr align="center" bgcolor="#ffffff">
              <td>
                <input type="text" name="setName" size="10">
              </td>
              <td>
                <input type="text" name="setType" size="10">
              </td>
              <td>
              <logic:equal name="type" value="traffic">
                <textarea type="text" name="address"></textarea>
              </logic:equal>
    <logic:notEqual name="type" value="traffic">
                <input type="text" name="address" size="10">
                </logic:notEqual>
              </td>
              <td>
                <input type="text" name="setTel" size="10">
              </td>
              <td></td>
            </tr>
      </table>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td align="center">
    <input type="submit" value="<bean:message key="button.submit"/>" class="btn-1">
            <input type="reset" value="<bean:message key="button.reset"/>" class="btn-1">
    </td>
          </tr>
        </table>
        <script>
    var i=0,arr=new Array('Ffffff','Ffffff');
    function addNew(){
    i++;
    tr=document.all.t136.insertRow();
    tr.style.backgroundColor=arr[i%2];
    tr.insertCell().innerHTML='<div align=center><input type="text" name="setName" size="10"></div>';
    tr.insertCell().innerHTML='<div align=center><input type="text" name="setType" size="10"></div>';
    //<logic:equal name="type" value="traffic">
    tr.insertCell().innerHTML='<div align=center><textarea type="text" name="address"></textarea></div>';
    //</logic:equal>
    //<logic:notEqual name="type" value="traffic">
    tr.insertCell().innerHTML='<div align=center><input type="text" name="address" size="10"></div>';
    //</logic:notEqual>
    tr.insertCell().innerHTML='<div align=center><input type="text" name="setTel" size="10"></div>';
    tr.insertCell().innerHTML='<a href=javascript:void(0) onclick=del()>删除</a>';
    }
    function del(){
    document.all.t136.deleteRow(window.event.srcElement.parentElement.parentElement.rowIndex);
    for(i=0;i<document.all.t136.rows.length-4;i++){
    document.all.t136.rows[i+4].cells[1].children[0].name="setName";
    document.all.t136.rows[i+4].style.backgroundColor=arr[i%2];
    }
    }
    </script>
    </body>
    </html>
      

  16.   

    while (index >= size()) {
    /*初期化*/
    add(new Sk0007S000Bean());
    }
    这样处理主要就是初始化list,因为提交list的时候容器会验证list中的值是否为bean,如果不是会抛出BeanUtils.populate错误。对于页面上的不能提交的标签如bean:write之类的,要想通过这种方式提交,何以设为hidden项,或者在struts-config中把action中的scope去掉,变成默认,实际上就是把值放到session中去也可以提交!
      

  17.   

    form中用数组属性
    jsp的htm;input中name和数组名相同就行了
      

  18.   

    请教:我采用了上述方法后,但仍然无法取得页面修改后的值,在页面中
    <logic:iterate  id="lotteryOptions" name="testform" property="lotteryOptions"  indexId="index">  
    选项:<html:text name="lotteryOptions"  property="optionValue"  indexed="true" /> 
    应答:<html:text name="lotteryOptions"  property="Reply"  indexed="true" size="3"/> 
    <br/>
    </logic:iterate>在Formbean testform中对lotteryOptions定义如下
    private ArrayList lotteryOptions = new OptionList(LotteryOption.class);
    public void setLotteryOptions(ArrayList loOptions){
    this.lotteryOptions = loOptions;
    }
    public ArrayList getLotteryOptions(){
    return this.lotteryOptions;
    }并且定义了另一个formbean LotteryOption 如下:
    public class LotteryOption extends ActionForm {
    private String optionValue;
    private String smsReply;

    public String getOptionValue(){
    return this.optionValue;
    }
    public void setOptionValue(String value){
    this.optionValue = value;
    }
    public String getSmsReply(){
    return this.smsReply;
    }
    public void setSmsReply(String reply){
    this.smsReply = reply;
    }
    }但在页面上输入数据后提交,发现并未设置到lotteryList中,