一、Struts框架组件:
ActionServlet控制器
Action 包含事务逻辑
ActionForm 显示模块数据
ActionMapping 帮助控制器将请求映射到操作
ActionForward 用来指示操作转移的对象
ActionError 用来存储和回收错误
Struts Taglib 可以减轻开发显示层次的工作
二、进去看流程(可“配置”的页面流转)
仍以登录为例,程序包含以下主要文件:
web.xml,struts-config.xml,ActionForm.java,Action.java,jsp页面。
运行流程:
(1)开启服务器
(2)加载web.xml到内存
(3)根据web.xml加载ActionServlet,并读取struts-config.xml到内存。
web.xml
<?xml version="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0">
<display-name>struts_login</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
(4)在浏览器输入url打开jsp页面
(5)输入用户名、密码,提交表单
(6)根据web.xml文件的配置,action被提交到ActionServlet,调用ActionServlet的doGet/doPost方法à调用process方法
(7)à调用RequestProcessor类(核心类)的process方法(核心方法)。在process方法中,
A、截取url(调用processPath方法);
B、读取struts-config.xml中<action-mappings>的配置信息,取得Action(调用processMapping方法);
C、读取struts-config.xml中<form-beans>的配置信息,取得ActionForm(调用processActionForm方法);
D、ActionForm收集表单数据,首先转换数据类型,然后设置到ActionForm中(调用processPopulate方法)
E、执行用户自定义Action中的execute方法,返回ActionForward(调用processActionPerform方法)
F、根据返回的ActionForward,读取struts-config.xml中<forward>的配置信息,完成页面跳转。
struts-config.xml
<?xml version="1.0"encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-beanname="loginForm"type="com.ys.struts.LoginActionForm"></form-bean>
</form-beans>
<action-mappings>
<actionpath="/login"
type="com.ys.struts.LoginAction"
name="loginForm"
scope="request">
<forwardname="success"path="/login_success.jsp"></forward>
<forwardname="error" path="/login_error.jsp"></forward>
</action>
</action-mappings>
</struts-config>