知识大全 Struts 1.x中批量封装对象

Posted

篇首语:勇敢,事会成功;勤劳,幸福必来。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Struts 1.x中批量封装对象相关的知识,希望对你有一定的参考价值。

Struts 1.x中批量封装对象  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  批量发布产品信息 页面输出如下图所示

   图 发布产品

   图 查看产品

  具体实现

  首先创建代表产品的类 Product java 代码如下     package tipsAndTricks;

  import java sql Date;

  public class Product 

  private String name;

  private double price;

  private Date dateOfProduction;

  public Date getDateOfProduction() 

  return dateOfProduction;

  

  public void setDateOfProduction(Date dateOfProduction) 

  this dateOfProduction = dateOfProduction;

  

  public String getName() 

  return name;

  

  public void setName(String name) 

  this name = name;

  

  public double getPrice() 

  return price;

  

  public void setPrice( double price) 

  this price = price;

  

  

  package tipsAndTricks;

  import java sql Date;

  public class Product 

  private String name;

  private double price;

  private Date dateOfProduction;

  public Date getDateOfProduction() 

  return dateOfProduction;

  

  public void setDateOfProduction(Date dateOfProduction) 

  this dateOfProduction = dateOfProduction;

  

  public String getName() 

  return name;

  

  public void setName(String name) 

  this name = name;

  

  public double getPrice() 

  return price;

  

  public void setPrice( double price) 

  this price = price;

  

  

  本例子中的dateOfProduction属性使用了java sql Date 而不是java util Date 这是因为Struts x不支持请求参数到java util Date的转换 归根到底是由于mons beanutils nvert()不支持关于java util Date的转换 另外 值得注意的是mon beanutils是通过java sql Date valueOf()方法工作的 所以在页面输入的字符串的格式必须为 yyyy MM dd

  实现上述功能大概有三种方法 下面我会分别对两种方法进行详细的讲述

  方法一 动态表单(Dynamic Actoin Form)+ 数组    首先 让我们来看一下Struts的配置文件WEB INF/struts config xml 内容如下

  view plaincopy to clipboardprint?    <? xml version= encoding= UTF ?>

  <! DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >

  < struts config >

  < data sources />

  < form beans >

  < form bean name = dynaProductsForm

  type = apache struts action DynaActionForm >

  < form property name = products

  type = tipsAndTricks Product[] size = />

  </ form bean >

  </ form beans >

  < global exceptions />

  < global forwards />

  < action mappings >

  < action attribute = dynaProductsForm input = /addProducts jsp

  name = dynaProductsForm path = /batchWrappingWithArray

  scope = request type = tipsAndTricks BatchWrappingWithArrayAction

  validate = false >

  < forward name = success path = /viewProducts jsp />

  </ action >

  </ action mappings >

  < message resources parameter = tipsAndTricks ApplicationResources />

  </ struts config >

  <? xml version= encoding= UTF ?>

  <! DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >

  < struts config >

  < data sources />

  < form beans >

  < form bean name = dynaProductsForm

  type = apache struts action DynaActionForm >

  < form property name = products

  type = tipsAndTricks Product[] size = />

  </ form bean >

  </ form beans >

  < global exceptions />

  < global forwards />

  < action mappings >

  < action attribute = dynaProductsForm input = /addProducts jsp

  name = dynaProductsForm path = /batchWrappingWithArray

  scope = request type = tipsAndTricks BatchWrappingWithArrayAction

  validate = false >

  < forward name = success path = /viewProducts jsp />

  </ action >

  </ action mappings >

  < message resources parameter = tipsAndTricks ApplicationResources />

  </ struts config > 我想这些配置应该用不着怎么解释了 有Struts x验证的朋友对此都不会陌生 因此 接下来创建/addProducts jsp文件 代码如下

  view plaincopy to clipboardprint?    <% @ page language = java pageEncoding = utf %>

  <% @ taglib uri = prefix = %>

  <% @ taglib uri = prefix = c %>

  <! DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >

  < >

  < head >

  < title > Add Products </ title >

  </ head >

  < body >

  < :form action = /batchWrappingWithArray method = post >

  < table border = >

  < tr style = background color:powderblue; font weight:bold; >

  < td > Product Name </ td >

  < td > Price </ td >

  < td > Date of production </ td >

  </ tr >

  < c:forEach var = products items = $dynaProductsForm map products >

  < tr >

  < td >< :text indexed = true name = products property = name /></ td >

  < td >< :text indexed = true name = products property = price /></ td >

  < td >< :text indexed = true name = products property = dateOfProduction /></ td >

  </ tr >

  </ c:forEach >

  < tr >

  < td colspan = >< :submit /></ td >

  </ tr >

  </ table >

  </ :form >

  </ body >

  </ >

  <% @ page language = java pageEncoding = utf %>

  <% @ taglib uri = prefix = %>

  <% @ taglib uri = prefix = c %>

  <! DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >

  < >

  < head >

  < title > Add Products </ title >

  </ head >

  < body >

  < :form action = /batchWrappingWithArray method = post >

  < table border = >

  < tr style = background color:powderblue; font weight:bold; >

  < td > Product Name </ td >

  < td > Price </ td >

  < td > Date of production </ td >

  </ tr >

  < c:forEach var = products items = $dynaProductsForm map products >

  < tr >

  < td >< :text indexed = true name = products property = name /></ td >

  < td >< :text indexed = true name = products property = price /></ td >

  < td >< :text indexed = true name = products property = dateOfProduction /></ td >

  </ tr >

  </ c:forEach >

  < tr >

  < td colspan = >< :submit /></ td >

  </ tr >

  </ table >

  </ :form >

  </ body >

  </ > 例中 我使用了JSTL 如果大家还没有尝试过使用JSP 的JSTL和EL 建议大家去看看相关文章 上面的<c:forEach />的作用是到dynaProductsForm的map属性中取出products数组 并对其进行遍历 再依靠<:text />标志将products的元素的属性以输入框的形式输出 <:text />标志的属性indexed= true 则表示在输出HTML时 将<input>的命名为类似products[ ] name的名字

  然后 再创建/viewProducts jsp页面 内容如下

  view plaincopy to clipboardprint?    <% @ page language = java pageEncoding = utf %>

  <% @ taglib uri = prefix = c %>

  <! DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >

  < >

  < head >

  < title > View Products </ title >

  </ head >

  < body >

  < table border = >

  < tr style = background color:powderblue; font weight:bold; >

  < td > Product Name </ td >

  < td > Price </ td >

  < td > Date of production </ td >

  </ tr >

  < c:forEach var = product items = $products >

  < tr >

  < td > $product name </ td >

  < td > $product price </ td >

  < td > $product dateOfProduction </ td >

  </ tr >

  </ c:forEach >

  </ table >

  </ body >

  </ >

  <% @ page language = java pageEncoding = utf %>

  <% @ taglib uri = prefix = c %>

  <! DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >

  < >

  < head >

  < title > View Products </ title >

  </ head >

  < body >

  < table border = >

  < tr style = background color:powderblue; font weight:bold; >

  < td > Product Name </ td >

  < td > Price </ td >

  < td > Date of production </ td >

  </ tr >

  < c:forEach var = product items = $products >

  < tr >

  < td > $product name </ td >

  < td > $product price </ td >

  < td > $product dateOfProduction </ td >

  </ tr >

  </ c:forEach >

  </ table >

  </ body >

  </ > 我想这份也不多作说明 不过大家可以通过上述代码看出使用JSTL + EL的确比Struts x的logic + bean要方便和简洁 不仅如此 EL还支持一定的运算符和函数操作

  最后是建立Action文件 BatchWrappingWithArrayAction java 代码如下

  view plaincopy to clipboardprint?    package tipsAndTricks;

  import javax servlet HttpServletRequest;

  import javax servlet HttpServletResponse;

  import apache struts action Action;

  import apache struts action ActionForm;

  import apache struts action ActionForward;

  import apache struts action ActionMapping;

  import apache struts action DynaActionForm;

  public class BatchWrappingWithArrayAction extends Action 

  public ActionForward execute(ActionMapping mapping ActionForm form

  HttpServletRequest request HttpServletResponse response) 

  DynaActionForm dynaProductsForm = (DynaActionForm) form;

  request setAttribute( products dynaProductsForm get( products ));

  return mapping findForward( success );

  

  

  package tipsAndTricks;

  import javax servlet HttpServletRequest;

  import javax servlet HttpServletResponse;

  import apache struts action Action;

  import apache struts action ActionForm;

  import apache struts action ActionForward;

  import apache struts action ActionMapping;

  import apache struts action DynaActionForm;

  public class BatchWrappingWithArrayAction extends Action 

  public ActionForward execute(ActionMapping mapping ActionForm form

  HttpServletRequest request HttpServletResponse response) 

  DynaActionForm dynaProductsForm = (DynaActionForm) form;

  request setAttribute( products dynaProductsForm get( products ));

  return mapping findForward( success );

  

   此Action将动态表单传过来的products数组放到request中 转到/viewProducts jsp 发布运行应用程序 在浏览器的地址栏中输入 效果请参考如图 图

  在/addProducts jsp的 Date of production 必须以(yyyy MM dd)的形式正确填写 且不能为空

  方法二 表单(Actoin Form)+ 列表(List)    方法一虽然简单 但是有一个明显的缺点——数组的长度已经固定 故我们不能在运行时通过程序设置对象数量 下面将要介绍的方法可以很好地解决这个问题

  首先 我们要创建类 AutoInitArrayList java 代码如下

  view plaincopy to clipboardprint?    package tipsAndTricks;

  import java util ArrayList;

  public class AutoInitArrayList < T > extends ArrayList < T > 

  private static final long serialVersionUID = L ;

  private Class < T > t = null ;

  public AutoInitArrayList(Class < T > t) 

  this t = t;

  

  @Override

  public T get( int index) 

  try 

  while (index >= size()) 

  add(t newInstance());

  

   catch (Exception e) 

  e printStackTrace();

  

  return super get(index);

  

  

  package tipsAndTricks;

  import java util ArrayList;

  public class AutoInitArrayList < T > extends ArrayList < T > 

  private static final long serialVersionUID = L ;

  private Class < T > t = null ;

  public AutoInitArrayList(Class < T > t) 

  this t = t;

  

  @Override

  public T get( int index) 

  try 

  while (index >= size()) 

  add(t newInstance());

  

   catch (Exception e) 

  e printStackTrace();

  

  return super get(index);

  

   AutoInitArrayList继承ArrayList并重载get()方法 作用就是在Struts x框架调用这个方法时 如果index超出列表大小 则会实例化新项放到列表中 避免出现(IndexOutOfBoundsException)异常

  接着 让我们看Struts的配置 内容如下

  view plaincopy to clipboardprint?    <? xml version= encoding= UTF ?>

  <! DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >

  < struts config >

  < data sources />

  < form beans >

  < form bean name = dynaProductsForm

  type = apache struts action DynaActionForm >

  < form property name = products

  type = tipsAndTricks Product[] size = />

  </ form bean >

  < form bean name = normalProductsForm

  type = tipsAndTricks NormalProductsForm />

  </ form beans >

  < global exceptions />

  < global forwards />

  < action mappings >

  < action attribute = dynaProductsForm input = /addProducts jsp

  name = dynaProductsForm path = /batchWrappingWithArray

  scope = request type = tipsAndTricks BatchWrappingWithArrayAction

  validate = false >

  < forward name = success path = /viewProducts jsp />

  </ action >

  < action attribute = normalProductsForm input = /addProducts jsp

  name = normalProductsForm path = /batchWrappingNormal scope = request

  type = tipsAndTricks BatchWrappingNormalAction validate = false >

  < forward name = success path = /viewProducts jsp />

  </ action >

  </ action mappings >

  < message resources parameter = tipsAndTricks ApplicationResources />

  </ struts config >

  <? xml version= encoding= UTF ?>

  <! DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >

  < struts config >

  < data sources />

  < form beans >

  < form bean name = dynaProductsForm

  type = apache struts action DynaActionForm >

  < form property name = products

  type = tipsAndTricks Product[] size = />

  </ form bean >

  < form bean name = normalProductsForm

  type = tipsAndTricks NormalProductsForm />

  </ form beans >

  < global exceptions />

  < global forwards />

  < action mappings >

  < action attribute = dynaProductsForm input = /addProducts jsp

  name = dynaProductsForm path = /batchWrappingWithArray

  scope = request type = tipsAndTricks BatchWrappingWithArrayAction

  validate = false >

  < forward name = success path = /viewProducts jsp />

  </ action >

  < action attribute = normalProductsForm input = /addProducts jsp

  name = normalProductsForm path = /batchWrappingNormal scope = request

  type = tipsAndTricks BatchWrappingNormalAction validate = false >

  < forward name = success path = /viewProducts jsp />

  </ action >

  </ action mappings >

  < message resources parameter = tipsAndTricks ApplicationResources />

  </ struts config > 然后 创建表单类 NormalProductsForm java 代码如下

  view plaincopy to clipboardprint?    package tipsAndTricks;

  import java util List;

  import apache struts action ActionForm;

  public class NormalProductsForm extends ActionForm 

  private List products = new AutoInitArrayList < Product > (Product class );

  public List getProducts() 

  return products;

  

  public void setProducts(List products) 

  this products = products;

  

  package tipsAndTricks;

  import java util List;

  import apache struts action ActionForm;

  public class NormalProductsForm extends ActionForm 

  private List products = new AutoInitArrayList < Product > (Product class );

  public List getProducts() 

  return products;

  

  public void setProducts(List products) 

  this products = products;

  

   接下来是Action类 BatchWrappingNormalAction java 代码如下

  view plaincopy to clipboardprint?      /**/ /*

  * Generated by MyEclipse Struts

  * Template path: templates/java/JavaClass vtl

  */

  package tipsAndTricks;

  import javax servlet HttpServletRequest;

  import javax servlet HttpServletResponse;

  import apache struts action Action;

  import apache struts action ActionForm;

  import apache struts action ActionForward;

  import apache struts action ActionMapping;

  public class BatchWrappingNormalAction extends Action 

  public ActionForward execute(ActionMapping mapping ActionForm form

  HttpServletRequest request HttpServletResponse response) 

  NormalProductsForm normalProductsForm = (NormalProductsForm) form;

  request setAttribute( products normalProductsForm getProducts());

  return mapping findForward( success );

  

  

  /**/ /*

  * Generated by MyEclipse Struts

  * Template path: templates/java/JavaClass vtl

  */

  package tipsAndTricks;

  import javax servlet HttpServletRequest;

  import javax servlet HttpServletResponse;

  import apache struts action Action;

  import apache struts action ActionForm;

  import apache struts action ActionForward;

  import apache struts action ActionMapping;

  public class BatchWrappingNormalAction extends Action 

  public ActionForward execute(ActionMapping mapping ActionForm form

  HttpServletRequest request HttpServletResponse response) 

  NormalProductsForm normalProductsForm = (NormalProductsForm) form;

  request setAttribute( products normalProductsForm getProducts());

  return mapping findForward( success );

  

   基本上与方法一的Action一样 下面 再看看新的输入文件/addProducts jsp 内容如下

  view plaincopy to clipboardprint?    <% @ page language = java pageEncoding = utf %>

  <% @ taglib uri = prefix = %>

  <% @ taglib uri = prefix = c %>

  <! DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >

  < >

  < head >

  < title > Add Products </ title >

  </ head >

  < body >

  < :form action = /batchWrappingNormal method = post >

  < table border = >

  < tr style = background color:powderblue; font weight:bold; >

  < td > Product Name </ td >

  < td > Price </ td >

  < td > Date of production </ td >

  </ tr >

  < c:forEach begin = end = var = i >

  < tr >

  < td >< input name = products[$i] name /></ td >

  < td >< input name = products[$i] price /></ td >

  < td >< input name = products[$i] dateOfProduction /></ td >

  </ tr >

  </ c:forEach >

  < tr >

  < td colspan = >< :submit /></ td >

  </ tr >

  </ table >

  </ :form >

  </ body >

  </ >

  <% @ page language = java pageEncoding = utf %>

  <% @ taglib uri = prefix = %>

  <% @ taglib uri = prefix = c %>

  <! DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN >

  < >

  < head >

  < title > Add Products </ title >

  </ head >

  < body >

  < :form action = /batchWrappingNormal method = post >

  < table border = >

  < tr style = background color:powderblue; font weight:bold; >

  < td > Product Name </ td >

  < td > Price </ td >

  < td > Date of production </ td >

  </ tr >

  < c:forEach begin = end = var = i >

  < tr >

  < td >< input name = products[$i] name /></ td >

  < td >< input name = products[$i] price /></ td >

  < td >< input name = products[$i] dateOfProduction /></ td >

  </ tr >

  </ c:forEach >

  < tr >

  < td colspan = >< :submit /></ td >

  </ tr >

  </ table >

  </ :form >

  </ body >

cha138/Article/program/Java/ky/201311/28917

相关参考

知识大全 在Java2环境中应用IP地址封装对象

在Java2环境中应用IP地址封装对象  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在Java

知识大全 Struts架构中的Session对象创建和控制

Struts架构中的Session对象创建和控制  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 面向对象三大特性之封装

  java学习已经到了最核心的章节面向对象的三大特性封装继承和多态接下来会写多篇博文(数目不定请关注近期博客吧!午饭们!)来总结这里面的每一个细节小可很希望每一篇文章的阐述会给你带来一点启发小可也是

知识大全 批量实现面向对象的实例代码

本文为大家详细介绍下面向对象的继承以及如何实现批量实现面向对象感兴趣的可以参考下哈希望对大家有所帮助 复制代码代码如下:cha138/Article/program/Java/JS

知识大全 多线程的批量线程同步解决方案

  多线程运行时有待处理线程?试试看下面介绍的这个批量线程同步方法吧  在一批线程处理程序中有时必须等到所有线程全部运行完后才能进行下一步任务处理可以采用如下方法解决创建一个锁对象该锁对象提供一个当前

知识大全 struts2的action中获得request response sessi

  在struts中有两种方式可以得到这些对象  非IoC方式  要获得上述对象关键Struts中opensymphonyxworkActionContext类我们可以通过它的静态方法getConte

知识大全 使用Struts+Hibernate上传大对象(BLOB)

使用Struts+Hibernate上传大对象(BLOB)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一

知识大全 struts2迭代不同对象的测试

  在平时使用s:iterator这个迭代标签一般我们都是把一个相同类型的多个对象放入一个集合当中那么是否可以放入不相同的对象来迭代呢?日常工作有时候可能是需要这样来简化程序的我今天测试了一下好像完全

知识大全 从内部类继承

  由于内部类构建器必须同封装类对象的一个句柄联系到一起所以从一个内部类继承的时候情况会稍微变得有些复杂这儿的问题是封装类的秘密句柄必须获得初始化而且在衍生类中不再有一个默认的对象可以连接解决这个问题

知识大全 struts标签使用举例--logic篇

 logic:empty  该标签是用来判断是否为空的如果为空该标签体中嵌入的内容就会被处理该标签用于以下情况  )当Java对象为null时  )当String对象为时  )当javaut