这种方式比较精简 配置项不多 spring采用自动扫描配置 ,分spring_springMVC.xml 、 hibernate_config.xml 两个文件
web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>SpringMVCDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-configfiles.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVCDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>welcome.jsp</welcome-file> </welcome-file-list> </web-app>
spring_springmvc.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:annotation-driven/><!-- 开启扫描驱动 --> <mvc:default-servlet-handler/><!-- 默认加载静态资源 --> <context:component-scan base-package="com.educationcrm.controller" /><!--自动扫描--> <context:component-scan base-package="com.educationcrm.service.imp"/><!--自动扫描--> <!-- 跳转节点控制--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> <-- hibernate 配置--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocations" value="classpath:hibernate-configfiles.xml"/> </bean> <!--拦截器必须实现 org.springframework.web.servlet.HandlerInterceptor 接口 --> <mvc:interceptors> <!--Session判断拦截器--> <mvc:interceptor> <mvc:mapping path="/session/*/*"/> <bean class="com.educationcrm.interceptor.Session_interceptor"></bean> </mvc:interceptor> <!--字符转换拦截器--> <mvc:interceptor> <mvc:mapping path="/*"/> <bean class="com.educationcrm.interceptor.Charact_interceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!--<!–kaptcha验证码配置 –>--> <!--<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">--> <!--<property name="config">--> <!--<bean class="com.google.code.kaptcha.util.Config">--> <!--<constructor-arg>--> <!--<props>--> <!--<prop key="kaptcha.border">no</prop>--> <!--<prop key="kaptcha.border.color">105,179,90</prop>--> <!--<prop key="kaptcha.textproducer.font.color">red</prop>--> <!--<prop key="kaptcha.image.width">120</prop>--> <!--<prop key="kaptcha.textproducer.font.size">35</prop>--> <!--<prop key="kaptcha.image.height">40</prop>--> <!--<prop key="kaptcha.session.key">code</prop>--> <!--<prop key="kaptcha.textproducer.char.length">4</prop>--> <!--<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>--> <!--</props>--> <!--</constructor-arg>--> <!--</bean>--> <!--</property>--> <!--</bean>--> </beans>
hibernate.xml配置如下
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url" >jdbc:mysql://localhost/EducationCRM?useUnicode=true&characterEncoding=utf-8</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">false</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="sessionInfoName">sessionInfo</property> <property name="uploadFieldName">filedata</property> <property name="uploadFileMaxSize">20971520</property> <property name="uploadFileExts">txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid</property> <property name="uploadDirectory">attached</property> <mapping class="com.educationcrm.dao.BuyVipCard"/> </session-factory> </hibernate-configuration>