`
longgangbai
  • 浏览: 7254344 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

在Struts2.0中批量操作(update Or Add)的實現和注意點

阅读更多

   在項目中,需要針對一批数据进行或添加或着修改操作。使用Struts2.0 实现。本人仅使用Web

使用是防止对个对象修改添加时,可能出项空造成部分数据不对应所以进行,进行特殊处理。

 

(1)首先在 Action 代码中定义了 List 类型变量。并且运用 Java5 中的泛型来表明该 List 集合类型变量中的元素都是 ProductItemPrice对象。当然,也可以在 Action 代码中不使用泛型来标明是何种对象作为 List 集合类型的元素。另一种方法就是示例中写明的在属性文件中指定 List 集合类型的元素是何种对象。

注意:该属性文件属于局部类型转换属性定义文件。文件名要以 ActionName 打头,然后以“ --conversion.properties ”结尾。这表示是对该 Action 中的 List 集合类型指定元素类型。而且该属性文件一定要和 Action 放在同一目录下。否则运行系统时候 Struts2 是不会知道该 Action List 集合类型变量元素是何种类型对象。

2 )试设想有一种情况,多个 Action 都需要将某一变量的类型进行转换。此时可以像之前所述使用属性文件来定义被转换类型的变量。但是 1 Action 定义 1 个属性文件则太浪费时间。因此 Struts2 中还有个全局类型转换属性定义文件,这样所有需要类型转换的 Action 都可以调用该文件中定义的需要类型转换的变量。全局属性文件名字必须为“ xwork-conversiion.properties ”。文件中定义的内容其实和局部属性文件中大同小异,只是这些被定义的变量可以在所有 Action 中进行类型转换。全局属性文件没必要和具体 Action 代码文件放在一起,只需要放在源代码根目录下即可。也就是说全局属性文件直接放在“ src ”文件夹下即可。

3 )在页面输入的 JSP 中,千万不能将 List 集合类型的变量名写错,否则 Action 是得不到具体在页面上输入的值。而且因为是 List 类型,所以可以利用 OGNL 来循环遍历。这样在页面上可依次输入数据。数据显示的 JSP 页面上没有什么特别需要注意的,读者可以看到只是利用 Struts2 的标签来显示这些数据。

4 )读者可由图 7.3 、图 7.4 看出,批量的 Material 对象数据输入其实和单个 Material 对象输入本质上没有多大区别。在视图界面上只是利用 OGNL Struts2 标签来保证数据可以输入和显示。在 Action 这一层和普通的 Struts2 Action 导航没有多大区别。最重要的是全局和局部类型转换属性文件的定义。让系统明白集合类型中包含的元素是何种对象。不过也请读者不要误解,以为 Set 集合类型转换也是如此。接下来笔者将讲解 Set 集合类型数据类型转换的注意点。

 

  /**
 * 可选项产品价格的管理
 *
 * @author longgangbai
 *
 */
@SuppressWarnings("serial")
public class ProductPriceAction extends
  BaseAction<ProductItemPrice, ProductItemPriceService>

以上struts2的action采用和SpringSide中设计思路一样:

 

简述如下:

ProductItemPrice:父类实体

ProductItemPriceService:实体对应的Service层

其中ProductItemPrice有两个子类:

/**
 * 可选项关联的价格项
 *
 * @author longgangbai
 *
 */
@Entity
@DiscriminatorValue("MultipleProductItemPrice")
@GenericGenerator(name = "seq_gen", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
  @Parameter(name = "sequence_name", value = "seq_MultipleProductItemPrice"),
  @Parameter(name = "initial_value", value = "100000") })
public class MultipleProductItemPrice extends ProductItemPrice

 

/**
 * 单选项关联的价格项信息
 *
 * @author longgangbai
 *
 */
@Entity
@DiscriminatorValue("SingleProductItemPrice")
@GenericGenerator(name = "seq_gen", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
  @Parameter(name = "sequence_name", value = "seq_SingleProductItemPrice"),
  @Parameter(name = "initial_value", value = "100000") })
public class SingleProductItemPrice extends ProductItemPrice

 

 

 

 

 

特殊处理的地方分为以下几点:

 

 /**
  * 查询可选项或单选项的价格的信息
  */
 private List<ProductItemPrice> productItemPrices;   //仅仅用于展示
 /**
  * 查询可项的价格的信息
  */
 @SuppressWarnings("unchecked")
 private List mproductItemPrices; //用于操作可选项操作MultipleProductItemPrice 

 /**
  * 查询多选项的价格的信息
  */
 @SuppressWarnings("unchecked")
 private List sproductItemPrices;//用于操作可选项操作SingleProductItemPrice

 

 

有点怪异的地方:

 

mproductItemPrices 存储MultipleProductItemPrice 的集合但是不可以泛型有点郁闷

 

sproductItemPrices存储SingleProductItemPrice 的集合但是不可以泛型有点郁闷

 

但是如果不存在父子关系可以使用泛型,郁闷吧?^_^

希望有高人指点!!

 

通过Struts2.0的IOC功能实现注入:

 

 

在Struts2.0的Action的局部属性文件内容配置如下:

Action局部配置文件的命名规则:

如下:

   Action的名称+“-”+conversion.properties

 

配置的属性:

   Element_集合的名称=集合对象中对应的对象类

Element_productItemPrices=com.unutrip.vacation.model.product.ProductItemPrice
Element_sproductItemPrices=com.unutrip.vacation.model.single.SingleProductItemPrice
Element_mproductItemPrices=com.unutrip.vacation.model.multiple.MultipleProductItemPrice

 

 

在展示是的技巧:

重点在针对标签的命名规则

 

  针对可选项集合:MultipleProductItemPrice

 

便利显示并支持修改如下:

 

   <s:iterator status="sta"  value="mproductItemPrices">
       <tr id="<s:property value='%{#sta.index+1}'/>">// 目的为动态生成对象做准备
      <td>
       <s:hidden name="%{'mproductItemPrices['+#sta.index+'].productId'}"/>
      <s:hidden name="%{'mproductItemPrices['+#sta.index+'].id'}"/>
                      <s:property value="%{#sta.index+1}"/>     
      </td> 
      <td>
        <s:textfield name="%{'mproductItemPrices['+#sta.index+'].startDate'}" size="6" />
      </td> 
      <td>
         <s:textfield name="%{'mproductItemPrices['+#sta.index+'].endDate'}" size="6" />
      </td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].adultPrice'}" size="6" /></td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].adultBasePrice'}" size="6" /></td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].childPrice'}" size="6" /></td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].childBasePrice'}" size="6" /></td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].babyPrice'}" size="6" /></td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].babyBasePrice'}" size="6" /></td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].adultOfferPrice'}" size="6" /></td> 
      <td><s:textfield name="%{'mproductItemPrices['+#sta.index+'].childOfferPrice'}" size="6" /></td> 
       </tr>
    </s:iterator>

 

由于项目支持动态添加所以:

本人经过思考,不得其解,则查看源代码是发现生成静态标签的规则如下:

 

  得到规律如下:

<input type='text' name='mproductItemPrices[0].startDate'  size='6'/>

 

 

备注其中红色的规律:集合的名称【下标】.属性

 

最后根据静态标签的规则实现需要的动态添加,并且组成的集合对象的目的

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    struts2.0中文教程

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新...

    Struts2.0视频教程+struts2.0中文教程

    Struts2.0视频教程,struts2.0中文教程,Struts2.0视频教程,struts2.0中文教程,

    struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0

    struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0

    Struts 2.0系列(MAX)

    在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD Struts 2中的OGNL Strus 2的新表单标志的使用 ...

    sstruts2.0 struts2.0

    sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0

    struts2.0jar包

    struts2.0jar包 struts2.0包 struts2.0源文件

    Struts2.0中文教程权威版

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新...

    struts2.0的数据校验框架struts2.0的数据校验框架

    struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架

    struts 2.0 详细配置

    struts 2.0 详细配置 struts 2.0 详细配置 struts 2.0 详细配置

    JavaEE源代码 Struts2.0

    JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0...

    Struts2.0 Jar包

    此为Struts2.0最新Jar包,方便各位用于Struts2.0的开发.

    Struts2.0中文教程

    Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程

    Struts 2.0

    清晰的介绍了Struts 2.0框架的工作流程,Action线程安全,程序入口,配置文件。

    struts2.0源代码

    struts2.0源代码(有关于struts2.0实现上传与下载和如何操作数据库的源代码),非常有用

    Struts2.0学习Struts2.0文档

    为Struts 2.0做好准备 Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点: MVC 2模型的使用 功能齐全的标志库(Tag Library) 开放...

    struts2.0的特点

    struts2.0的特点

    struts2.0入门案例

    struts2.0 入门案例、简单的struts2.0入门案例 2.0配置包,基础

Global site tag (gtag.js) - Google Analytics