1、先进行WEB.XML配置,并在web.xml首先引入springmvc-servlet.xml文件,
注意: <servlet-name>的名字,加上-servlet.xml,为servlet.xml文件,例如 <servlet-name>spring</servlet-name>,则servlet.xml文件名称为:spring-servlet.xml
<!-- 配置框架springmvc,配置文件所在位置-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/mvc/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>//加载时首先加载为1
</servlet>
2、将spring加载到web.xml中
<!-- 将spring 加载到web中 -->添加监听
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
3、配置上下文路径
<!-- 配置上下文路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring.xml,/WEB-INF/spring/spring-*.xml</param-value>
</context-param>
说明:如果有很多的关于spring的配置文件,建议分开写,比如事务一个文件(spring-transaction.xml),springmvc-hibernate.xml一个配置文件,这样方便读写。
4、配置项目路径
<!-- 设置url路径 / 表示项目名称-->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5、配置过滤器
<!-- 配置过滤器 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
说明:什么样的文件进入框架
6、设置编码格式
<!-- 设置编码格式为utf-8 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置springmvc-servlet.xml
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!--在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
;
<context:component-scanbase-package="com.app.controller" />//com.app.controller为要扫描的包名
<!-- 定时器开关 开始 -->
<task:annotation-driven />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 激活自动代理功能 -->
<aop:aspectj-autoproxyproxy-target-class="true" />
<!-- 静态资源处理 css js imgs -->
<mvc:resources location="/resource/"mapping="/resource/**" />
<mvc:resources location="/static/" mapping="/static/**"/>
<mvc:resources location="/up/"mapping="/up/**" />
<mvc:view-controller path="/"view-name="forward:/indexcon/index" />
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<beanclass="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
<!-- 如果不定义 mvc:mapping path 将拦截所有的URL请求 -->
<beanclass="com.app.filter.CommonInterceptor"></bean>
</mvc:interceptors>
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
<value>text/json;charset=utf-8</value>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding"value="utf-8" />
<!-- 文件大小最大值 5M (1*1024*1024)=1M -->
<property name="maxUploadSize"value="52428800" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize"value="40960" />
<!-- 启用是为了推迟文件解析,以便捕获文件大小异常 -->
<property name="resolveLazily"value="true" />
</bean>
<!-- 配置ViewResolver 。可用多个ViewResolver 。使用order属性排序。 InternalResourceViewResolver
放在最后 -->
<bean
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension"value="true" />
<property name="favorParameter"value="true" />
<property name="ignoreAcceptHeader"value="true"></property>
<property name="defaultContentType"value="text/html" />
<property name="mediaTypes">
<map>
<!-- 告诉视图解析器,返回的类型为json格式 -->
<entry key="json" value="application/json"/>
<entry key="xml"value="application/xml" />
<entry key="htm" value="text/htm"/>
<entry key="file"value="application/octet-stream" />
<entry key="image" value="image/*"/>
</map>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix"value="/WEB-INF/view/" />
<property name="suffix"value=".jsp" />
</bean>
<!--定义异常处理页面 -->
<bean id="exceptionResolver"class="com.app.exception.MySimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<propkey="java.lang.SQLException">error/500</prop>
<propkey="java.lang.Exception">error/500</prop>
<prop key="java.net.ConnectException">error/500</prop>
</props>
</property>
</bean>
</beans>
配置springmvc-hibernate.xml
<beans><!-- 数据库的连接信息 -->
注意:要与jdbc.properties中的名称、值相同
<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>
<property name="url"value="jdbc:oracle:thin:@localhost:1521:orcl" />
<propertyname="username" value="system" />
<propertyname="password" value="root" />
</bean>
<!-- 配置sessionfactory -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<propertyname="dataSource" ref="dataSource"/>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<propkey="hibernate.hbm2ddl.auto">update</prop>
<propkey="hibernate.show_sql">true</prop> <!-- hibernate 的拼写的sql在控制台输出 -->
<propkey="hiberante.format_sql">true</prop> <!-- 使用hibernate的标准sql -->
</props>
</property>
<propertyname="configLocations">
<list>
<value>classpath*:com/phome/cofig/hibernate.cfg.xml</value> <!-- 对象关系映射,即实体类与数据库表的对应信息,这里采用注释的方式,而不采用xml文件的形式 -->
</list>
</property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionBese"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"lazy-init="true" abstract="true">
<propertyname="transactionManager"ref="transactionManager"></property>
<propertyname="transactionAttributes">
<props> <!-- 定义什么样的方法会用到事务,所有带add*,update*,insert*,find*,delete*,get*,set*方法开头的都可以用事务管理-->
<propkey="add*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="update*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="find*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="get*">PROPAGATION_NEVER</prop>
<propkey="set*">PROPAGATION_NEVER</prop>
</props>
</property>
</bean>
</beans>
配置spring.xml
<!-- 配置bean的注入方式,以注解(以@开始)的方式注入bean-->
<!-- 以注解方式注入bean -->
<context:component-scanbase-package="com.phome" />
<!-- 以注解方式注入bean的属性 -->
<context:annotation-config />
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!--在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
;
<context:component-scanbase-package="com.app.controller" />//com.app.controller为要扫描的包名
<!-- 定时器开关 开始 -->
<task:annotation-driven />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 激活自动代理功能 -->
<aop:aspectj-autoproxyproxy-target-class="true" />
<!-- 静态资源处理 css js imgs -->
<mvc:resources location="/resource/"mapping="/resource/**" />
<mvc:resources location="/static/" mapping="/static/**"/>
<mvc:resources location="/up/"mapping="/up/**" />
<mvc:view-controller path="/"view-name="forward:/indexcon/index" />
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<beanclass="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
<!-- 如果不定义 mvc:mapping path 将拦截所有的URL请求 -->
<beanclass="com.app.filter.CommonInterceptor"></bean>
</mvc:interceptors>
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
<value>text/json;charset=utf-8</value>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding"value="utf-8" />
<!-- 文件大小最大值 5M (1*1024*1024)=1M -->
<property name="maxUploadSize"value="52428800" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize"value="40960" />
<!-- 启用是为了推迟文件解析,以便捕获文件大小异常 -->
<property name="resolveLazily"value="true" />
</bean>
<!-- 配置ViewResolver 。可用多个ViewResolver 。使用order属性排序。 InternalResourceViewResolver
放在最后 -->
<bean
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension"value="true" />
<property name="favorParameter"value="true" />
<property name="ignoreAcceptHeader"value="true"></property>
<property name="defaultContentType"value="text/html" />
<property name="mediaTypes">
<map>
<!-- 告诉视图解析器,返回的类型为json格式 -->
<entry key="json" value="application/json"/>
<entry key="xml"value="application/xml" />
<entry key="htm" value="text/htm"/>
<entry key="file"value="application/octet-stream" />
<entry key="image" value="image/*"/>
</map>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix"value="/WEB-INF/view/" />
<property name="suffix"value=".jsp" />
</bean>
<!--定义异常处理页面 -->
<bean id="exceptionResolver"class="com.app.exception.MySimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<propkey="java.lang.SQLException">error/500</prop>
<propkey="java.lang.Exception">error/500</prop>
<prop key="java.net.ConnectException">error/500</prop>
</props>
</property>
</bean>
</beans>
配置springmvc-hibernate.xml
<beans><!-- 数据库的连接信息 -->
注意:要与jdbc.properties中的名称、值相同
<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>
<property name="url"value="jdbc:oracle:thin:@localhost:1521:orcl" />
<propertyname="username" value="system" />
<propertyname="password" value="root" />
</bean>
<!-- 配置sessionfactory -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<propertyname="dataSource" ref="dataSource"/>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<propkey="hibernate.hbm2ddl.auto">update</prop>
<propkey="hibernate.show_sql">true</prop> <!-- hibernate 的拼写的sql在控制台输出 -->
<propkey="hiberante.format_sql">true</prop> <!-- 使用hibernate的标准sql -->
</props>
</property>
<propertyname="configLocations">
<list>
<value>classpath*:com/phome/cofig/hibernate.cfg.xml</value> <!-- 对象关系映射,即实体类与数据库表的对应信息,这里采用注释的方式,而不采用xml文件的形式 -->
</list>
</property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionBese"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"lazy-init="true" abstract="true">
<propertyname="transactionManager"ref="transactionManager"></property>
<propertyname="transactionAttributes">
<props> <!-- 定义什么样的方法会用到事务,所有带add*,update*,insert*,find*,delete*,get*,set*方法开头的都可以用事务管理-->
<propkey="add*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="update*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="find*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<propkey="get*">PROPAGATION_NEVER</prop>
<propkey="set*">PROPAGATION_NEVER</prop>
</props>
</property>
</bean>
</beans>
配置spring.xml
<!-- 配置bean的注入方式,以注解(以@开始)的方式注入bean-->
<!-- 以注解方式注入bean -->
<context:component-scanbase-package="com.phome" />
<!-- 以注解方式注入bean的属性 -->
<context:annotation-config />