Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记
每天进步一点点,大家好,我是王亚豪,希望和每位程序猿爱好者共同学习,共同进步。我的QQ:369737397,欢迎和大家进行交流
1. 添加Spring 2.0的Libraries
选择以下4个jar,并配置到/WEB-INF/lib下
Spring2.0 AOP Libraries
Spring2.0 Core Libraries
Spring2.0 Persistence Core Libraries
Spring2.0 WEb Libraries
同时,将applicationContext.xml配置到WEB/INF下面。
Spring2.0 AOP Libraries
Spring2.0 Core Libraries
Spring2.0 Persistence Core Libraries
Spring2.0 WEb Libraries
同时,将applicationContext.xml配置到WEB/INF下面。
2. 添加Hibernate 3.0的Libraries
选择以下2个jar,并配置到/WEB-INF/lib下
Hibernate 3.0 Core Libraries
hibernate 3.0 Advanced Support Libraries
同时Hibernate的文件选择合并到applicationContext.xml(即不单独创建hibernate的配置文件,而是将其作为资源文件整合到applicationContext.xml中)
Hibernate 3.0 Core Libraries
hibernate 3.0 Advanced Support Libraries
同时Hibernate的文件选择合并到applicationContext.xml(即不单独创建hibernate的配置文件,而是将其作为资源文件整合到applicationContext.xml中)
3. 添加commons-pool-1.4.jar和commons-dbcp-1.2.2.jar两个jar到lib下,以消除applicationContext.xml中找不到class的的错误
4. 添加struts2的五个jar到lib下
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
xwork-2.0.5.jar
struts-core-1.3.5.jar
注意:由于要整合Spring,还需要添加struts2-spring-plugin-2.0.11.2.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
xwork-2.0.5.jar
struts-core-1.3.5.jar
注意:由于要整合Spring,还需要添加struts2-spring-plugin-2.0.11.2.jar
5. 配置web.xml
5.1 配置spring的应用上下文
<!-- spring的应用上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
5.2 配置struts2的过滤器
<!-- spring的应用上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
5.2 配置struts2的过滤器
<!-- struts2 过滤器 -->
<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>
5.3 配置spring的监听器,以便在启动时就自动加载spring的配置
<!-- spring的监听器,以便在启动时就自动加载spring的配置 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
5.4 配置OpenSessionInViewFilter过滤器,处理事务
<!-- OpenSessionInViewFilter过滤器 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<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>
5.3 配置spring的监听器,以便在启动时就自动加载spring的配置
<!-- spring的监听器,以便在启动时就自动加载spring的配置 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
5.4 配置OpenSessionInViewFilter过滤器,处理事务
<!-- OpenSessionInViewFilter过滤器 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
6. 配置struts.xml
6.1 添加一常量,将action交予Spring处理
<constant name="struts.objectFactory" value="spring"></constant>
6.2 配置action
<constant name="struts.objectFactory" value="spring"></constant>
6.2 配置action
7. 将struts.xml中的action注册到applicationContext.xml中