zoukankan      html  css  js  c++  java
  • spring和struts整合

    整合准备:导入jar包

    如果只是访问action,没有做数据库方面的操作的话 只需要导入下面的jar

    spring相关jar

     

    以及struts相关jar包

    整合过程:

    用到了struts所以需要在web.xml中配置过滤器 ,又因为使用到了spring的监听器来提高性能,所以也需要配置监听器

    web.xml代码:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="2.5" 
     3     xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     7   <welcome-file-list>
     8     <welcome-file>index.jsp</welcome-file>
     9   </welcome-file-list>
    10   <!-- 配置struts过滤器 -->
    11   <filter>
    12      <filter-name>struts2</filter-name>
    13      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>      
    14      <init-param>
    15           <param-name>actionPackages</param-name>
    16           <param-value>com.mycompany.myapp.actions</param-value>
    17      </init-param>    
    18   </filter>
    19   
    20   <filter-mapping>
    21       <filter-name>struts2</filter-name>
    22        <url-pattern>/*</url-pattern>
    23   </filter-mapping>
    24   <!-- 配置监听器 -->
    25   <listener>
    26       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    27   </listener>
    28   <context-param>
    29       <param-name>contextConfigLocation</param-name>
    30       <param-value>classpath:bean.xml</param-value>
    31   </context-param>
    32 </web-app>

    在src下分别创建struts.xml和applicationContext.xml文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6 <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
     7     <package name="default" extends="struts-default" namespace="/">
     8         <!--之前的做法:<action name="user" class="action的类路径"></action>-->
     9         <!-- 把创建action对象交给spring进行管理  所以这里不用重新创建  
    10         在spring配置中已经创建了这里将要创建的action对象
    11         所以只需要指定bean.xml中的创建的id值    class属性中指定的是bean.xml中创建的对象的id值-->
    12         <action name="user" class="user"></action>
    13     </package>
    14 </struts>
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:aop="http://www.springframework.org/schema/aop"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     8     http://www.springframework.org/schema/beans/spring-beans.xsd
     9     http://www.springframework.org/schema/context
    10     http://www.springframework.org/schema/context/spring-context.xsd
    11     http://www.springframework.org/schema/aop
    12     http://www.springframework.org/schema/aop/spring-aop.xsd
    13     http://www.springframework.org/schema/tx
    14     http://www.springframework.org/schema/tx/spring-tx.xsd">
    15     
    16     <!-- spring容器创建action对象  struts配置文件中就不需要重新创建了 只需要指定id值就行 -->
    17     <bean id="user" class="org.action.UserAction" scope="prototype"></bean>
    18 </beans>

    注意这里 action中的class属性的值和spring配置文件中的id值对应

    action代码:

     1 package org.action;
     2 
     3 import javax.servlet.ServletContext;
     4 
     5 import org.apache.struts2.ServletActionContext;
     6 import org.springframework.context.ApplicationContext;
     7 import org.springframework.web.context.WebApplicationContext;
     8 
     9 import com.opensymphony.xwork2.ActionSupport;
    10 
    11 public class UserAction extends ActionSupport {
    12 
    13     @Override
    14     public String execute() throws Exception {
    15         // TODO Auto-generated method stub
    16         System.out.println("action。。。。。。。");
    17         ServletContext s=ServletActionContext.getServletContext();
    18         ApplicationContext ac=(ApplicationContext) s.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    19         if(ac!=null){
    20             System.out.println("服务器启动时创建了applicationContext对象.........");
    21         }
    22         return NONE;
    23     }
    24 
    25 }

    启动服务器:

    控制台打印:

    请求action时的过程:

    请求action时,会根据请求的action名称 去struts配置中找到与之对应的id值
    找到之后不需要重新创建action对象了 因为创建对象的交给了Spring管理
    加载Spring配置文件的时候 如果其中有对象的配置 那么此时就会创建配置对象
    而一般为了提高性能 在服务器启动的时候就去加载Spring配置文件(利用Spring监听器实现) 创建在其中配置的对象 并且放在域对象中
    所以 这里就会根据struts配置文件中对应id值后的class属性(是Spring配置文件的id值 不是action类路径)的值 去Spring配置文件中找到与之对应的id值
    这样配置的前提当然是需要导入Spirng与struts整合jar包
    【struts2-spring-plugin-2.3.30.jar】

  • 相关阅读:
    js跨域之window.name跨域解决方案
    html中用IE条件判断
    收集的CSS浏览器兼容问题大全(粘贴过来,会用到的)
    移动网站开发系列(一)
    离散数学归谬法与反证法的比较
    端口未正常关闭解决方法
    nodejs 连接 mysql 出现 Client does not support authentication protocol requested by server; consider upgrading MySQL client
    http规范
    退役后的苦逼高三生活续++
    NOIP2018 游记
  • 原文地址:https://www.cnblogs.com/Joke-Jay/p/6523391.html
Copyright © 2011-2022 走看看