zoukankan      html  css  js  c++  java
  • struts_spring

    struts于spring整合方案:
     思路:
      1.让web工程启动的时候,直接初始化spring容器
      2.action对象池归属问题,应该由谁来进行处理
     步骤:
      1.引入struts能力,导入相关包
      2.引入spring能力,导入相关包:spring 3.0 core libraries,spring 3.0 web libraries(用于启动容器的listener在其中)
      3.(一)把spring提供的用于进行spring初始化的listener配置进入web.xml
       目的:让web容器初始化的时候直接加载spring容器
       在web.xml中加入:
         <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
         <context-param>
            <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
         </context-param>
       (二)在struts-config.xml中初始化spring容器
        <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
          <set-property property="contextConfigLocation"
              value="/WEB-INF/classes/applicationContext.xml"/>
        </plug-in>
      4.spring对action的处理有两种解决方案:
       方案一:
        在struts-config.xml中:
         action的整套配置不变
          <action-mappings>
           <action path="/a" type="action.AAction" ></action>
          </action-mappings>
         加入controllor屏蔽struts自身的action对象池(位置要求:在action-mappings标签和message-resources标签之间)
          <!-- 屏蔽了struts自身的Action对象池,当去的Action对象的时候.通过controllor逻辑,自动到spring对象池中查找对象 -->
             <controller>
              <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
             </controller>
          在spring配置文件applicationContext.xml中:
           <!-- 以下为service配置 -->
         <bean id="aService" class="service.AServiceImple"></bean>

         <!-- 以下为action配置,属性名为name,而不是id name = struts-config.xml中的path属性值-->
         <bean name="/a" class="action.AAction">
          <property name="aService" ref="aService"/>
         </bean>
       方案二:
        在struts-config.xml中:
         <!-- 所有的请求均交给spring提供的DelegatingActionProxy类.此类根据用户的请求名称xx.do,到spring容器中找对应的Action,调用其execute方法 -->
         <action path="/a" type="org.springframework.web.struts.DelegatingActionProxy">
          <forward name="success" path="success.jsp"></forward>
         </action>
        在spring配置文件applicationContext.xml中:
         <bean name="/a" class="action.AAction"></bean>
       方案三(不建议):
        在Action类中:
         WebApplicationContext wac = (WebApplicationContext) request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
         AService aService = (AService) wac.getBean("aService");
         aService.aMethod();

  • 相关阅读:
    JS代码和网页的混用
    猫扑论坛遭遇黑客挂马
    安全专家解析黑客攻击网银四大手段
    网页分析利器:IEDevToolBar
    2009年世界顶级杀毒软件排行榜
    英特尔遭遇CPU级RootKit,目前无药可医
    Linq联表查询
    从客户端中检测到有潜在危险的 Request.Form 值。
    MVC Cookie的使用
    TimSpan比较时间差
  • 原文地址:https://www.cnblogs.com/liaren/p/struts_spring.html
Copyright © 2011-2022 走看看