第一篇文章已经导入了基础的jar包,整合struts2+spring
一,导入struts2-spring-plugin-2.3.37.jar(版本根据自身需求添加),再添加配置文件applicationContext.xml,并加入对struts的管理
applicationContext.xml配置文件存放位置:
1:src下面 需要在web.xml中定义如下:
contextConfigLocation
classpath:applicationContext.xml
< /context-param>
2:WEB-INF下面 需要在web.xml中定义如下:
contextConfigLocation
WEB-INF/applicationContext*.xml
< /context-param>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="loginAction" class="com.action.LoginAction" scope="prototype">
</bean>
</beans>
二,修改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>
<constant name="struts.devMode" value="false" />
<package name="struts2" extends="struts-default">
<!-- class的内容要与Spring配置文件中的bean的id相同 -->
<action name="login" class="loginAction">
<result name="success">/result.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
三,修改web.xml加入spring相关支持(注意这里根据applicationContext.xml配置文件的位置修改相应的配置,我这里将applicationContext.xml配置文件放在WEB-INF下面)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SSH</display-name>
<welcome-file-list>
<!-- //默认首页 -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 用来定位Spring框架配置文件 ,最好放在struts配置的前面,防止出现意外错误。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- 配置Spring监听 ,这也放在struts前面吧。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器。配置如下: -->
<!-- <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> -->
<!-- Struts2.1.3开始,将废弃ActionContextCleanUp过滤器,
而在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能。
三个初始化参数:
1、config参数:指定要加载的配置文件。逗号分割。
2、actionPackages参数:指定Action类所在的包空间。逗号分割。
3、configProviders参数:自定义配置文件提供者,需要实现ConfigurationProvider接口类。逗号分割。 -->
<!-- struts2的配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
四,重启tomcat,打开页面
点击login,页面跳转
如果一切顺利的话,struts2跟spring的整合完成。
五,总结
1.Struts主要起控制作用,
Spring主要起解耦作用,
Hibernate主要起操作数据作用
2.搭建过程中遇到了
org.springframework.web.context.ContextLoaderListener
这个错误,通过排查,原来时jar包导入没有生效
这里告诉大家一个万能的方法(将所有jar包复制到lib下面再add to build path)
3.加油小白,你又离大牛近了一步了。
4.下一篇再一起把hibernate整合,SSH框架就算加减完毕。