struts1.x+spring+hibernate集成(Action交给spring来创建,这时候要注意代理,实现了接口的类才能使用JDK动态代理,没有实现接口的类则只能使用CGLIB)
* 将所有的jar包拷贝到WEB-INF/lib下,如果jar包有重复的则删除一些,去版本新的
* 将spring,struts,hibernate的配置文件拷贝过来,spring和hibernate的放在src目录下,struts的放在WEB-INF
在str下:
applicationContext-action.xml
applicationContext-bean.xml
applicationContext-commons.xml (放的是申明式事务)
hibernate.cfs.xml
(在hibernate中加入:<property name="hibernate.hbm2ddl.auto">update</property>)
加入这个后不用我们手动的导出表,当创建SessionFactory的时候就会自动的导出
log4j.properties
MessageResources.properties
在WEB-INF下:struts.config.xml web.xml
在web.xml文件中要加入<context-param>和<listener>标签
1,解决字符集问题(采用Spring提供的filter)(将以下内容放到web.xml文件中)
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2,为了避免hibernate的懒加载异常而创建的解决方案
仍然是采用spring提供的
下面的是一个过滤器,能够让Session在请求解释完成后关闭(所以才能避免懒加载异常),将下面的代码copy到web.xml中
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>