美文网首页
SpringMVC-基础配置说明

SpringMVC-基础配置说明

作者: 张明学 | 来源:发表于2020-05-24 20:18 被阅读0次

    本篇文章主要介绍SpringMVC的配置,更新中...
    Spring Web MVC官方文档

    Jar依赖


    <!-- spring容器 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <!-- spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <!-- spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    

    只引用spring-webmvc也可以,spring-webmvc已经引用了spring-beans、spring-core、spring-aop、spring-context、spring-expression、spring-web。在开发中还需要引用servlet和jsp,这两个jar包不用打到war包里部署到服务器中,一般服务器中,打到war中引用jar冲突。jstl是个选项,用到就引用。

    <!-- servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- jsp -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    

    web.xml


    SpringMVC的DispatcherServlet

    SpringMVC的DispatcherServlet作为应用SpringMVC功能基础

    <!-- SpringMVC核心控制器 -->
    <servlet>
        <servlet-name>SpringMVCDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动加载 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>SpringMVCDispatcherServlet</servlet-name>
        <!-- 将所有请求交由SpringMVC来管理 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    ContextLoaderListener

    ContextLoaderListener监听容器启动和关闭,启动时初始化WebApplicationContext,自动装配ApplicationContext的配置信息。关闭时销毁WebApplicationContext。

    1. 配置Spring的配置文件路径参数
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-mvc.xml</param-value>
    </context-param>
    
    1. 配置Listener
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    1. 如何获取WebApplicationContext
    // 获取方式1
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext()
    // 获取方式2
    ServletContext sc = request.getSession().getServletContext();
    ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc);
    

    其它配置

    <!-- 默认启动页面 -->
    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>
    
    <!-- 中文编码 -->
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    SpringMVC的配置


    <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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    
        <!-- 注解驱动 -->
        <mvc:annotation-driven/>
    
        <!-- 配置注解扫描路径 -->
        <context:component-scan base-package="com.zmx"/>
    
        <!-- 静态资源处理方式一:交由Web的默认servlet,不交给SpringMVC的DispatcherServlet -->
        <mvc:default-servlet-handler/>
        <!-- 静态资源处理方式一:由SpringMVC来管理 -->
        <!-- 可以放在根目录下面 -->
        <mvc:resources mapping="/resources/image/**" location="/resources/static/image/"></mvc:resources>
        <!-- 也可以放到classpath目录下,跟jar一起打包,也可以加载别的jar下面的文件(classpath*或配置通配) -->
        <mvc:resources mapping="/resources/js/**" location="classpath:/js/"></mvc:resources>
        
        <!-- 视图解析器 -->
        <bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    </beans>
    

    <mvc:annotation-driven/>

    注解驱动:注册DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter。在不配置的情况下,如果项目中配置<mvc:default-servlet-handler/>将导致Controller找不到。

    <context:component-scan/>

    那些目录的类被纳入Spring的管理

    静态资源处理

    由于在web.xml中配置了所有请求都被Spring的DispatcherServlet处理,那么静态资源文件:html,js,css,png等不是DispatcherServlet处理范围。需要配置静态资源处理方式。

    1. <mvc:default-servlet-handler/>
      静态资源交由Web的默认servlet,不交给SpringMVC的DispatcherServlet。
    2. <mvc:resources/>
      mvc:resourcest处理方式更灵活,可以将mapping中请求的资源映射到项目具体目录中,也可以外部引用jar中的classpath某个目录。(classpath表示本项目的classpath,classpath* 将所有jar的classpath,也可以用通配符)
     <!-- 可以映射到web根目录下面的某个子目录 -->
    <mvc:resources mapping="/resources/image/**" location="/resources/static/image/"></mvc:resources>
    <!-- 也可以映射到classpath目录的某个子目录 -->
    <mvc:resources mapping="/resources/js/**" location="classpath:/js/"></mvc:resources>
    
    <a href="/resources/image/a.jpeg"> 加载web根目录下面的resources/static/image/a.jpeg
    <script src="/resources/js/fun.js"> 会去加载classpath目录下面的js/fun.js
    
    WX20200524-201703@2x.png

    ViewResolver视图解析器

    InternalResourceViewResolver

    prefix指定了视图资源的前缀,suffix指定视图资源的后缀

    @RequestMapping(value = "/hello")
    public String hello(){
        return "success";
    }
    

    success即为:/WEB-INF/jsp/success.jsp

    相关文章

      网友评论

          本文标题:SpringMVC-基础配置说明

          本文链接:https://www.haomeiwen.com/subject/pjzjahtx.html