- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.28version>
- dependency>
配置前端控制器DispactherServlet
- web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
-
- <web-app>
- <servlet>
- <servlet-name>DispatcherServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:SpringMVC.xmlparam-value>
- init-param>
- <load-on-startup>2load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>DispatcherServletservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
创建Controller类,配置映射路径,同时讲控制类交给Spring容器
- package com.example.Controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- public class Controller01 {
- @RequestMapping("/show")
- public String show() {
- System.out.println("show()...");
- // 返回视图路径
- return "/index.jsp";
- }
- }
组件 | 描述 | 常用组件 |
处理器映射器:HandlerMapping | 匹配映射路径对应的Handler,返回可执行的处理器链对象HandlerExecutionChain对象 | RequestMappingHandlerMapping |
处理适配器:HandlerAdapter | 匹配HandlerExecutionChain对应的适配器进行处理器调用,返回视图模型对象 | RequestMappingHandlerAdapter |
视图解析器:ViewResolver | 对视图模型对象进行解析 | InternalResourceViewResolver |