0
点赞
收藏
分享

微信扫一扫

【Java从零到架构师第③季】【28】SpringMVC-Servlet的url匹配

船长_Kevin 2022-01-22 阅读 69

持续学习&持续更新中…

守破离


【Java从零到架构师第③季】【28】SpringMVC-Servlet的url匹配

Servlet的url匹配

在这里插入图片描述

*.do

  • 只会匹配以.do为结尾的请求

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>0</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <!-- 匹配所有以.do为结尾的请求 -->
            <!-- 匹配到请求后,就会交给对应的Servlet(这里就是DispatcherServlet)去处理 -->
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    </web-app>
    
    @Controller
    public class DoController {
    //    @RequestMapping(value = "/test.do")
        @RequestMapping(value = "/test/test.do")
        @ResponseBody
        public String test(String username, String password) {
            System.out.println("username : " + username + ", password : " + password);
            return "<h1 style='color:red;'>热爱Coding,热爱Programming。</h1>";
        }
    }
    

/

  • 除了动态资源(例如.jsp文件)以外

  • 会匹配所有请求(静态资源(.css、.js、.png…)、自定义的URL请求、…)

/*

  • 真正意义上的匹配所有请求(动态资源、静态资源、自定义的URL请求、…)
  • 一般用于Filter

总结

  • 可以理解为:/ + *.jsp = /*

Tomcat是如何匹配请求的

在这里插入图片描述

/—DefaultServlet

org.apache.catalina.servlets.DefaultServlet用于处理除过动态资源(例如JSP文件)以外的所有请求,当然会包括静态资源(.css、.png、…)。

也就是说,DefaultServlet就是Tomcat用来处理静态资源的。

DefaultServlet会将静态资源解析处理后,返回(写出)给客户端。

  <!-- The default servlet for all web applications, that serves static resources. -->
    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
		<!-- ... -->
    </servlet>
    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

*.jsp—JspServlet

org.apache.jasper.servlet.JspServlet用于处理以.jsp为结尾的请求。

也就是说,JspServlet就是Tomcat用来处理JSP文件的。

JspServlet可以将JSP文件解析处理后,返回(写出)给客户端。

    <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
		<!-- ... -->
    </servlet>
    <!-- The mappings for the JSP servlet -->
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

新的问题

  • 现在我们已经知道了*.do//*的区别。

  • 也已经知道了Tomcat是如何处理静态资源和JSP文件的。

  • 那么,使用SpringMVC,在平时开发中,我们应该如何去匹配请求呢?

    1. 首先,肯定不能使用/*,因为/*会匹配到所有的请求,当然也会将JSP文件也给匹配到,但是我们的DispatcherServlet却不能够解析处理JSP文件。
    2. 其次,在开发中,我们肯定也不想为每个请求都在后面加上一个.do.action之类的后缀。
    3. 那么,只能使用/来匹配请求了。
  • 问题来了,如果使用/来匹配请求的话,我们知道,/是会匹配到除了JSP文件以外的所有请求的,但是静态资源(.css、.png、*.js、…)的请求怎么办呢?我们的DispatcherServlet也不能够解析这些静态资源呀。

静态资源被拦截的解决方案1

在这里插入图片描述

applicationContext.xml:

<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="programmer.lp"/>
    <!-- annotation-driven 用于保证注解可以被正常使用 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="defaultCharset" value="utf-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 如果上面没有处理字符串响应的话,就需要加上这一句 -->
    <!-- <mvc:annotation-driven/> -->
    <!-- DispatcherServlet没有处理的请求都交回给默认Servlet去处理 -->
    <mvc:default-servlet-handler/>
</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</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>
</web-app>

静态资源被拦截的解决方案2

在这里插入图片描述

	<mvc:annotation-driven/>    
	<!-- **代表所有子路径 -->
    <!-- mapping是请求路径 -->
    <!-- location是静态资源的位置 -->
	<mvc:resources mapping="/assets/**" location="/assets/"/>

参考

小码哥-李明杰: Java从0到架构师③进阶互联网架构师.


本文完,感谢您的关注支持!


举报

相关推荐

0 条评论