Chapter05 URL pattern (about URL pattern interception mechanism in servle t)

Posted by m!tCh on Sun, 12 Dec 2021 12:28:34 +0100

catalogue

About URL pattern

1. Request index jsp

2.web.xml

3. MyController class

4. When the page is loaded. If the "/" static resource cannot be loaded, you need to specify the location of the static resource or add an annotation in MVC config

Corresponding to two spring MVC config xml

Corresponding static resource directory structure

About URL pattern

When using the framework, URL pattern can use two values
 1. Use extension method, syntax * xxxx, xxxx is the extension of the customization file. Commonly used methods are * do,*. action,*. mvc, etc
  http://locatlhost:8080/myweb/some.do
  http://locatlhost:8080/myweb/other.do
 2. Use slash "/"
  When / is used in your project, it will replace the default in tomcat,
  As a result, all static resources are processed by DispatcherServlet. By default, DispatcherServlet has no ability to process static resources.
  No controller object can handle access to static resources. So static resources (html, js, pictures, css are 404)

  Dynamic resource some Do can be accessed because our program is exclusive to the MyController controller, which can handle some Do request

1. Request index jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-3.6.0.js"></script>
</head>
<body>

    <form action="some" method="post">
        full name:<input type="text" name="name"/><br/>
        Age:<input type="text" name="age"/><br/>
        <input type="submit" value="Submit parameters"/>
    </form>
    <br/>
    <img src="static/images/3.jpg" title="I am a static resource and cannot be displayed"/>
</body>
</html>

2.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">

    <!--Declaration, registration springmvc Core object of DispatcherServlet
        Need in tomcat After the server starts, create DispatcherServlet example.
        Why create DispatcherServlet What about instances of objects?
        because DispatcherServlet During the creation process, it will be created at the same time springmvc Container object,
        read springmvc Create all the objects in the configuration file when the user initiates a request
        You can use objects directly.


        servlet The initialization of is performed init()method. DispatcherServlet stay init()in{
            //Create a container and read the configuration file
            WebApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc.xml");
            //Put the container object in the ServletContext
            getServletContext().setAttribute(key,ctx);
        }

        start-up tomcat Error, read this file /WEB-INF/springmvc-servlet.xml(/WEB-INF/myweb-servlet.xml)
        springmvc When creating a container object, the configuration file read is by default /WEB-INF/<servlet-name>-servlet.xml
    -->
    <servlet>
        <servlet-name>myweb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!--custom springmvc The location of the read configuration file-->
        <init-param>
            <!--springmvc Properties of the location of the configuration file-->
            <param-name>contextConfigLocation</param-name>
            <!--Specify the location of the customization file-->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>

        <!--stay tomcat After startup, create Servlet object
            load-on-startup: express tomcat The order in which objects are created after startup. Its value is an integer. The smaller the value is,
                                tomcat The earlier the object is created. Integer greater than or equal to 0
        -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>myweb</servlet-name>
        <!--
            When using frames, url-pattern Two values can be used
            1.Use extension mode, syntax *.xxxx ,xxxx Is the extension of the customization file. Common ways are *.do,*.action,*.mvc wait
              http://locatlhost:8080/myweb/some.do
              http://locatlhost:8080/myweb/other.do
            2.Use slash "/"
              When used in your project / ,It will replace tomcat Medium default,
              As a result, all static resources are given DispatcherServlet Processing, by default DispatcherServlet No ability to handle static resources.
              No controller object can handle access to static resources. So static resources( html,js,Pictures, css All 404)

              dynamic resource some.do It can be accessed because there are in our program MyController The controller is exclusive and can handle some.do request
        -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--Register claim filter, resolve post The request is garbled-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--Sets the character encoding used in the project-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <!--Force request object( HttpServletRequest)use encoding Encoded value-->
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <!--Forced response object( HttpServletResponse)use encoding code-->
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <!--
            /* : Means to force all requests to be processed through the filter first.
        -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

3. MyController class

package edu.tjdz.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @RequestMapping:
 *      value : The common part of all request addresses is called the module name
 *      Location: above the class
 */
@Controller
public class MyController {

    @RequestMapping("/some")
    public ModelAndView doSome(String name,Integer age){
        ModelAndView mv = new ModelAndView();
        mv.addObject("myname",name);
        mv.addObject("myage",age);
        mv.setViewName("show");
        return mv;
    }

}

4. During page loading, if the "/" static resource cannot be loaded, you need to specify the location of the static resource or add an annotation in MVC config

<! -- the first way to handle static resource access:
    You need to add < MVC: default servlet handler > to the spring MVC configuration file
    The principle is: after adding this tag, the framework will create the controller object DefaultServletHttpRequestHandler (similar to the MyController we created ourselves)
    The DefaultServletHttpRequestHandler object can forward accepted requests to tomcat's default servlet.
-->
<mvc:default-servlet-handler/>
<! -- the second way to handle static resources
    MVC: after resources is added, Guangjia will create the processor object ResourceHttpRequestHandler.
    Let this exclusive handle access to static resources without relying on the tomcat server.
    mapping: the url address for accessing static resources, using**
    Location: the directory location of the static resource in your project

    img: * *: indicates images/p1.jpg, images/user/logo.gif, images/order/landscape/list.png
-->
<!--<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/js/**" location="/js/"/>-->
<mvc:resources mapping="/static/**" location="/static/"/>
<! -- MVC: resources conflicts with @ RequestMapping -- >
<mvc:annotation-driven/>

Corresponding to two types of springmvc-config.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">

    <!--Declaration component scanner-->
    <context:component-scan base-package="edu.tjdz.controller"/>

    <!--statement springmvc The view parser in the framework helps developers set the path of view files-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--prefix : Path to view file-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!--suffix : The extension of the view file-->
        <property name="suffix" value=".jsp"/>
    </bean>

   <!--default-servlet-handler and @RequestMapping Comments conflict, need to be added annotation-driver-->
    <mvc:annotation-driven />

    <!--The first way to handle static resource access:
        Need in springmvc Add to configuration file <mvc:default-servlet-handler>
        The principle is : After adding this tag, the framework creates a controller object DefaultServletHttpRequestHandler(Similar to what we created ourselves MyController)
        DefaultServletHttpRequestHandler This object can forward accepted requests to tomcat of default this servlet. 
    -->
    <mvc:default-servlet-handler/>
</beans>
<?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">

    <!--Declaration component scanner-->
    <context:component-scan base-package="edu.tjdz.controller"/>

    <!--statement springmvc The view parser in the framework helps developers set the path of view files-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--prefix : Path to view file-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!--suffix : The extension of the view file-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--The second way to handle static resources
        mvc:resources After adding, the light armor will be created ResourceHttpRequestHandler This processor object.
        Let this exclusive handle access to static resources without dependency tomcat The server.
        mapping : Access to static resources url Address, using **
        location : Directory location of static resources in your project

        img:**:express images/p1.jpg,images/user/logo.gif,images/order/landscape/list.png
    -->
    <!--<mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/html/**" location="/html/" />
    <mvc:resources mapping="/js/**" location="/js/"/>-->
    <mvc:resources mapping="/static/**" location="/static/"/>
    <!--mvc:resources and@RequestMapping There is a certain conflict-->
    <mvc:annotation-driven/>

    <!--Use a configuration statement to specify multiple static resource accesses-->

</beans>

Corresponding static resource directory structure

 

 

Topics: html elementUI webview