zoukankan      html  css  js  c++  java
  • Spring+Struts集成(方案一)

    SSH框架是现在非常流行的框架之一,本文接下来主要来对SpringStruts的集成进行展示.

             集成原理:Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象.

             集成框架图如下:

     

     

    计算机生成了可选文字:Web客户端一气request耀fo阴ard厂一一一一response了灵t1OnserVIJSpD1spatch切片「U片SACtionMode!Spring

     

    1 spring struts依赖包配置.

    *struts

    --拷贝struts相关java包和jstl.

    --web.xml中配置ActionServlet.

    --提供struts-config.xml核心配置文件.

    --提供struts国际化资源文件,最好提供默认国际化文件.

    *spring

    --拷贝spring相关java

    *SPRING_HOME/dist/spring.jar

    *SPRING_HOME/lib/log4j/log4j-1.2.14.jar

    *SPRING_HOME/lib/jakarta-commons/commons-logging.jar

    *SPRING_HOME/lib/aspectj/aspectjrt.jar

    *SPRING_HOME/lib/aspectj/aspectjweaver.jar

    --提供spring配置文件.

    2 web.xml文件中配置ContextLoaderListener,Web Server启动的时候将BeanFactory放在ServletContext

    代码如下:

                 

    1. <!-- 找到文件名 -->  
    2.       <context-param>  
    3.           <!-- 此处的名字是固定死的,在ContextLoader里中的一个常量 -->  
    4.           <param-name>contextConfigLocation</param-name>  
    5.           <param-value>classpath:applicationContext-*.xml</param-value>  
    6.       </context-param>  
    7.       <!-- 设置Listener,一次性创建BeanFactory -->  
    8.       <listener>  
    9.           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    10.       </listener>  

     

     3  Action中采用WebApplicationContextUtil.getRequiredWebApplicationContext()ServletContext中取得BeanFactory

             

    1. //从配置文件中的监听器中获取SessionFactory,此种方法不用每次频繁的创建SessionFactory  
    2.         BeanFactory factory=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());  
    3.         UserManager userManager = (UserManager)factory.getBean("userManager");  
    4.         userManager.login(username, password);  


     

    4  通过BeanFactoryIOC容器中取得业务逻辑对象.

     

          此集成方案的不足:WEB层中知道Spring的相关内容,因为需要去主动的查找对象:BeanFactory.可以通过依赖注入的方式解决此问题.方案二便是通过依赖注入的方式来进行.

  • 相关阅读:
    L2-004. 这是二叉搜索树吗?*
    L2-001. 紧急救援(最短路的变形)*
    L2-002. 链表去重(数组模拟)
    L1-028. 判断素数
    Linux相关
    2016ICPC-大连 A Simple Math Problem (数学)
    2016ICPC-大连 Convex (几何)
    2016ICPC-大连 To begin or not to begin (简单思维)
    TC704div2 C ModEquationEasy 矩阵快速幂+dp
    poj 3150 Cellular Automaton 矩阵快速幂
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3934525.html
Copyright © 2011-2022 走看看