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请求乱码

<!-- 处理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  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  class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="comboPooledDataSource"></property>
</bean>

 

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值