ITPub博客

首页 > 应用开发 > IT综合 > Struts2整合Spring Hibernate的CRUD实例

Struts2整合Spring Hibernate的CRUD实例

IT综合 作者:小梁124 时间:2014-11-11 16:13:52 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



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的应用上下文 -->

  contextConfigLocation
  /WEB-INF/applicationContext*.xml

<!-- struts2 过滤器 -->

  struts2
  
   org.apache.struts2.dispatcher.FilterDispatcher
  



  struts2
  /*

<!-- spring的监听器,以便在启动时就自动加载spring的配置 -->

  
   org.springframework.web.context.ContextLoaderListener
  


<!-- OpenSessionInViewFilter过滤器 -->

  lazyLoadingFilter
  
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  



  lazyLoadingFilter
  *.action


  /user/userAdd.jsp


applicationContext.xml



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]">

  class="org.apache.commons.dbcp.BasicDataSource">
      value="com.mysql.jdbc.Driver">
  

      value="jdbc:mysql://localhost:3306/catalog">
  

  
  

  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  
   
  

  
   
   
     org.hibernate.dialect.MySQLDialect
   

   

  

  
   
    org/ethip/catalog/model/User.hbm.xml
   

  



  
   
  


  class="org.ethip.catalog.service.UserService">
  
   
  


  scope="prototype">
  
   
  


  scope="prototype">
  
   
  


  scope="prototype">
  
   
  


  scope="prototype">
  
   
  


  scope="prototype">
  
   
  



struts.xml






<!-- 此constant设置临时文件存放目录,因为默认的default.properties中没有指定 -->

<!-- 将action托管给spring -->


  
   userList.action
  

  
   /user/userList.jsp
  

      method="userDelete">
   userList.action
  

      method="userEdit">
   /user/userEdit.jsp
  

      method="userUpdate">
   userList.action
  




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

文章来源:http://www.nei-mao.com/


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29870867/viewspace-1328545/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-09-11

  • 博文量
    71
  • 访问量
    214129