一.web.xml
配置三项dispatcherServlet,characterEncodingFilter,ContextLoaderListener
<web-app> <display-name>Archetype Created Web Application</display-name> <!--中心调度dispatcherServlet,tomcat启动创建servlet,加载springmvc.xml配置文件--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!--设置为1时tomcat启动创建servlet--> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--过滤器,解决post请求乱码问题--> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--监听器,在servletContext创建的时候加载配置文件applicationContext.xml--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
二.springmvc.xml配置
springmvc.xml中主要配置扫描controller注解的类,视图解析器viewResolver,注解驱动,静态资源标注
<!--包扫描controller-->
<context:component-scan base-package="cn.itcast"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter> </context:component-scan> <!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--字符串转日期转换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.itcast.utils.StringToDateConverter"></bean> </set> </property> </bean> <!--注解驱动--> <mvc:annotation-driven conversion-service="conversionService"/> <!--静态资源不过滤--> <mvc:resources mapping="/js/" location="/js/**"/> <mvc:resources mapping="/css/" location="/css/**"/> <mvc:resources mapping="/images/" location="/images/**"/>
三.applicationContext.xml配置
applicationContext.xml中包含了spring和mybatis的配置
spring的配置有:包扫描,事务(事务管理,事务通知,事务通知切入)
mybatis的配置有:数据源,Sqlsessionfactory,DAO接口扫描
<!--包扫描除了controller--> <context:component-scan base-package="cn.itcast"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> </context:component-scan> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> </bean> <!--接口扫描--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.itcast.dao"></property> </bean> <!--数据源--> <context:property-placeholder location="classpath:jdbcConfig.properties"></context:property-placeholder> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.classDriver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--事务管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" /> </tx:attributes> </tx:advice> <!--事务通知切入--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.service.impl.*.*(..))"></aop:advisor> </aop:config>