vo.java
表单元数
formbean 方
list voList = new ArrayList();
ok

解决方案 »

  1.   

    可以在客户端组装成一个一定格式的字符串提交,然后在action中按照格式解析。
      

  2.   

    to TangMixia(汤米虾):
    看得不大明白,可以详细些吗?
      

  3.   

    to s_phoenix():
    谢谢,这不失为一个好方法!
    但我想知道Struts有为这种常见情况提供方案吗
      

  4.   

    class FormBeanString att1;
    String att2;
    String att3;getAtt1
    setAtt1
      

  5.   

    TO  tomuno(tomuno) :
    这不是限定了一定是3个元素吗
      

  6.   

    <logic: iterate>
    </logic: iterate>
      

  7.   

    这么写方便
    以后改也好改
    比如说
    我先这么复制他30个属性
    用到几个就改几个
    要是 还满足不了你的需求
    那就用动态actionform吧
    DynaActionForm
    http://blog.msnfans.com/foxgem/archives/1913.html
      

  8.   

    DynaActionForm,自己在配置文件中进行配置,免得写ActionForm那么麻烦了
      

  9.   

    我是这样做的:(由于对BO理解不够,做的可能不正宗)
    <设计方案>(1)JSP代码: 从数据库采集来的collection使用iterate标签显示输出
    <html:form action="/beanAction">
    <logic:iterate id="list" name="yourActionForm" property="list" type="xxx" >
    <html:multibox name="list" property="selected">
    <bean:write name="list" property="userID"/>
    </html:multibox>        
    </html:form>(2)ActionForm代码:把每个用户的信息串个串~(把我的心~ 你的心 ~串成串~)acount是数组包含你要显示的属性,arraylist把他们连接起来.(我理解为这个是类似于vector的2维)public class YourActionForm extends ValidatorForm{

    private Account[] accountList;      
    public Account[] getAccountList(){
               return accountList;
    }
    public void setAccountList(Account[] accountList){
               this.accountList = accountList;
    }
    }(3)Action代码:使用到
    public class YourAction extends Action{ public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
    {
      YourActionForm yourActionForm = (YourAccountActionForm)actionForm;
      javax.sql.DataSource dataSource = (javax.sql.DataSource)this.servlet.getServletContext().getAttribute(....DATASOURCE_KEY);
      Account[] list;
      try
      {
      连接数据库,执行访问操作 得到collection,通过名叫accountbean里的 getAllUser方法去做.
                      java.sql.Connection connection = dataSource.getConnection();
    list = Account.getAllUser(connection);
      }
      catch(Exception exception)
      {
                      设置
      }
      yourActionForm.setAccountList(accountList);   return new ActionForward("/yourjsp.jsp");
    }
      }(4)Account代码:放置数组account声明,包含你的list里每条信息的所有属性~~~~(checkbox.list等)
    public class Account
    {
      public Account() {

    }  private String selected;
      private String list;
      private String password;
      private String userName;
     .......................
    想有什么属性就都写上吧
      private java.sql.Connection connection;
      
      
        public Account(String selected, String list, String password, String userName, String address, String userType) {

      this.selected = selected;
     ...................
      }
      
      public String getSelected()
      {
        return selected;
      }
      public void setSelected(String selected)
      {
    this.selected = selected;
      }
      public String getUserID()
      {
    return userID;
      }
      public void setUserID(String userID)
      {
    this.userID = userID;
      }
      public String getPassword()
      {
    return password;
      }
      public void setPassword(String password)
      {
    this.password = password;
      }
      public String getUserName()
      {
    return userName;
      }
      public void setUserName(String userName)
      {
    this.userName = userName;
      }
      public String getAddress()
      {
    return address;
      }
      public void setAddress(String address)
      {
    this.address = address;
      }
      public String getUserType()
      {
    return userType;
      }
      public void setUserType(String userType)
      {
    this.userType = userType;
      }
      /**
       * Set the sql connection,this method shoud be called before <strong>isExist</strong>, <strong>newAccount</strong>, <strong>loadAccount</strong> and <strong>saveAccount</strong>
       * @param connection sql connection
       */
      public void setConnection(java.sql.Connection connection)
      {
    this.connection = connection;
      }  /**
       * Check whether such account exists
       * @return true if exists, else false
       */
      public String isExist()
      {
    try
    {
      String sql = "SELECT * FROM MA_USER WHERE USERID='"+ this.userID + "' AND PASSWORD='" + this.password + "'";
      java.sql.Statement statement = this.connection.createStatement();
      ResultSet resultSet = statement.executeQuery(sql);
      if(resultSet.next())
      {
    String userType = resultSet.getString("USERTYPE");
    if(userType.equals("")){
    String noexit = "none";
    return noexit;
    }else if(userType.equals("administrator")){
    String adminexit = "administrator"; 
        return adminexit;
    }else{
    String guestexit = "guest";
    return guestexit;
    }
      }
      resultSet.close();
    }
    catch(Exception exception)
    {
      exception.printStackTrace();
    }
    String databasestatus="disconnected";
    return databasestatus;
      } 从数据库取得collection的方法getAllUser
     public static Account[] getAllUser(Connection connection) throws SQLException
    {
      String sql="SELECT * FROM YOUTABLE";
      Statement statement = connection.createStatement();
      ResultSet resultSet = statement.executeQuery(sql);
      ArrayList arrayList = new ArrayList();   while(resultSet.next())
      {
    String selected = "";
    String userID = resultSet.getString("USERID");
    .......................
    Account account = new Account(selected,.....);
                      把你的心我的心\串成串\串成串
    arrayList.add(account);
      }
               返回你的串就是了~~~ 然后actionform里就可以给你的iterate设置值拉,说的直饿,晚上吃羊肉串去.
      return (Account[])arrayList.toArray(new Account[arrayList.size()]);
    }}
      

  10.   

    DynaActionForm还不是需要去更改配置文件?还是不满足楼主的要求啊!
    在我的项目中,我都是用我说的方法,这样才是真正灵活的办法。不过我也一直想知道是否有更好的解决方案。
      

  11.   

    写的太仓促了~ 把很多没用的代码也带上了,不好意思哈~ 第4步Account里只是public static Account[] getAllUser是我想说的
      

  12.   

    to Arqui(Arqui):
    我理解不了您的代码啊。
    formBean里为什么没有与表单元素的property对应的field?
    multibox还好办,它自动就会搜寻可以不定元素的array与之相对应,
    像<html:select>,<html:text>这些,如果不能确定数目的话没法设计formBean中字段的啊to tomuno(tomuno):
    当然不能一一手动修改啊,元素数目完全就是动态的,总不能一次次的改吧;
    至于DynaActionForm,第一次接触,看来还要好好研究一下看能否用得上。to s_phoenix() :
    hand~~看来不得已还是要用您提出的那种解析字符串方法了!
    我只是想学习一下Struts有没有更自然一点的解决方案。to sonsan() :
    您理解错我的意思了!
    formBean并不是把信息传到页面的formBean,
    而是表单提交后用于传递数据到Action的formBean
      

  13.   

    看了一下那文章,似乎DynaActionForm的功能也只是自动产生acitonForm,并未提供动态确定元素数目的功能啊。配置文件里一样要把form里有多少个元素写死
      

  14.   

    这种情况我的建议是撇开struts,应用Servlet+bean手动控制表单个数,有规律的还可以做成模板,以后开发配置一下,一个模块就出来了非常方便高效!!!
      

  15.   

    阿~你所说的不定数不就是 那个表是根据数据库生成的吗???????数据库有一条就生成一条记录在JSP与之想对应,只要是这样的话我那个就没错拉:),呵呵~反正我是这么做的.好用好用.formbean给我的感觉是用来做表单验证的,取值我喜欢用request.我说的那个actionbean是用Account[]这个家伙定义的.Account[]是这样定义的(见4).也许我理解的不对,希望你解决,但是解决后把代码帖出来呗.谢谢.