web.xml文件
<!-- OpenSessionInView --> <filter> <filter-name>opensessioninview</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>opensessioninview</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- SpringMVC中央控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup><!--值越小优先级越小 --> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 监听器 --> <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>
applicationContext.xml文件
<!-- 自动扫描 --> <context:component-scan base-package="com.peng"> <!--排除controller扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--事物管理 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.peng.entity"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> </props> </property> </bean> <!-- 邮件发送器 --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.126.com"/> <property name="username" value="kaishengit"/> <property name="password" value="p@ssw@rd"/> <property name="defaultEncoding" value="UTF-8"/> </bean> <!-- Hibernate事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置数据源 --> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverName}"/> <property name="url" value="jdbc:mysql:///test"/> <property name="username" value="root"/> <property name="password" value="lanxum"/> <property name="maxActive" value="20"/> <property name="initialSize" value="10"/> <property name="minIdle" value="5"/> </bean>
springmvc-servlet.xml配置
<!--自动扫描 --> <context:component-scan base-package="com.peng.controller"/> <!--启动注解 --> <mvc:annotation-driven/> <!-- 静态资源路径 --> <mvc:resources location="/static/" mapping="/static/**"/> <!--view配置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!--文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--上传文件大小 --> <property name="maxUploadSize" value="1000000"/> </bean> <!--拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.peng.controller.MyInterceptor"> <!-- 允许放行的 --> <property name="excluedUrls"> <list> <value>/user/home</value> </list> </property> </bean> </mvc:interceptor> </mvc:interceptors> <!-- 异常处理 --> <bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="com.peng.exception.AuthorizationException">redirect:/user/home</prop> </props> </property> </bean>