ActionForm是Jsp与Servlet(Action)之间传输数据的纽带,为了搞清楚ActionForm的使用情况,我做了一些试验。
ActionForm -- examples.simple.SimpleActionForm 我给SimpleForm的载入、构建和各getter\setter中都加入了打印信息的代码。 Action -- examples.SuccessAction 只实现了跳转控制功能 Action -- examples.simple.ProcessSimpleAction 处理simple.jsp页面提交,控制页面跳转 JSP -- index.jsp 欢迎页面,只有一个简单的连接指向/prepareSimple JSP -- simple.jsp 有填写的表格,form的提交指向/processSimple JSP -- simpleresult.jsp 显示提交的结果
struts-config.xml的定义 <struts-config> <form-beans> <form-bean name="simpleForm" type="examples.simple.SimpleActionForm" /> </form-beans> <action-mappings> <action path="/prepareSimple" type="examples.SuccessAction"> <forward name="success" path="/jsp/simple/Simple.jsp" /> </action> <action input="/jsp/simple/Simple.jsp" name="simpleForm" path="/processSimple" scope="request"
type="examples.simple.ProcessSimpleAction" validate="true"> <forward name="success" path="/jsp/simple/SimpleResults.jsp" /> </action> </action-mappings> </struts-config>
程序运行阶段 1、当点击index.jsp的/prepareSimple连接的时候,程序显示如下: 正在启动:SimpleActionForm 正在构造:SimpleActionForm() 正在:reset() 正在:getName() 正在:getSecret() 正在:getColor() 正在:getConfirm() 正在:getRating() 正在:getRating() 正在:getRating() 正在:getRating() 正在:getRating() 正在:getMessage() 可见struts判断了simple.jsp,明确了此页面需要的ActionForm Bean,于是就初试化bean,在产生simple.jsp页面的过程中,根据页面的需求
去取bean的属性(为什么去取一遍我有点想不通,好像是无用的,大概是检查一下吧)。 2、在提交simple.jsp之后,程序显示如下: 正在构造:SimpleActionForm() 正在:reset() 正在:setColor() 正在:setHidden() 正在:setMessage() 正在:setName() 正在:setSecret() 正在:validate() 在ProcessSimpleAction.execute() 正在:getName() 正在:getSecret() 正在:getColor() 正在:getConfirm() 正在:getConfirm() 正在:getRating() 正在:getHidden() 正在:getMessage() struts在收到提交请求后,首先根据XML的定义,生成了一个form bean,然后将各个值都(从request中)取出来,并附给bean。然后启动ProcessSimpleAction, ProcessSimpleAction完成工作之后,struts在构造simpleresult.jsp的时候,发现需要simpleForm,因此又会从form bean中取值(这次取值是有用的)。

|