首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

批量打包对象(Bean)

2012-10-09 
批量封装对象(Bean)不知道大家是否遇过这种情况,在一个页面里同时提交几个对象。例如,在发布产品的页面,同

批量封装对象(Bean)


不知道大家是否遇过这种情况,在一个页面里同时提交几个对象。例如,在发布产品的页面,同时发布几个产品。我在之前一个项目就遇到过这种需求,当时用的是Struts 1.x。那是一个痛苦的经历,我在Google搜了很久都没有理想的结果。幸运的是,在Struts 2.0中这种痛苦将一去不复返。下面我就演示一下如何实现这个需求。

首先,在源代码文件夹下的tutorial包中新建Product.java文件,内容如下:

package tutorial;

import java.util.Date;

publicclass Product {
   private String name;
   privatedouble price;
   private Date dateOfProduction;
  
   public Date getDateOfProduction() {
       return dateOfProduction;
   }
  
   publicvoid setDateOfProduction(Date dateOfProduction) {
       this.dateOfProduction = dateOfProduction;
   }
  
   public String getName() {
       return name;
   }
  
   publicvoid setName(String name) {
       this.name = name;
   }
  
   publicdouble getPrice() {
       return price;
   }
  
   publicvoid setPrice(double price) {
       this.price = price;
   }   
}

然后,在同上的包下添加ProductConfirm.java类,代码如下:

package tutorial;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

publicclass ProductConfirm extends ActionSupport {
   public List<Product> products;

   public List<Product> getProducts() {
       return products;
   }

   publicvoid setProducts(List<Product> products) {
       this.products = products;
   }
  
   @Override
   public String execute() {
       for(Product p : products) {
           System.out.println(p.getName() + " | "+ p.getPrice() +" | " + p.getDateOfProduction());
       }
       return SUCCESS;
   }
}

接看,在同上的包中加入ProductConfirm-conversion.properties,代码如下:

Element_products=tutorial.Product

再在struts.xml文件中配置ProductConfirm Action,代码片段如下:

<action name="ProductConfirm" uri="/struts-tags"%>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    <s:form action="ProductConfirm" theme="simple">           
        <table>
            <tr style="background-color:powderblue; font-weight:bold;">
                <td>Product Name</td>
                <td>Price</td>
                <td>Date of production</td>
            </tr>
            <s:iterator value="new int[3]" status="stat">
                <tr>
                    <td><s:textfield name="%{'products['+#stat.index+'].name'}"/></td>
                    <td><s:textfield name="%{'products['+#stat.index+'].price'}"/></td>
                    <td><s:textfield name="%{'products['+#stat.index+'].dateOfProduction'}"/></td>
                </tr>
            </s:iterator>
            <tr>
                <td colspan="3"><s:submit /></td>
            </tr>
        </table>
    </s:form>   
</body>
</html>

在同样的文件夹下创建ShowProducts.jsp,内容如下:

<%@ page  contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Hello World</title>
</head>
<body>   
    <table>
        <tr style="background-color:powderblue; font-weight:bold;">
            <td>Product Name</td>
            <td>Price</td>
            <td>Date of production</td>
        </tr>
        <s:iterator value="products" status="stat">
            <tr>
                <td><s:property value="name"/></td>
                <td>$<s:property value="price"/></td>
                <td><s:property value="dateOfProduction"/></td>
            </tr>
        </s:iterator>
    </table>
</body>
</html>

热点排行