springMVC+Hibernate配置
本文描述下 sypro 项目中使用 springMVC+Hibernate配置,初学SpringMVC做下简单整理解。
1.web项目首先我们要使用 web.xml文件将 spring配置引入进来
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <description>springMvc+hibernate4+easyui</description>
- <display-name>sypro2.02</display-name>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-ehcache.xml,classpath:spring-apacheFtpServer.xml</param-value>
- </context-param>
- <filter>
- <filter-name>openSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter>
- <description>字符集过滤器</description>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <description>字符集编码</description>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>openSessionInViewFilter</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <listener>
- <description>spring监听器</description>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <listener>
- <description>apache ftp server监听器</description>
- <listener-class>sy.util.ApacheFtpServerListener</listener-class>
- </listener>
- <listener>
- <description>Introspector缓存清除监听器</description>
- <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
- </listener>
- <servlet>
- <description>spring mvc servlet</description>
- <servlet-name>springMvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <description>spring mvc 配置文件</description>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springMvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>3</session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>init.jsp</welcome-file>
- </welcome-file-list>
- <error-page>
- <error-code>404</error-code>
- <location>/error/404.jsp</location>
- </error-page>
- <error-page>
- <error-code>500</error-code>
- <location>/error/500.jsp</location>
- </error-page>
- <distributable />
- </web-app>
部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是”/WEB-INF
/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。
如果是要自定义文件名可以在web.xml里加入contextConfigLocation如下,在<param-value>
</param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔
- <display-name>sypro2.02</display-name>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-ehcache.xml,classpath:spring-apacheFtpServer.xml</param-value>
- </context-param>
- <filter>
- <filter-name>openSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
加载contextConfigLocation则交给监听来实现
- <listener>
- <description>spring监听器</description>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
然后配置springMVC处理请求映射
- <servlet>
- <description>spring mvc servlet</description>
- <servlet-name>springMvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <description>spring mvc 配置文件</description>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springMvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
2.applicationContext.xml 文件常用配置,在该项目中使用的自定义写法 将
applicationContext.xml 重命名为: spring-mvc.xml
,注意如果不使用自定义写法applicationContext.xml则存放在
/WEB-INF/applicationContext.xml目录且不可重命名
applicationContext.xml (spring-mvc.xml) 常用配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
- <context:component-scan base-package="sy.controller" />
- <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
- <bean id="mappingJacksonHttpMessageConverter"
- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
- </list>
- </property>
- </bean>
- <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/" p:suffix=".jsp" />
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="defaultEncoding">
- <value>UTF-8</value>
- </property>
- <property name="maxUploadSize">
- <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
- </property>
- <property name="maxInMemorySize">
- <value>4096</value>
- </property>
- </bean>
- <!-- 拦截器 -->
- <mvc:interceptors>
- <mvc:interceptor>
- <mvc:mapping path="/**" />
- <bean class="sy.interceptors.EncodingInterceptor" />
- </mvc:interceptor>
- <mvc:interceptor>
- <mvc:mapping path="/**" />
- <bean class="sy.interceptors.AuthInterceptor" />
- </mvc:interceptor>
- </mvc:interceptors>
- </beans>
spring-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:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- ">
- <!-- 引入属性文件 -->
- <context:property-placeholder location="classpath:config.properties" />
- <!-- 自动扫描dao和service包(自动注入) -->
- <context:component-scan base-package="sy.dao,sy.service" />
- </beans>
- <!-- 引入属性文件 -->
- <context:property-placeholder location="classpath:config.properties" />
项目配置映入属性文件在配置hibenrate数据库配置提供了便利
如下 spring-hibernate.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:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- ">
- <!-- JNDI方式配置数据源 -->
- <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName" value="${jndiName}"></property> </bean> -->
- <!-- dbcp数据源 -->
- <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${driverClassName}"></property>
- <property name="url" value="${url}"></property>
- <property name="username" value="${username}"></property>
- <property name="password" value="${password}"></property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
- <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
- </props>
- </property>
- <!-- 注解方式配置 -->
- <!-- <property name="packagesToScan"> <list> <value>sy.hbm</value> </list>
- </property> -->
- <!-- hbm方式配置 -->
- <property name="mappingDirectoryLocations">
- <list>
- <value>classpath:sy/hbm</value>
- </list>
- </property>
- </bean>
- <!-- 配置事务 -->
- <bean name="transactionManager"
- class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- </beans>
spring缓存配置
- <!-- 开启spring缓存 -->
- <cache:annotation-driven cache-manager="cacheManager" />
- <bean id="cacheManagerFactory"
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
- p:configLocation="classpath:ehcache.xml" p:shared="false" />
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
- p:cacheManager-ref="cacheManagerFactory" />
springMVC+Hibernate配置的更多相关文章
- spring + springMVC +hibernate 配置2
这种方式比较精简 配置项不多 spring采用自动扫描配置 ,分spring_springMVC.xml . hibernate_config.xml 两个文件 web.xml配置如下 <?x ...
- springMVC用法 以及一个简单的基于springMVC hibernate spring的配置
替代struts 1 web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ...
- Maven搭建SpringMVC+Hibernate项目详解 【转】
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
- Spring MVC+Spring +Hibernate配置事务,但是事务不起作用
最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...
- springmvc+hibernate入门-揭开神秘的面纱
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这 ...
- Maven搭建SpringMVC+Hibernate项目详解
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
- springMVC+Hibernate常用的配置文件
每次写一个新的web项目时都要写配置文件.比较麻烦,现在把常用到的配置文件记录下来,方便以后使用 web.xml <?xml version="1.0" encoding=& ...
- 项目总结SpringMVC+hibernate框架 web.xml 分析(2)
紧接 项目总结SpringMVC+hibernate框架 原理(MVC) applicationContext.xml 文件(3) 这一步讲解项目模块化的配置,项目中每个模块配置一个文件,命名规则为 ...
- Maven搭建SpringMVC+Hibernate项目详解(转)
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
随机推荐
- java断点
第一步: 用firefox运行程序,当点击保存,提示保存失败后,启动firebug 通过请求找到addNew.ezt出现错误,在eztnews.xml里通过ctrl+F查找找到请求执行的类和方法 找到 ...
- Android中Edittext的属性
//此为转载别人的,挺不错的 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" ...
- kvm的live-snapshot
目前项目中已经存在的快照是针对卷的快照,并且需要关机.所以目前的需求有两个:1.不关机快照:2.针对虚拟机的快照,而不是针对券的快照. 由需求所以针对libvirt做了一些实验,纪录如下: 环境:物理 ...
- AndroidGradle --多渠道打包配置(转发)
需求 国内Android app发布一般会有多个渠道,为了跟踪发展情况,通常会为每一个渠道定制一个特别的apk. 一般友盟之类第三方统计的渠道ID定义如下,以wandoujia为例 <meta- ...
- ZooKeeper应用理论及其应用场景
ZooKeeper Client APIZooKeeper Client Library提供了丰富直观的API供用户程序使用,下面是一些常用的API: ● create(path, data, fla ...
- C# 上传文件至远程服务器
C# 上传文件至远程服务器(适用于桌面程序及web程序) 2009-12-30 19:21:28| 分类: C#|举报|字号 订阅 最近几天在玩桌面程序,在这里跟大家共享下如何将本地文件上传 ...
- 几种在shell命令行中过滤adb logcat输出的方法
我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adblog ...
- 模态对话框 bootstrap-modal.js
调用方式 通过data属性 无需编写JavaScript代码即可生成一个对话框.在一个主控元素,例如按钮,上设置data-toggle="modal",然后再设置data-targ ...
- CentOS6.5 常用命令
卸载 rpm -e XXX yum -y remove XXX 查看内核版本 cat /proc/version
- Ubuntu 下开发 Android 环境变量设置
-----------------------------------------------------ANDROID_SDK_HOME:/home/cmm/avds PATH:/home/cmm/ ...