鄙人一点对struts1.2中ActionForm的理解
初学struts的人我认为首先应该把struts1.2学精,何为精,就是明白struts的控制流程以及它核心API的实现原理(比如 Action/DispatchAction/MappingDispatchAction/LookupDispatchAction /ActionForm的实现原理)然后自己会写一个“山寨版”的struts1.2,只有到达这样的程度你才能深刻体会struts1.2的MVC,也 才证明你学会了struts1.2,当你再学习struts2.0的时候会非常的容易,那么玩转struts1.2的关键就是能够玩转 ActionForm,在此我把自己学习struts1.2时ActionForm的使用及其实现原理贴出来晒一晒,还望各位不要见笑,给些意见。
ActionForm的应用
(一)、ActionForm的特性
1.创建一个form类必须继承于四个父类中的一个,要么继承ActionForm,要么继承ValidatorForm,要么继承ValidatorActionForm,要么继承
2.一个form类中的每一个属性都将和页面中form表单中的每一个表单元素一一对应
Example:
一个表单为:
<form>
<input type="text" name="username"></input>
<input type="password" name="password"></input>
<input type="text" name="email"></input>
</form>
一个与之对应的form类
public class UserForm extends ActionForm{
private String username;
private String password;
private String email;
private String address;
//下面省略getter和setter方法
}
一个引用了该form类的appAction:
<form-beans>
<form-bean name="userForm" type="form.UserForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/test" type="action.TestAction" name="userForm" attribute="uf" scope="request"></action>
</action-mappings>
3.在引用了form-bean的action中name属性的值就是form-bean中name的值
4.这个userForm默认会被放在session中,使用scope可以指定存储该form对象的地方,自然attribute就是用来给存放在该范围中的form对象起的别名,如果不设置attribute
属性那么attribute默认的就使用name的值
(二)、普通HTML表单使用Form的工作原理
ActionServlet 对