1. Start the server and load some configuration files
DispatcherServlet object is created
springmvc.xml is loaded
HelloController object is created
InternalResourceViewResolver View resolver object is created to
enable annotation support for mvc
Corresponding code
Initial website page index.jsp
<%-- Created by IntelliJ IDEA. User: ASUS Date: 2020/4/11 Time: 12:56 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3> Getting started </ h3> <a href="hello">12</a> </body> </html>
springmvc.xml configuration file: enable annotation support, enable annotation bean scanning package, instantiate the view parser object,
<?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"> <!-Open the annotation scanning package-> <context:component-scan base-package="cn.cast"/> <!-View parser object-> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-Turn on SpringMVC framework annotation support-> <mvc:annotation-driven/> </beans>
web.xml configuration file: configure DispatcherServlet (interceptor) and its properties
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-Load configuration file-> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-Load the configuration file when the server starts-> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <!-Intercept all requests-> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
HelloContoller class: used to handle / hello requests
package cn.cast.controller; /** * Controller class */ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { / * * * Request mapping annotation: path = / hello * / hello becomes the request path for this method execution * @return */ @RequestMapping(path = "/hello") public String sayHello(){ System.out.println("Hello SpringMVC"); return "success"; } }
success.jsp: response page returned after processing the request
<%-- Created by IntelliJ IDEA. User: ASUS Date: 2020/4/11 Time: 13:15 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> < h3 > Successful entry </ h3 > </body> </html>
2. Background processing request
Received <a href="hello"> 12 </a> request in the background
-> All requests are intercepted by DispatcherServlet
-> The interceptor calls HelloController's sayHello (already configured to respond to / hello requests) method
-> The method returns "success" after execution
-> Interceptor parses "success" through the view parser, plus prefix and suffix, make the page jump to success.jsp
-> The corresponding package returned is this success.jsp