- Struts 1 配置文件
Struts 1 应用的配置文件有两个分别是web.xml与struts-config.xml文件。其中,web.xml文件时配置所有Web应用的,而struts-config.xml文件时Struts 1专用配置为文件。
web.xml的文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置ActionServlet -->
<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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<!-- Servlet映射成.do为后缀的文件 -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
sturts-config.xml的文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<!-- FormBean 本质上是javaBean,用来存储页面表单中各个域的值-->
<form-beans>
<form-bean name="customerForm" type="com.sanqing.struts.form.CustomerForm" />
</form-beans>
<global-exceptions />
<!-- 定义整个系统都可以使用的全局转向中转地址 -->
<global-forwards ></global-forwards>
<!-- web.xml文件中后缀为.do的用户请求被转到这里处理 -->
<action-mappings>
<action
name="customerForm"
input="/main.jsp"
parameter="actionType"
path="/customer"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" />
</action-mappings>
<message-resources
parameter="com.sanqing.struts.action.ApplicationResources" />
<!-- action交给spring去管理,这个是spring 的plug-in 的配置 -->
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:applicationContext.xml" />
</plug-in>
</struts-config>
- Struts 1 处理过程
Web应用启动时加载并初始化ActionServlet,ActionServlet从struts-config.xml文件中读取配置信息,并把他们存放到各种配置对象中。启动后的应用处理过程如下。
(1) 通过客户端页面与用户进行交互, 将页面提交的数据封装到ActionForm中。
(2) 桶过请求路径查找struts-config.xml配置文件中Action的配置,找到并调用对应的Action.
(3) 在Action中调用业务逻辑方法处理用户的请求。
(4)查找ActionMapping,找到正确的jsp页面进行转发,返回给客户端。