Java Struts 多行提交到Form的List元素的属性中

博客首页 » Java Struts 多行提交到Form的List元素的属性中

发布于 29 Oct 2013 14:51
标签 blog
http://tech.ddvip.com/2008-12/122881555798671_2.html

提交多行数据到Struts的ActionForm的List属性中
2012-11-06 00:38:06 发表评论

WEB 应用中一般都会处理主从表的信息, 或者称之为头层与行层的一对多的关系数据,如订单头/订单明细. 对于这种关系数据提交到后台的 Struts 的 ActionForm 的话, 这个 ActionForm 就要好好的设计一下, 不然会给自已带来许多额外的代码. 比如有的人的处理方法就是把页面提交到后台的毫无关系的散装数据非常吃力的拼凑一对多的关系对象出来.
  下面举一个如今非常现实的关于股票的例子, 简单的应用场景是: 记录某个帐户所持有的股票信息,提交到后台,然后显示出来. 输入页面如下图
  帐户信息包括帐户名和资金帐号;持有股票的每一行信息包括股票代码, 股票名称, 成本价, 股票数量. 股票行可以动态增删.

  输入页面 input.jsp

  后台处理类图
  为了简化不必要的代码, 我们要实现的终及目标是: 在输入页面上点击 "保存数据" 按钮, 由 Struts 的 RequestProcessor.processPopulate() 方法把页面提交的基本信息组装到 AccountStockForm 的 account 的对应属性中,股票行信息对应生成一个 Stock 实例加到 AccountStockForm的 List 属性 stocks 中, 后续在 AccountStockAction 中直接处理account和stocks属性就非常简单了. AccountStockForm在这里只作为一个壳.
  下面从前台到后台说明关键性的代码, 完整的 MyEclipse 工程包可以点击 TestStruts135.zip下载到.
  一: struts-config.xml 配置
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 <?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts-config PUBLIC 
     "-Apache Software FoundationDTD Struts Configuration 1.3EN" 
     "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 
<struts-config> 
  <form-beans> 
    <form-bean name="accountStockForm" 
     type="com.unmi.form.AccountStockForm"/> 
  </form-beans> 
  <action-mappings> 
    <action path="/showStock" name="accountStockForm" 
     type="com.unmi.action.AccountStockAction" scope="request"> 
      <forward name="show" path="/show.jsp"/> 
    </action> 
  </action-mappings> 
</struts-config> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
     "-
Apache Software FoundationDTD Struts Configuration 1.3EN"
     "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans>
    <form-bean name="accountStockForm"
     type="com.unmi.form.AccountStockForm"/>
  </form-beans>
  <action-mappings>
    <action path="/showStock" name="accountStockForm"
     type="com.unmi.action.AccountStockAction" scope="request">
      <forward name="show" path="/show.jsp"/>
    </action>
  </action-mappings>
</struts-config>
  二: 输入页面 input.jsp, 注意表单域命名
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 <html:form action="/showStock"> 
  <h3>记录持有的股票<br></h3> 
  <fieldset>s<legend>基本信息</legend> 
  <table width="100%" border=0><tr> 
    <td>帐户名:<html:text property="account.name"/></td> 
    <td>资金帐号:<html:text property="account.number"/></td> 
  </tr></table> 
  </fieldset> 
  <br> 
  <fieldset><legend>持有股票</legend> 
  <table width=100% border=0 id="stockTable"> 
  <tr> 
    <td><input type="checkbox" onclick="checkAll(this)"></td> 
    <td>股票代码</td> 
    <td>股票名称</td> 
    <td>成本价</td> 
    <td>股票数量</td> 
  </tr> 
  <tr> 
    <td><input type="checkbox" name="check"></td> 
    <td><input name="stocks[0].code" size="15"></td> 
    <td><input name="stocks[0].name" size="15"></td> 
    <td><input name="stocks[0].price" size="15"></td> 
    <td><input name="stocks[0].quantity" size="15"></td> 
  </tr> 
  </table> 
</html:form> 
  <html:form action="/showStock">
    <h3>记录持有的股票<br></h3>
    <fieldset>s<legend>基本信息</legend>
    <table width="100%" border=0><tr>
      <td>帐户名:<html:text property="account.name"/></td>
      <td>资金帐号:<html:text property="account.number"/></td>
    </tr></table>
    </fieldset>
    <br>
    <fieldset><legend>持有股票</legend>
    <table width=100% border=0 id="stockTable">
    <tr>
      <td><input type="checkbox" onclick="checkAll(this)"></td>
      <td>股票代码</td>
      <td>股票名称</td>
      <td>成本价</td>
      <td>股票数量</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="check"></td>
      <td><input name="stocks[0].code" size="15"></td>
      <td><input name="stocks[0].name" size="15"></td>
      <td><input name="stocks[0].price" size="15"></td>
      <td><input name="stocks[0].quantity" size="15"></td>
    </tr>
    </table>
  </html:form>

  例如输入框名 account.name 提交后能设置到 accountStockForm 的account的name属性
  输入框名为 stocks[0].code 提交后会设置到 accountStockForm 的 List stocks的第一个元素的code属性.以此类推
  在提交表单前要重排行层的索引,从 0 起, 否则到后右的 Form 会一些空数据.
  三: AccountStockForm 的关键代码
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 private Account account = new Account(); 
private List stocks = new AutoArrayList(Stock.class); 
 
public void setStocks(List stocks) 
{ 
  this.stocks.clear(); 
  this.stocks.addAll(stocks); 
} 
  private Account account = new Account();
  private List stocks = new AutoArrayList(Stock.class);
  
  public void setStocks(List stocks)
  {
    this.stocks.clear();
    this.stocks.addAll(stocks);
  }
  定义了两个属性,分别是一个bean(Account,接受基本信息)和一个List(stocks,接受股票行信息),注意这两个属性必须初始化,不然在表单提交后会出现空指针错误. setStocks方法是让stocks属性永远保有持是一个 AutoArrayList 实例. 这样在表单提交后设置值是总能调用 AutoArrayList 的 get(int index) 方法.
  四: 自定义的 AutoArrayList
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 public class AutoArrayList extends ArrayList { 
   
  private Class itemClass; 
   
  public AutoArrayList(Class itemClass) { 
    this.itemClass = itemClass; 
  } 
   
  public Object get(int index) { 
    try { 
      while (index >= size()) { 
        add(itemClass.newInstance()); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return super.get(index); 
  } 
} 
public class AutoArrayList extends ArrayList {
  
  private Class itemClass;
  
  public AutoArrayList(Class itemClass) {
    this.itemClass = itemClass;
  }
  
  public Object get(int index) {
    try {
      while (index >= size()) {
        add(itemClass.newInstance());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return super.get(index);
  }
}
理解为什么要继承一个ArrayList, 覆写get(int index)方法要简单了解 Struts 处理提交数据的工作原理: 大致如下: 页面提交后, 由 ActionServlet交给RequestProcessor的processPopulate()方法,由processPopulate()方法收集请求数据,放在map中,key为表单域的name属性,如 name, account.name, stocks[0].code. 然后借助于 Common-beanutils 工具包设置到 ActionForm 的相应属性中
  如果key是简单的'name',直接form.setName(map.get('name'));
  如果key是'account.name', 执行的操作是 form.getAccount().setName(map.get('account.name');
  如果key是'stocks[0].code', 它可以对应到数据或集合中,如对于数组 form.stocks[0].code=map.get('stocks[0].code'); 对于集合(List) form.get(0).setCode(map.get('stocks[0].code'))
  从上也能理解为什么 form 中的那两个属性必须实始化,不然会出现空指针错. 而且为什么 stocks 要用 AutoArrayList 实例化, 避免出现索引越界的错误.
  五: 在 AccountStockAction 中可以打印出提交的数据
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 AccountStockForm asForm = (AccountStockForm)form; 
 
Account account = asForm.getAccount(); 
System.out.println("Account Name:"+account.getName()+ 
    " Number:"+account.getNumber()); 
 
List stocks = asForm.getStocks(); 
for (int i=0; i<stocks.size() ;i++) 
{ 
  Stock stock = (Stock)stocks.get(i); 
  System.out.println("Stock["+i+"]Code:"+stock.getCode()+ 
      " Name:"+stock.getName()+ 
      " Price:"+stock.getPrice()+ 
      " Quantity:"+stock.getQuantity()); 
} 
 
return mapping.findForward("show"); 
    AccountStockForm asForm = (AccountStockForm)form;
    
    Account account = asForm.getAccount();
    System.out.println("Account Name:"+account.getName()+
        " Number:"+account.getNumber());
    
    List stocks = asForm.getStocks();
    for (int i=0; i<stocks.size() ;i++)
    {
      Stock stock = (Stock)stocks.get(i);
      System.out.println("Stock["+i+"]Code:"+stock.getCode()+
          " Name:"+stock.getName()+
          " Price:"+stock.getPrice()+
          " Quantity:"+stock.getQuantity());
    }
    
    return mapping.findForward("show");

  在Action中就能直接取用提交来的数据了,不需要 getParameterValues()了.
  六: 最后一步, 对于这样的 ActionForm 我们应该如何显示出来呢,我们用了 nested 标签 (show.jsp)
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 <html:form action="/showStock"> 
  <h3>修改持有的股票<br></h3> 
  <fieldset><legend>基本信息</legend> 
  <table width="100%" border=0><tr> 
  <nested:nest property="account"> 
    <td>帐户名:<nested:text property="name" readonly="true"/></td> 
    <td>资金帐号:<nested:text property="number" readonly="true"/></td> 
  </nested:nest> 
  </tr></table> 
  </fieldset> 
  <br> 
  <fieldset><legend>持有股票</legend> 
  <table width=100% border=0 id="stockTable"> 
  <tr> 
    <td><input type="checkbox" onclick="checkAll(this)"></td> 
    <td>股票代码</td> 
    <td>股票名称</td> 
    <td>成本价</td> 
    <td>股票数量</td> 
  </tr> 
  <nested:iterate id="stock" property="stocks"> 
  <tr> 
    <td><input type="checkbox" name="check"></td> 
    <td><nested:text property="code" size="15"/></td> 
    <td><nested:text property="name" size="15"/></td> 
    <td><nested:text property="price" size="15"/></td> 
    <td><nested:text property="quantity" size="15"/></td> 
  </tr> 
  </nested:iterate> 
  </table> 
</html:form> 
  <html:form action="/showStock">
    <h3>修改持有的股票<br></h3>
    <fieldset><legend>基本信息</legend>
    <table width="100%" border=0><tr>
    <nested:nest property="account">
      <td>帐户名:<nested:text property="name" readonly="true"/></td>
      <td>资金帐号:<nested:text property="number" readonly="true"/></td>
    </nested:nest>
    </tr></table>
    </fieldset>
    <br>
    <fieldset><legend>持有股票</legend>
    <table width=100% border=0 id="stockTable">
    <tr>
      <td><input type="checkbox" onclick="checkAll(this)"></td>
      <td>股票代码</td>
      <td>股票名称</td>
      <td>成本价</td>
      <td>股票数量</td>
    </tr>
    <nested:iterate id="stock" property="stocks">
    <tr>
      <td><input type="checkbox" name="check"></td>
      <td><nested:text property="code" size="15"/></td>
      <td><nested:text property="name" size="15"/></td>
      <td><nested:text property="price" size="15"/></td>
      <td><nested:text property="quantity" size="15"/></td>
    </tr>
    </nested:iterate>
    </table>
  </html:form>
  可以查看生成的HTML源文件, 你就能更好理解 input.jsp 中的表单域为什么要那么命名了.
  小结的内容是请注意以下几个重点:
  1. 输入信息的页面 input.jsp 没有使用 Struts 标签,目的是让大家理解,表单域应如何命名才能对应上 ActionForm 中的哪一个属性
  2. 显示数据的页面是用的 Struts 标签,并留意 nested 标签的应用. 可以从生成的 HTML 源文件中体会出什么
  3. 提交数据前要重新编排行层中输入框 Name 属性的下标植.
  4. 回味为什么要引入 ArrayList 的子类 AutoArrayList, 关键在 get(int index) 方法的覆写
  5. 最后是 ActionForm 中 List 属性 stocks 的 setter 方法的实现, 保持那个 List 的运行时具体类型不变


本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Java Struts 多行提交到Form的List元素的属性中

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment