本文描述下 sypro 项目中使用 springMVC+Hibernate配置,初学SpringMVC做下简单整理解。

1.web项目首先我们要使用 web.xml文件将 spring配置引入进来

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  5. <description>springMvc+hibernate4+easyui</description>
  6. <display-name>sypro2.02</display-name>
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-ehcache.xml,classpath:spring-apacheFtpServer.xml</param-value>
  10. </context-param>
  11. <filter>
  12. <filter-name>openSessionInViewFilter</filter-name>
  13. <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  14. <init-param>
  15. <param-name>singleSession</param-name>
  16. <param-value>true</param-value>
  17. </init-param>
  18. </filter>
  19. <filter>
  20. <description>字符集过滤器</description>
  21. <filter-name>encodingFilter</filter-name>
  22. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  23. <init-param>
  24. <description>字符集编码</description>
  25. <param-name>encoding</param-name>
  26. <param-value>UTF-8</param-value>
  27. </init-param>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>openSessionInViewFilter</filter-name>
  31. <url-pattern>*.do</url-pattern>
  32. </filter-mapping>
  33. <filter-mapping>
  34. <filter-name>encodingFilter</filter-name>
  35. <url-pattern>/*</url-pattern>
  36. </filter-mapping>
  37. <listener>
  38. <description>spring监听器</description>
  39. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  40. </listener>
  41. <listener>
  42. <description>apache ftp server监听器</description>
  43. <listener-class>sy.util.ApacheFtpServerListener</listener-class>
  44. </listener>
  45. <listener>
  46. <description>Introspector缓存清除监听器</description>
  47. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  48. </listener>
  49. <servlet>
  50. <description>spring mvc servlet</description>
  51. <servlet-name>springMvc</servlet-name>
  52. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  53. <init-param>
  54. <description>spring mvc 配置文件</description>
  55. <param-name>contextConfigLocation</param-name>
  56. <param-value>classpath:spring-mvc.xml</param-value>
  57. </init-param>
  58. <load-on-startup>1</load-on-startup>
  59. </servlet>
  60. <servlet-mapping>
  61. <servlet-name>springMvc</servlet-name>
  62. <url-pattern>*.do</url-pattern>
  63. </servlet-mapping>
  64. <session-config>
  65. <session-timeout>3</session-timeout>
  66. </session-config>
  67. <welcome-file-list>
  68. <welcome-file>index.jsp</welcome-file>
  69. <welcome-file>init.jsp</welcome-file>
  70. </welcome-file-list>
  71. <error-page>
  72. <error-code>404</error-code>
  73. <location>/error/404.jsp</location>
  74. </error-page>
  75. <error-page>
  76. <error-code>500</error-code>
  77. <location>/error/500.jsp</location>
  78. </error-page>
  79. <distributable />
  80. </web-app>

部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是”/WEB-INF
/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。
如果是要自定义文件名可以在web.xml里加入contextConfigLocation如下,在<param-value>
</param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔

  1. <display-name>sypro2.02</display-name>
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-ehcache.xml,classpath:spring-apacheFtpServer.xml</param-value>
  5. </context-param>
  6. <filter>
  7. <filter-name>openSessionInViewFilter</filter-name>
  8. <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  9. <init-param>
  10. <param-name>singleSession</param-name>
  11. <param-value>true</param-value>
  12. </init-param>
  13. </filter>

加载contextConfigLocation则交给监听来实现

  1. <listener>
  2. <description>spring监听器</description>
  3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  4. </listener>

然后配置springMVC处理请求映射

  1. <servlet>
  2. <description>spring mvc servlet</description>
  3. <servlet-name>springMvc</servlet-name>
  4. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5. <init-param>
  6. <description>spring mvc 配置文件</description>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:spring-mvc.xml</param-value>
  9. </init-param>
  10. <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>springMvc</servlet-name>
  14. <url-pattern>*.do</url-pattern>
  15. </servlet-mapping>

2.applicationContext.xml 文件常用配置,在该项目中使用的自定义写法 将
applicationContext.xml 重命名为: spring-mvc.xml
,注意如果不使用自定义写法applicationContext.xml则存放在
/WEB-INF/applicationContext.xml目录且不可重命名

applicationContext.xml (spring-mvc.xml) 常用配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  11. <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
  12. <context:component-scan base-package="sy.controller" />
  13. <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
  14. <bean id="mappingJacksonHttpMessageConverter"
  15. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  16. <property name="supportedMediaTypes">
  17. <list>
  18. <value>text/html;charset=UTF-8</value>
  19. </list>
  20. </property>
  21. </bean>
  22. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  23. <bean
  24. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  25. <property name="messageConverters">
  26. <list>
  27. <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
  28. </list>
  29. </property>
  30. </bean>
  31. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  32. <bean
  33. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  34. p:prefix="/" p:suffix=".jsp" />
  35. <bean id="multipartResolver"
  36. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  37. <property name="defaultEncoding">
  38. <value>UTF-8</value>
  39. </property>
  40. <property name="maxUploadSize">
  41. <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
  42. </property>
  43. <property name="maxInMemorySize">
  44. <value>4096</value>
  45. </property>
  46. </bean>
  47. <!-- 拦截器 -->
  48. <mvc:interceptors>
  49. <mvc:interceptor>
  50. <mvc:mapping path="/**" />
  51. <bean class="sy.interceptors.EncodingInterceptor" />
  52. </mvc:interceptor>
  53. <mvc:interceptor>
  54. <mvc:mapping path="/**" />
  55. <bean class="sy.interceptors.AuthInterceptor" />
  56. </mvc:interceptor>
  57. </mvc:interceptors>
  58. </beans>

spring-xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. ">
  10. <!-- 引入属性文件 -->
  11. <context:property-placeholder location="classpath:config.properties" />
  12. <!-- 自动扫描dao和service包(自动注入) -->
  13. <context:component-scan base-package="sy.dao,sy.service" />
  14. </beans>
  1. <!-- 引入属性文件 -->
  2. <context:property-placeholder location="classpath:config.properties" />

项目配置映入属性文件在配置hibenrate数据库配置提供了便利

如下 spring-hibernate.xml 配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  9. ">
  10. <!-- JNDI方式配置数据源 -->
  11. <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  12. <property name="jndiName" value="${jndiName}"></property> </bean> -->
  13. <!-- dbcp数据源 -->
  14. <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  15. <property name="driverClassName" value="${driverClassName}"></property>
  16. <property name="url" value="${url}"></property>
  17. <property name="username" value="${username}"></property>
  18. <property name="password" value="${password}"></property>
  19. </bean>
  20. <bean id="sessionFactory"
  21. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  22. <property name="dataSource" ref="dataSource" />
  23. <property name="hibernateProperties">
  24. <props>
  25. <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
  26. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  27. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  28. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  29. </props>
  30. </property>
  31. <!-- 注解方式配置 -->
  32. <!-- <property name="packagesToScan"> <list> <value>sy.hbm</value> </list>
  33. </property> -->
  34. <!-- hbm方式配置 -->
  35. <property name="mappingDirectoryLocations">
  36. <list>
  37. <value>classpath:sy/hbm</value>
  38. </list>
  39. </property>
  40. </bean>
  41. <!-- 配置事务 -->
  42. <bean name="transactionManager"
  43. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  44. <property name="sessionFactory" ref="sessionFactory"></property>
  45. </bean>
  46. <tx:annotation-driven transaction-manager="transactionManager" />
  47. </beans>

spring缓存配置

    1. <!-- 开启spring缓存 -->
    2. <cache:annotation-driven cache-manager="cacheManager" />
    3. <bean id="cacheManagerFactory"
    4. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    5. p:configLocation="classpath:ehcache.xml" p:shared="false" />
    6. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
    7. p:cacheManager-ref="cacheManagerFactory" />

springMVC+Hibernate配置的更多相关文章

  1. spring + springMVC +hibernate 配置2

    这种方式比较精简 配置项不多 spring采用自动扫描配置 ,分spring_springMVC.xml  . hibernate_config.xml 两个文件 web.xml配置如下 <?x ...

  2. springMVC用法 以及一个简单的基于springMVC hibernate spring的配置

    替代struts 1  web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ...

  3. Maven搭建SpringMVC+Hibernate项目详解 【转】

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  4. Spring MVC+Spring +Hibernate配置事务,但是事务不起作用

    最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...

  5. springmvc+hibernate入门-揭开神秘的面纱

            Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这 ...

  6. Maven搭建SpringMVC+Hibernate项目详解

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  7. springMVC+Hibernate常用的配置文件

    每次写一个新的web项目时都要写配置文件.比较麻烦,现在把常用到的配置文件记录下来,方便以后使用 web.xml <?xml version="1.0" encoding=& ...

  8. 项目总结SpringMVC+hibernate框架 web.xml 分析(2)

    紧接 项目总结SpringMVC+hibernate框架 原理(MVC) applicationContext.xml 文件(3) 这一步讲解项目模块化的配置,项目中每个模块配置一个文件,命名规则为 ...

  9. Maven搭建SpringMVC+Hibernate项目详解(转)

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

随机推荐

  1. 安卓开发-使用XML菜单布局简单介绍

    使用xml布局菜单   目前为止我们都是通过硬编码来增加菜单项的,android为此提供了一种更便利的方式,就是把menu也定义为应用程序的资源,通过android对资源的本地支持,使我们可以更方便地 ...

  2. myeclipse中常用的快捷键

    存盘 Ctrl+s(肯定知道) 注释代码 Ctrl+/ 取消注释 Ctrl+\(Eclipse3已经都合并到Ctrl+/了) 代码辅助 Alt+/ 快速修复 Ctrl+1 代码格式化 Ctrl+Shi ...

  3. HDU 1010 Temper of the bone(深搜+剪枝)

    Tempter of the Bone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  4. equal与==区别

    对于String中的“equal方法”和“==”一直有点混肴,今天重新看了一下他们两点的区别,记录下来让自己以后不在忘记! 先说“==”: “==”是用来比较两个String对象在内存中的存放地址是否 ...

  5. java包(package)

    为了更好地组织类,java提供了包机制,用于区别类名的命名空间. 这样在不同的命名空间就可以有相同命名的类. 1 把功能相似或相关的类或接口阻止在同一个包中,方便类的查找和使用. 2 如同文件夹一样, ...

  6. Linux -- ls只显示目录

    ls没有直接显示目录的选项, 不过根据目录和文件显示的差异,可以搭配grep来实现 方法1: ll | grep "^d" 方法2: ls -F | grep$ "/$& ...

  7. Linux学习 -- Shell编程 -- 流程控制

    if语句 单分支 if [ 条件判断式 ]; then 程序 fi 或者 if [ 条件判断式 ] then 程序 fi 例子: 双分支 if [ 条件判断式 ] then 程序 else 程序 fi ...

  8. 二叉树,平衡树,红黑树,B~/B+树汇总

    二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操作的时候,都不需要彻底重建原始的索引树 ...

  9. android 5.0新特性学习--CardView

    CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View. 官网地址:https://de ...

  10. VirtualBox中CentOS通过Host-Only方式实现虚拟机主机互相访问、共享上网

    VirtualBox常用的网络配置如下: 连接方式 主机访问虚拟机 虚拟机访问主机 虚拟机访问虚拟机 虚拟机访问外网 说明 网络地址转换(NAT) 不支持 支持 不支持 支持 默认连接方式,虚拟IP, ...