Spring与Struts2整合VS Spring与Spring MVC整合

Spring与Struts2整合,struts.xml在src目录下

1.在web.xml配置监听器

web.xml

<!-- 配置Spring的用于初始化ApplicationContext的监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<!-- 配置Struts2的主过滤器 -->
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

2,拷贝一个用于整合的jar包(与Spring整合用的一个插件)
        struts-2.1.8.1/lib/struts2-spring-plugin-2.1.8.1.jar
3,说明:
    1,在写Action时要指定 @Controller 与 @Scope("prototype")
    2,在struts.xml中配置action时,在class属性中写bean的名称

Spring与Spring MVC整合

web.xml

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
</context-param>
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!-- 不要再采用/*,使用*.xxx -->
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
上一篇:JAVA中堆内存空间管理问题探讨


下一篇:c语言基础学习10_文件操作02