【Struts2+Spring3+Hibernate3】SSH框架整合實現CRUD
一、導入Spring3.0、Hibernate3.0、Struts2開發庫。
二、配置web.xml文件的內容,如下:
<?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" version="3.0"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <description>Spring core configuration</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
1.如上struts2的核心控制器filter也可配置為如下類</span>
org.apache.struts2.dispatcher.FilterDispatcher
2.FilterDispatcher和StrutsPrepareAndExecuteFilter、StrutsPrepareFilter、
ExecuteFilter的區別參見:
http://peng-hao1988.iteye.com/admin/blogs/1472063
三、Spring的配置文件applicationContext.xml的配置內容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="<bean id="userAct" class="com.hzboy.action.UserManagerAct" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean id="userService" class="com.hzboy.service.UserManagerService"> <property name="dao" ref="dao"></property> </bean> <bean id="dao" class="com.hzboy.dao.BaseDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
</beans></pre>
1.Demo默認Hibernate的DataSource、HibernateProperties、
MappingResources信息配置到applicationContext.xml文件中。
如果使用Hibernate.cfg.xml配置這些信息,需要在
applicationContext.xml文件中將configLocation這個屬性去
掉注釋,將其他的property注釋或刪除。
2.applicationContext.xml默認放在webRoot\WEB-INF\路徑下,
如果想要將spring的配置文件移動到其他文件夾或修改名字,需要在
web.xml中添加如下代碼:
<context-param> <description> 我的spring配置文件放在WEB-INF根目錄下,文件名為springconfig.xml </description> <param-name>contextConfigLoaction</param-name> <param-value>/WEB-INF/springconfig.xml</param-value> </context-param>3.Demo默認Hibernate的DataSource、HibernateProperties、
MappingResources信息配置到applicationContext.xml文件
中。如果使用Hibernate.cfg.xml配置這些信息,需要在
applicationContext.xml文件中將configLocation這個屬性
去掉注釋,并將其他的property注釋或刪除。
Hibernate.cfg.xml配置如下:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" ";<!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/myssh </property> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile">MySQL</property> <mapping resource="com/hzboy/orm/Userinfo.hbm.xml" /> </session-factory> </hibernate-configuration></pre>
四、配置struts2的配置文件struts.xml如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "; <struts> <constant name="struts.objectFactory" value="spring" /> <package name="myApp" extends="struts-default"> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result> <result name="success">/WEB-INF/jsp/success.jsp</result> </global-results> <action name="userManagerAct" class="userAct" method="doLogin"> <result name="login">/index.jsp</result> </action><action name="query" class="userAct" method="doQuery" /> <action name="delete" class="userAct" method="doDelete" /> <action name="edit" class="userAct" method="doEdit"> <result name="editUser">/WEB-INF/jsp/editUser.jsp</result> </action> <action name="add" class="userAct" method="doAdd"> <result name="addUser">/WEB-INF/jsp/addUser.jsp</result> </action> </package>
</struts> </pre>
1.當使用spring管理struts2的 action時要注意需在struts2的action配
置文件中添加如下代碼:
<constant name="struts.objectFactory" value="spring" />并且需要將struts2-spring-plugin-2.2.1.jar添加到classpath中去。
2.如要將Struts2的默認配置文件struts.xml文件重命名或移動到WEB-INF
到下去,需要在web.xml的struts2過濾器中添加如下代碼:
<init-param> <description>我的struts配置文件放在WEB-INF根目錄下,文件名為action.xml</description> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,../action.xml</param-value> </init-param>
五、基本配置已經弄完了,在用工具添加MyEclipse和Eclipse添加開發庫是要注意
,盡量使用高版本的,如果是工具自帶的庫,一般不會出現問題,如果有錯誤的話,一
般是JAR包沖突,把一些名字一樣版本號不一樣的包刪除低版本的。如果是用戶自定義
的開發庫最好不要隨意添加JAR包,去官方下載完整的開發庫,根據需要添加,如果是
在不知道那些是需要的,那就全部添加進去。
示例代碼:http://download.csdn.net/detail/peng_hao1988/4191421