Struts2整合Spring Hibernate的CRUD实例

来源:互联网 发布:三国志10 知乎 编辑:程序博客网 时间:2024/05/26 16:00
1. 添加Spring 2.0的Libraries

选择以下4个jar,并配置到/WEB-INF/lib下
Spring2.0 AOP Libraries
Spring2.0 Core Libraries
Spring2.0 Persistence Core Libraries
Spring2.0 WEb Libraries
同时,将applicationContext.xml配置到WEB/INF下面。

2. 添加Hibernate 3.0的Libraries

选择以下2个jar,并配置到/WEB-INF/lib下
Hibernate 3.0 Core Libraries
hibernate 3.0 Advanced Support Libraries
同时Hibernate的文件选择合并到applicationContext.xml(即不单独创建hibernate的配置文件,而是将其作为资源文件整合到applicationContext.xml中)

3. 添加commons-pool-1.4.jar和commons-dbcp-1.2.2.jar两个jar到lib下,以消除applicationContext.xml中找不到class的的错误

4. 添加struts2的五个jar到lib下

commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
xwork-2.0.5.jar
struts-core-1.3.5.jar
注意:由于要整合Spring,还需要添加struts2-spring-plugin-2.0.11.2.jar

5. 配置web.xml

5.1 配置spring的应用上下文
<!-- spring的应用上下文 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
5.2 配置struts2的过滤器

<!-- struts2 过滤器 -->
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
5.3 配置spring的监听器,以便在启动时就自动加载spring的配置
<!-- spring的监听器,以便在启动时就自动加载spring的配置 -->
<listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
5.4 配置OpenSessionInViewFilter过滤器,处理事务
  <!-- OpenSessionInViewFilter过滤器 -->
<filter>
  <filter-name>lazyLoadingFilter</filter-name>
  <filter-class>
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>lazyLoadingFilter</filter-name>
  <url-pattern>*.action</url-pattern>
</filter-mapping>

6. 配置struts.xml

6.1 添加一常量,将action交予Spring处理
<constant name="struts.objectFactory" value="spring"></constant>
6.2 配置action

7. 将struts.xml中的action注册到applicationContext.xml中

8. 其他...



本文提供Struts2整合Spring Hibernate的CRUD的实例的源代码

1. 数据库脚本,数据库采用MySQL 5.0

CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. 几个重要的文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="[url=http://java.sun.com/xml/ns/javaee]http://java.sun.com/xml/ns/javaee[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://java.sun.com/xml/ns/javaee]http://java.sun.com/xml/ns/javaee[/url]
[url=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]">
<!-- spring的应用上下文 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!-- struts2 过滤器 -->
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring的监听器,以便在启动时就自动加载spring的配置 -->
<listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
<!-- OpenSessionInViewFilter过滤器 -->
<filter>
  <filter-name>lazyLoadingFilter</filter-name>
  <filter-class>
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>lazyLoadingFilter</filter-name>
  <url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
  <welcome-file>/user/userAdd.jsp</welcome-file>
</welcome-file-list>
</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[url=http://www.springframework.org/schema/beans]http://www.springframework.org/schema/beans[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://www.springframework.org/schema/beans]http://www.springframework.org/schema/beans[/url] [url=http://www.springframework.org/schema/beans/spring-beans-2.0.xsd]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]">

<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/catalog">
  </property>
  <property name="username" value="root"></property>
  <property name="password" value="ethip"></property>
</bean>
<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>org/ethip/catalog/model/User.hbm.xml</value>
   </list>
  </property>
</bean>
<bean id="UserDAO" class="org.ethip.catalog.dao.UserDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
</bean>
<bean id="UserService"
  class="org.ethip.catalog.service.UserService">
  <property name="dao">
   <ref bean="UserDAO" />
  </property>
</bean>
<bean name="addUserBean" class="org.ethip.catalog.web.UserAction"
  scope="prototype">
  <property name="service">
   <ref bean="UserService" />
  </property>
</bean>
<bean name="listUserBean" class="org.ethip.catalog.web.UserAction"
  scope="prototype">
  <property name="service">
   <ref bean="UserService" />
  </property>
</bean>
<bean name="deleteUserBean" class="org.ethip.catalog.web.UserAction"
  scope="prototype">
  <property name="service">
   <ref bean="UserService" />
  </property>
</bean>
<bean name="editUserBean" class="org.ethip.catalog.web.UserAction"
  scope="prototype">
  <property name="service">
   <ref bean="UserService" />
  </property>
</bean>
<bean name="updateUserBean" class="org.ethip.catalog.web.UserAction"
  scope="prototype">
  <property name="service">
   <ref bean="UserService" />
  </property>
</bean>
</beans>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "[url=http://struts.apache.org/dtds/struts-2.0.dtd]http://struts.apache.org/dtds/struts-2.0.dtd[/url]">
<struts>
<include file="struts-default.xml" />
<!-- 此constant设置临时文件存放目录,因为默认的default.properties中没有指定 -->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<!-- 将action托管给spring -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="SSH2" extends="struts-default">
  <action name="userAdd" class="addUserBean" method="userAdd">
   <result name="success" type="redirect">userList.action</result>
  </action>
  <action name="userList" class="listUserBean" method="userList">
   <result name="success">/user/userList.jsp</result>
  </action>
  <action name="userDelete" class="deleteUserBean"
   method="userDelete">
   <result name="success" type="redirect">userList.action</result>
  </action>
  <action name="userEdit" class="editUserBean"
   method="userEdit">
   <result name="success">/user/userEdit.jsp</result>
  </action>
  <action name="userUpdate" class="updateUserBean"
   method="userUpdate">
   <result name="success" type="redirect">userList.action</result>
  </action>
</package>
</struts>

3. 本实例仅实现CRUD功能,没有实现其他的处理,如数据校验、国际化、分页等.

4. 由于附件过大,lib下所有的jar都删掉了,请各位学习者按照上一文的操作导入。

附件是ssh2目录结构


原创粉丝点击