Spring+SpringMVC整合----配置文件

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請求亂碼java

<!-- 處理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請求web

<!-- 將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 文件中配置視圖解析器spring

<!-- 配置視圖解析器 -->
<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 數據源express

<!-- 加載外部屬性文件 -->
<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 mvc

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

 

 11.在 bean.xml 文件中配置事務管理器及基於註解的事務app

<!-- 配置事務管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 開啓基於註解的事務 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
相關文章
相關標籤/搜索