1.在 web.xml 中加载 spring 的配置文件 bean.xml
   底层是 Listener
<!-- Spring -->
<context-param>
      <param-name>contextConfigLocation</param-name>
      <!-- 指定spring的配置文件的路径和名称 -->
      <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

2.在 web.xml 中加载 springMVC 的配置文件
   底层是 Servlet
<!-- SpringMVC -->
<servlet>
      <servlet-name>springDispatcherServlet</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>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
      <servlet-name>springDispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
</servlet-mapping>

 

3.在 web.xml 文件中设置处理POST请求乱码

<!-- 处理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>

 

4.在 web.xml 文件中配置将POST请求转化为PUT、DELETE请求

<!-- 将POST请求转化为PUT、DELETE请求 -->
<filter>
      <filter-name>HiddenHttpMethodFilter</filter-name>
      <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
      <filter-name>HiddenHttpMethodFilter</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>

 

5.在 springmvc .xml 文件中配置要扫描的包
因为两个配置文件都扫描com,neuedu 所以会扫描两次,
但是SpringMVC 是扫描 Controller 的,所以在这配置 use-default-filters="false"
把 Controller 和 ControllerAdvice(注解可以标记在类上)放进去
<context:component-scan base-package="com.neuedu" use-default-filters="false">
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

 

6.在 springmvc .xml 文件中配置视图解析器

<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="WEB-INF/views/"></property>
      <property name="suffix" value=".jsp"></property>
</bean>

 

7.在 springmvc .xml 文件中配置以下可以处理静态资源
   <mvc:annotation-driven/>:原来正常的requestMapping将可以正常访问
<!-- 处理静态资源 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>

 

8.在 bean.xml 文件中配置要扫描的包
同步骤5,为了不扫描Controller ,使用 exclude-filter
<context:component-scan base-package="com.neuedu">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

 

 9.在 bean.xml 文件中配置 jdbc.properties 及 C3P0 数据源

<!-- 加载外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0 数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="user" value="${jdbc.username}"></property>
      <property name="password" value="${jdbc.password}"></property>
      <property name="driverClass" value="${jdbc.driver}"></property>
      <property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>

 

 10.在 bean.xml 文件中配置 jdbcTemplate 

<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="comboPooledDataSource"></property>
</bean>

 

 11.在 bean.xml 文件中配置事务管理器及基于注解的事务

<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 开启基于注解的事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

 

 posted on 2017-09-05 10:25  Lverson_tester  阅读(384)  评论(1编辑  收藏  举报