java struts

使用ActionForm

3.1 使用ActionForm

ActionForm Bean有两种存在范围:request和session。如果ActionForm存在于request范围,它仅在当前的请求/响应生命周期中有效。在请求从一个Web组件转发到另一个Web组件的过程中,ActionForm实例一直有效。当服务器把响应结果返回给客户,ActionForm实例及其包含的数据就会被销毁。如果ActionForm存在于session范围,同一个ActionForm实例在整个HTTP会话中有效。

当控制器接受到请求时,如果请求访问的Web组件为Action,并且为这个Action配置了和ActionForm的映射,控制器将从request或 session范围中取出ActionForm实例,如果该实例不存在,就会自动创建一个新的实例。当控制器接受到一个新的请求时,ActionForm 的生命周期如下:

· 控制器接收到请求

· 从request或session范围中取出ActionForm实例,如果该实例不存在,就自动创建一个新的实例。

· 调用ActionForm的reset()方法

· 把ActionForm实例保存在request或session范围中

· 把用户输入的表单数据组装到ActionForm中

· 如果<action>的validate属性为true,则调用ActionForm的validate()方法。

· (1) 如果存在验证错误,把请求转发给<action>的input属性指定的Web组件,ActionForm实例依然保持在request或session范围内。


(2) 如果无验证错误,调用Action的execute()方法,把ActionForm实例传递给execute()方法。

· 把请求转发给其他Web组件,ActionForm实例依然保存在request或session范围内。


3.2 创建ActionForm

Struts框架中定义的ActionForm类时抽象的,必需在应用中创建它的子类,来捕获具体的HTML表单数据,ActionForm Bean中的属性和HTML表单中的字段一一对应。


1 validate()方法

如果Struts的配置文件满足以下两个条件,Struts控制器就会调用ActionForm的validate()方法:

· 为ActionForm配置了Action映射,即<form-bean>元素的name属性和<action>元素的name属性匹配。

· <action>元素的validate属性为true。

在ActionForm基类中定义的validate()方法直接返回null,如果创建了扩展ActionForm基类的子类,那么应该在子类中覆盖validate()方法。

validate()方法主要负责检查数据的格式和语法,而不负责检查数据是否符合业务逻辑。


2 reset()方法

不管ActionFormj存在于哪个范围内,对于每个请求,控制器都会先调用ActionForm的reset()方法,然后再把用户输入的表单数据组装到ActionForm中。reset()方法用于恢复ActionForm的属性的默认值,例如把boolean类型属性设为true或false,把字符串属性设为null或某个初始值。

如果ActionForm在request范围内,那么对于每个新的请求都会创建新的ActionForm实例。当新的实例创建后,如果它的属性已经被初始化为默认值,那么接着再在reset()方法中把属性设为默认值不是很有必要,因此在这种情况下,可以让reset()方法为空。

对于session范围内的ActionForm,同一ActionForm实例会被多个请求共享,reset()方法在这种情况下极为有用。


3.3 配置ActionForm

Struts配置文件的<form-beans>元素用来配置所有的ActionForm Bean。<form-beans>元素可以包含多个<form-bean>子元素,它代表单个的ActionForm Bean。

同一个ActionForm可以和多个Action映射。在<action>元素中,name和scope属性分别指定ActionForm的名字和范围,validate属性指定是否执行表单验证。


3.4 访问ActionForm

ActionForm可以被JSP、Struts标签、Action和其他Web组件访问。访问ActionForm大致有以下一些方法:

1 使用Struts HTML标签库

Struts HTML标签库提供了一组和ActionForm密切关联的标签,<html:form>标签生成HTML表单,它包括<html:text>、<html:select>、<html:option>、<html:radio> 和<html:submit>等子标签,这些子标签构成HTML表单的字段或按钮。<html:form>标签能和 ActionForm交互,读取ActionForm的属性值,把他们赋值给表单中对应的字段。

2 从request或session范围内取出ActionForm实例

Struts框架把ActionForm实例保存在HttpServletRequest或HttpSession中,保存时采用的属性key 为<form-bean>元素的name属性。因此,如果ActionForm在request范围内,则可以调用 HttpServletRequest的getAttribute()方法读取ActionForm实例。如果ActionForm在session范围内,则可以调用HttpSession的getAttribute()方法读取ActionForm实例。

3 在Action类的execute()方法中直接访问ActionForm

如果配置了ActionForm的Action的映射,Struts框架就会把ActionForm作为参数传递给Action的execute()方法,因此在Action类的execute()方法中可以读取或设置ActionForm属性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts Logic标签库中包含的标签列表 Tag name Description empty 如果标签parameter,propertie等属性所指定的变量值为null或空字符串,则处理标签包含的内容 equal 如果标签parameter,propertie等属性所指定的变量的值等于标签value属性所指定的值,则处理标签所包含的内容, 如: <logic:equal value="modify" property="action" name="projectForm"> <bean:message key="project.project_modify"/> </logic:equal> 上面的示例表示,如果projectFormaction属性等于modify,则处理<bean:message key="project.project_modify"/ >语句。 forward Forward control to the page specified by the ActionForward entry. greaterEqual Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. greaterThan Evaluate the nested body content of this tag if the requested variable is greater than the specified value. iterate Repeat the nested body content of this tag over a specified collection. lessEqual Evaluate the nested body content of this tag if the requested variable is less than or equal to the specified value. lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value. match Evaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable. messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request. messagesPresent Generate the nested body content of this tag if the specified message is present in this request. notEmpty Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string. notEqual Evaluate the nested body content of this tag if the requested variable is not equal to the specified value.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值