SSH框架项目配置和启动的加载顺序及请求的执行顺序

1:======配置和启动======

(1)配置web.xml

配置<context-param>,其中内容为Spring的配置文件applicationContext.xml。注意<param-name>的内容,必须为"contextConfigLocation";
配置<listener>,使用Spring提供的ContextLoaderListener。
配置范例:

1 <context-param>
2     <param-name>contextConfigLocation</param-name>
3     <param-value>classpath:applicationContext-*.xml</param-value>
4 </context-param>
5 <listener>
6     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7 </listener>

解析:
当Web应用启动时,将执行以下操作:
①由Tomcat创建一个ServletContext,并自动载入<context-param>中的属性;
②ContextLoaderListener检测到ServletContext的创建,运行Spring的相关程序;
③Spring根据<context-param>中contextConfigLocation的指向,加载对应applicationContext.xml;
④Spring根据applicationContext.xml的内容创建一个BeanFactory实例,并放入ServletContext中。
简而言之,该配置的作用是:当Web应用启动时,Spring将自动创建一个BeanFactory实例,并放入ServletContext中。

(2)配置Struts-config.xml

struts-config.xml文件中,注意其<action>标签的type属性,设置为Spring的代理Action类。由此真正的action创建交给Spring完成。
若不使用Spring,type设置为com.project.LoginAction等自定义类;使用Spring,type设置为"org.springframework.web.struts.DelegatingActionProxy"。
但"仍需构建具体Action",并编写Action的具体方法。该Action"在Spring的配置中引用",详见"(4)配置Spring"。
相当于将Struts配置转移到了Spring配置中,此处即Struts与Spring框架结合的关键结合点。
配置范例:

 1 <struts-config>
 2     <form-beans>
 3         <form-bean name="loginForm" type="com.project.usermgr.web.forms.LoginActionForm"/>
 4     </form-beans>
 5     <action-mappings>
 6         <action path="/login"
 7                 type="org.springframework.web.struts.DelegatingActionProxy"
 8                 name="loginForm"
 9                 scope="request"
10         >
11             <forward name="success" path="/login_success.jsp"/>
12         </action>
13     </action-mappings>
14     <message-resources parameter="MessageResources" />
15 </struts-config>

(3)配置application.xml中的Spring-bean信息
由Spring来创建和管理Action,并向Action注入Model层Service对象。
设置scope="prototype"后可使每个线程都有一个新的Action,从而解决Struts1.x的Action线程安全问题。
注意:
①必须使用name属性,且name属性的值必须和struts-config.xml文件中<action>标签的path属性值一致
②必须配置Model层Service对象,如userManager等。
配置范例:

1 <bean name="/login" class="com.project.usermgr.web.actions.LoginAction" scope="prototype">
2         <property name="userManager" ref="userManager"/>
3     </bean>

(4)构建Action
Model层Service对象由Spring"自动注入",因此无需手动构建该对象,也无需获取BeanFactory。通过"自动注入"的方式获取对象,即"依赖注入"。
注意,相关对象须提供set方法,以方便Spring注入。

 1 public class LoginAction extends Action {
 2     private UserManager userManager;
 3     @Override
 4     public ActionForward execute(ActionMapping mapping, ActionForm form,
 5             HttpServletRequest request, HttpServletResponse response)
 6             throws Exception {
 7         LoginActionForm laf = (LoginActionForm)form;
 8         String username = laf.getUsername();
 9         String password = laf.getPassword();
10         userManager.login(username, password);
11         return mapping.findForward("success");
12     }
13     //须提供set方法,以方便Spring注入。
14     public void setUserManager(UserManager userManager) {
15         this.userManager = userManager;
16     }
17 }

 

2:=======请求的执行=======

--------------请求的过程:-------------

1.服务器启动,创建Struts2的Filter控制器,创建Spring容器对象.

   实例化Struts2控制器时,加载struts.xml,struts-default.xml,default.properties,struts-plugin.xml等Struts相关配置

   实例化Spring容器时,加载applicationContext.xml

2.客户浏览器发送请求,请求到达Struts2的Filter控制器

3.如果是Action请求,Struts2控制器根据struts.xml的<action>配置,

   要调用一个Action对象处理.

4.Struts2控制器调用struts-spring-plugin.jar提供的ObjectFactory

   获取一个Action对象.

   方法一:ObjectFactory利用<action>元素的class属性去Spring容器寻找id=class的Bean对象.

   *方法二:如果按上述方法找不到,ObjectFactory会利用class指定值创建一个对象.然后将Spring容器的DAO,Service按名称匹配规则给Action注入.

 5.Struts2控制器调用Action执行业务处理,处理完毕,返回一个String标识

 6.Struts2控制器根据String标识调用Result组件,生成响应信息

 7.将响应信息给客户浏览器输出,完成响应处理.

posted @ 2019-10-24 19:57  小马000  阅读(1709)  评论(0编辑  收藏  举报