您的位置:首页 > 编程语言 > Java开发

struts_spring_hibernate集成

2009-02-15 22:21 429 查看
好了,ssh手动集成完成,也不知道怎么说,就是把前2个集成,放在一起。

先实现struts和spring,再实现hibernate和spring。

因为显示层和持久层关系不大。主要是通过业务层沟通起来。

核心还是spring,很好很强大。

这里有3个spring的配置文件:

applicationContext-actions.xml ---- 配置spring对struts中的路径和类;

applicationContext-beans.xml ---- 配置spring的IoC对类的管理;

applicationContext-common.xml --- 配置hibernate的sessionFactory和事务注入spring的IoC管理;

配置web.xml也so重要:

<!-- 将struts的核心控制器配置进容器 -->
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- 将spring的配置文件装载入web容器中 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!-- 通过filter设置程序编码为gbk -->
<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>

<!-- 配置 一个为了避免hibernate懒加载异常而创建的解决方案 进入容器 -->
<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>

注意2个地方别写错了:

1、在applicationContext-common.xml中:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="configLocation" value="classpath:hibernate.cfg.xml"/>

</bean>

这里面的classpath,在客户端或者junit测试中能通过。

但是在web项目中就出错了,是在读取sessionFactory的时候出错。

2、在web.xml中:

spring的配置文件通过监听(listener)载入web容器中,那么代码是:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath:applicationContext.xml

</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

其中的classpath也是不能丢的。

剩下我就把这几个spring中的包装类罗列出来:

org.springframework.orm.hibernate3.LocalSessionFactoryBean

本地的sessionFactory类 -- 加载SessionFactory

org.springframework.web.context.ContextLoaderListener

上下文加载监听 -- 加载applicationContext.xml

org.springframework.web.filter.CharacterEncodingFilter

字符编译过滤

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

处理session的lazy异常

org.springframework.orm.hibernate3.HibernateTransactionManager

hibernate事务管理器

org.springframework.web.struts.DelegatingActionProxy

代理action类 -- spring的包装类作用:1、取得BeanFactory。2、根据path找到IoC中的业务控制器(自己写的Action)

大概基本上就这几个了

so easy!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: