最近,在看孙卫琴编的《精通STRUTS》,感觉还不错,写得还算详细,适合像我这样的新手,呵呵~~~
言归正传,以一个简单的HELLO WORLD 程序为例............
当提交ActionForm时,请求路径为*.do,Servlet容器在WEB.XML中查找<URL-PATTERN>属性为*.do的<servlet-mapping>,<servlet-mapping><servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern></servlet-mapping> ,再根据<servlet-name>属性action 来查找匹配的<servlet-name>元素,<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class></servlet>,指定ActionServlet根据用户请求的/helloworld.do,在struts-config.xml中查找path属性为/helloworld的<action>元素,<action attribute="hello" input="/error.jsp"name="helloForm"path="/helloworld" scope="request"
type="hello.helloAction validate="true"> <forward name="success" path="/success.jsp"></forward></action>
ActionServlet 根据name="helloForm"创建helloForm对象,把用户提交的表单自动填充helloForm对象,
再把helloForm对象保存到scope指定的范围。validate="true" 指明ActionServlet必须调用helloForm对象的
validate()方法,此方法返回ActionErrors对象,它包含一个ActionMessage对象,封装了错误信息。ActionServlet把返回的ActionErrors对象保存在request范围,再根据input="/error.jsp"指定到error.jsp错误页,在该页中用<html:errors/>标签把错误显示到页面上。若ActionErrors对象不包含任何ActionMessage对象,说明表单验证成功。ActionServlet查找helloAction实例是否存在,若不存在就创建一个,然后调用execute()方法。改方法返回ActionFoward对象,<forward name="success" path="/success.jsp">转到path 指定的页面。