struts 和spring 整合

来源:互联网 发布:手机照片恢复软件推荐 编辑:程序博客网 时间:2024/05/01 18:49

一    struts 配置

1:首先是struts的架包导入:

   

commons-fileupload-1.3.1.jar

commons-io-2.2.jar

commons-lang3-3.1.jar

commons-logging-1.1.3.jar

freemarker-2.3.19.jar

javassist-3.11.0.GA.jar

ognl-3.0.6.jar

struts2-core-2.3.16.3.jar

xwork-core-2.3.16.3.jar

2:配置struts 的 web.xml 内容

      <filter>
        <filter-name>struts2</filter-name>        
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

     <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter>

<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

3:配置 struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name="struts2"  extends="struts-default">
       <action name="tiaozhuan" class="com.action.TiaoZhuanAction" method="tiao">
           <result name="success">/webpage/success.jsp</result>
       </action>
   </package>
</struts>

4:网页实现跳转

index.jsp 中跳转

<a href="tiaozhuan.action">测试struts跳转</a>  

5:配置***Action.java

public class TiaoZhuanAction  extends ActionSupport{
public String tiao(){
return "success";
}

二:spring 配置

1:导入spring所需最基础的架包

spring-core.jar

 报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    ——>依赖:spring-web.jar

        ——>依赖:spring-context.jar
        ——>依赖:spring-beans.jar
        ——>依赖:commons-logging.jar

还需:

            struts2-spring-plugin-2.1.8.1.jar

**报错:java.lang.NoClassDefFoundError:org/springframework/asm/ClassVisitor

需导入:org.springframework.asm-3.0.5.RELEASE.jar

**报错:java.lang.NoClassDefFoundError: org/springframework/expression/PropertyAccessor

需导入:org.springframework.expression-3.1.0.M1.jar 


2:web.xml中spring部分

     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

通过配置ContextLoaderListener监听器,使容器启动时,自动加载applicationContext配置,

默认情况下,会加载WEB-INF/applicationContext.xml这个文件,我们可以通过配置contextConfigLocation参数改变配置文件的路径

<context-param>     <param-name>contextConfigLocation</param-name>     <param-value>WEB-INF/classes/applicationContext.xml</param-value></context-param>
可以有多个<param-value></param-value>标签


3:struts.xml中增加内容

<struts>

      <constant name="struts.objectFactory" value="spring" />

</struts>

4:配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
           
           <bean id="tiaozhuanspring" class="com.action.TiaoZhuanAction"></bean>
</beans>

4:事项spring跳转应用

struts中配置action使用sprng产生的bean,

      <action name="tiaozhuansp" class="tiaozhuanspring" method="tiao">
           <result name="success">/webpage/success.jsp</result>
       </action>      

5:在网页中进行测试

    <a href="tiaozhuansp.action">测试struts,spring跳转</a>

0 0