Spring整合SpringMVC

搭建maven环境

项目框架视图

配置Spring applicationContext.xml

ssm框架思路是由Spring框架作为基干,整合SpringMVC和Mybatis,首先就是要写Spring的配置文件

  • 开启spring注解扫描 <context:component-scan base-package="">
  • 不扫描controller注解 交给SpringMVC处理
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            ">

<!--    开启spring注解的扫描 controller 不需要扫描-->
    <context:component-scan base-package="cn.cai">
<!--    将controller层交给 springmvc扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

测试可用后再进行springmvc的配置


配置SpringMVC
配置web.xml
  • 先在web.xml中配置dispatcherServlet 前置控制器 拦截所有客户端页面的请求
  • 配置characterEncodingFilter springmvc的字符编码过滤器
<servlet>
        <servlet-name>dispatcherServlet</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>
<servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

<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>
配置springmvc.xml
  • 开启扫描 只扫描org.springframework.stereotype.Controller下的注解
  • 配置视图解析器internalResourceViewResolver 配置视图的资源路径以及转到什么类型的文件
  • 将dispatcherServlet拦截的静态资源放行
  • 开启mvc注释支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启扫描器 扫描controller标签-->
    <context:component-scan base-package="cn.cai..controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--开启视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"/>
        <property name="suffix" value=".jsp" />
    </bean>

    <!--静态资源放行-->
    <mvc:resources location="/resources/images/" mapping="/resources/images/**"/>
    <mvc:resources location="/resources/css/" mapping="/resources/css/**"/>
    <mvc:resources location="/resources/js/" mapping="/resources/js/**"/>

    <!--开启mvc注解支持-->
    <mvc:annotation-driven/>
</beans>

测试mvc可用后进行下一步配置


开启spring监听器

tomcat开启后会通过web.xml读取springmvc的配置文件
可是spring的配置文件虽然写完了 却读取不了 于是配置一个监听器 写在web.xml中 在tomcat启动时读取spring的配置文件
spring自己写了一个监听类 继承自ServletContextListener 生命周期与tomcat一致
org.springframework.web.context.ContextLoaderListener

<!--    spring监听器 只会加载WEB/INF下的applicationContext.xml 需要另外配置 配置文件路径-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<!--    配置了spring的配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

整个流程
  1. 配置spring 关闭对controller的扫描 交由springmvc处理
  2. 配置springmvc
    2.1 配置DispatcherServlet 前置控制器(web.xml)
    2.2 配置CharacterEncodingFilter 字符编码过滤器 (web.xml)
    2.3 开启扫描(springmvc.xml)
    2.4 配置InternalResourceViewResolver视图解析器(springmvc.xml)
    2.5 放行前端控制器拦截的静态资源(springmvc.xml)
    2.6 开启springmvc注解的支持(springmvc.xml)
  3. 配置ContextLoaderListener监听器 (web.xml)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。