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

Struts+Hibernate+Spring 集成

2010-06-01 09:09 423 查看
使用MyEclipse 集成SSH的顺序最好为:Spring Hibernate Struts 可以减少很多不必要的麻烦 

导入Spring

Spring  AOP Libraries

Spring  Core Libraries

Spring  ORM/DAO/Hibernate3.0

Spring Web Libraries

把这几个包添加进去

导入Hibernate

选择Hibernate3.0

Hibernate 3.0 Core Libraries

然后会弹出一个对话框有两个选项如下:

Spring configuration file

Hibernate configuration file  我们选择它,让Hibernate自己构造文档

下一步将会让你创建一个会话工厂

SessionFactory id: sessionFactory  自己随便取一个名字填写

下一步将提示你配置数据源

这个就不做介绍了(略)

下一步将提示你是否建立一个会话工厂类,我们使用Spring集成这一步可以省略,去掉勾勾就可以了!

导入Struts

Struts specification(翻译过来就是选择Struts版本)

我们选择Struts1.2

OK所有的导入完成!现在我们将通过配置文件将Struts、Hibernate、Spring  集成起来!!

第一部进行struts-config.xml配置

第二部将ActionServlet让Spring管理

第三部进行web.xml文件的配置

第一部:

struts-config.xml配置

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<!-- 这里你可能还没有,你创建一个Action将会出现如下配置 -->
<action-mappings >
<action path="/test" type="com.yourcompany.struts.action.TestAction" />
</action-mappings>
<!-- <Controller>中的配置全部可以复制出来,以后直接粘贴便可以使用
需要注意:
<controller></controller>
必须写在<message-resources>之前
-->
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

第二部分

复制application.xml文件到src下改个名字,我取的名字为struts_web.xml 主要是要头文件

<?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-2.0.xsd"> 
<?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-2.0.xsd"> <!--这需要注意了:不在是ID,现在必须为name
在name中写上struts-config.xml中  找到path="/test" 属性
class中填写包与类名
-->
<bean name="/test" class="com.yourcompany.struts.action.TestAction">

</bean>

</beans>


第三部:

 web.xml文件的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<context-param>
<!--
<param-value></param-value>
中间一定要选择配置文件的路径哟,不要可会报错
如果不知道路径可以写成
classpath:applictionContext.xml

-->
<!--
struts_web.xml为第二部Action让Spring管理所以也需要配置出来
当然也可以让applictionContext.xml管理但是为了简话与配置文件的分开
所以在这配置比较好
-->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:struts_web.xml
classpath:applicationContext.xml,
</param-value>
</context-param>

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

<!-- <context-param>  <listener>  都可以复制下来
可以方便以后项目配置,直接粘贴方可使用,进行<param-value>中的修改就可以了
-->

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

到此所有的配置结束

-------------------------------------------------------------------------------------------------------------------------------------

注意:

如果你用的是MyEclipse5.0

那么你需要查看下spring 哪个配置文件application.xml

<?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-2.0.xsd"> 
<!-- 如果你的MyEclipse是5.0
注意是不是为classpath如果不是需要进行修改
很多人都因为这个原因而找不到错误
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
-->

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

</beans>
如果你以上配置完全正确,但还是有错误,使用MyEclipse6.0的朋友一定要注意了,当然还包括其它版本

pring 和 Hibernate 共用的一些 jar 文件发生了版本冲突, 删除 WEB-INF/lib/asm-2.2.3.jar 然后重启 WEB服务器

好了完了!

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiaomaha/archi
4000
ve/2008/03/29/2229237.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息