`

Struts笔记(二)ActionForm传递数据

阅读更多
2. ActionForm传递数据

创建一个ActionForm 并不困难,但是你的类必须符合一些要求:
ActionForm 必须扩展自org.apache.struts.action.ActionForm。基类ActionForm 是不能实例化的。

ActionForm 必须为每个应该从请求中收集的HTML输入控件定义一个公共属性。

如果你要求ActionForm 在传递属性到Action之前校验它们,你就必须实现validate方法;

如果想在组装前初始化属性,必须实现 reset , 它在ActionForm 组装前被调用;

下面是一个简单的ActionForm 类:

public class MyForm extends ActionForm {
protected String name;
protected String address;
public String getName()
{return this.name;};
public String getAddress()
{return this.address;};
public void setName(String name)
{this.name = name;};
public void setAddress(String address)
{this.address = address;};


使用DynaActionForm直接在struts-config.xml配置。
<form-bean
name="myForm"
type="org.apache.struts.action.DynaActionForm">
<form-property
name="name"
type="java.lang.String"/>
<form-property
name="address"
type="java.lang.String"/>
</form-bean>


  • ActionForm 作为字段收集器
  • ActionForm 作为数据缓冲,以便在提交之前进行校验
  • ActionForm 作为数据校验器,。ActionForm的validate 方法是一个扩展点,你可以在此插入对
  • 业务方法(它们知道如何校验数据)的调用。当校验失败, ActionForm 可以将整个内容
    都回传给web 页面,这样用户就可以重新尝试。
  • ActionForm 作为类型转换器,在ActionForm中包含一些helper方法,来进行类型转换。helper 方法可以有很多种实现方式。
  • ActionForm 作为传输对象(TO),ActionForm 可以被其它bean或者过程作为数据载体。就是说ActionForm 是一个传输对象(Transfer Object)。HTTP 将每个属性表达为名-值对。
  • ActionForm 作为防火墙,它使你在数据被允许传递到应用的其它部分之前必须检查数据。


Struts 标签扩展有助于根据ActionForm组装HTML 控件。ActionServlet根据HTTP 请求组装ActionForm。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics