Centos系统下载大全 | Redhat系统下载大全 | Windows2012系统下载大全 | Windows2008系统下载大全| CMS教程 | 网站地图 51运维网-专注Linux/Unix系统安全运维!
当前位置:51运维网 > 编程 > Jsp > 正文

Struts2整合Spring Hibernate的CRUD实例

时间:2013-09-23 10:59 来源:网络整理 作者:51ou.com 阅读:

Struts2整合Spring Hibernate的CRUD实例

来源:未知 作者:admin 责任编辑:dnzg.cn 发表时间:2010-03-13 19:24 点击:次

原帖及讨论:

Struts 2 + Spring 2.0 + Hibernate 3.0整合的操作可以参照我的这篇文章



本文提供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][/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance][/url]"
xsi:schemaLocation="[url=http://java.sun.com/xml/ns/javaee][/url]
[url=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][/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance][/url]"
xsi:schemaLocation="[url=http://www.springframework.org/schema/beans][/url] [url=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>

(责任编辑:dnzg.cn)


上一篇:JSP Sessions   下一篇:Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记

顶一下

(0)

0%

踩一下

(0)

感谢您对【51运维网 http://www.51ou.com/】的支持,我们为您免费提供《Struts2整合Spring Hibernate的CRUD实例》技术文章,《Struts2整合Spring Hibernate的CRUD实例》详细使用和说明,有时《Struts2整合Spring Hibernate的CRUD实例》可能不完善、敬请谅解!如果《Struts2整合Spring Hibernate的CRUD实例》有错误请给我们留言,我们将尽快修复文章错误,如果您觉得本站不错,请分享给周围的朋友!谢谢!

顶一下
(0)
0%
踩一下
(0)
0%
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
验证码:点击我更换图片