【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD

12年前

一、导入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也可配置为如下类

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="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:p="http://www.springframework.org/schema/p"   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">    <property name="driverClassName"     value="com.mysql.jdbc.Driver">    </property>    <property name="url" value="jdbc:mysql://localhost:3306/myssh">    </property>    <property name="username" value="root"></property>    <property name="password" value="123"></property>   </bean>   <bean id="sessionFactory"    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    <!-- if use the hibernate.cfg.xml as dataSource configuration, cancel the annotations -->    <!-- remove other properties and dataSource bean. -->    <!--     <property name="configLocation" value="classpath:hibernate.cfg.xml" />     -->    <property name="dataSource">     <ref bean="dataSource" />    </property>    <property name="hibernateProperties">     <props>      <prop key="hibernate.dialect">       org.hibernate.dialect.MySQLDialect      </prop>      <prop key="hibernate.hbm2ddl.auto">update</prop>     </props>    </property>    <property name="mappingResources">     <list>      <value>com/hzboy/orm/Userinfo.hbm.xml</value>     </list>    </property>   </bean>         <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>

          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"            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">    <!-- 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>


四、配置struts2的配置文件struts.xml如下:

<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"    "http://struts.apache.org/dtds/struts-2.1.dtd">  <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>    

    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