<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
<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-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
自定义SpringMVC的配置文件:springmvc.xml
<?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">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.ly"/>
<!-- 视图解析器对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--开启SpringMVC框架注解的支持-->
<mvc:annotation-driven conversion-service="conversionService"/>
代码测试:点击连接后执行测试方法,并跳转到成功页面
控制器类
/**
-
@Author: Ly
-
@Date: 2020-09-16 11:20
-
控制器类
*/
@Controller
public class HelloController {
@RequestMapping(path="/hello")
public String seyHello(){
System.out.println("Hello StringMVC");
return "success";
}
}
测试界面:
<%@ page language=“java” contentType=“text/html;charset=UTF-8” pageEncoding=“utf-8” %>
<title>入门程序</title>
<a href="hello">入门程序</a>
成功后的跳转界面:
<%–
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/9/16
Time: 11:28
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” isELIgnored=“false” %>
<title>Title</title>
success
[](
)执行原理分析
-------------------------------------------------------------------
Spring MVC主要组成部分:
* 前端控制器(DispatcherServlet)
* 请求到处理器映射(HandlerMapping)
* 处理器适配器(HandlerAdapter)
* 视图解析器(ViewResolver)
* 处理器或页面控制器(Controller)
* 验证器( Validator)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201024101052748.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0x6eTQxMDk5Mg==,size_16,color_FFFFFF,t_70#pic_left)
先看一下我们Spring MVC应用的搭建过程:
* 步骤1:新建Maven项目,设置项目名,配置Tomcat,在pom中进行导包
* 步骤2:在webapp下面的WEB-INF中配置web.xml
* 步骤3:配置完web.xml文件后,在src/main/resources下新建XML文件spring-mvc.xml,配置了注解支持与视图解析器
* 步骤4:基本配置过后,新建Java类,HelloController,并添加注解
* 步骤5:最后新建jsp文件,测试代码
当启动Tomcat服务器的时候,因为配置了load-on-startup标签,项目会加载springmvc.xml配置文件,创建DispatcherServlet对象,之后开启了注解扫描,创建了HelloController对象。在前端页面中用户点击连接发送请求时,请求会先到达DispatcherServlet核心控制器,根据配置@RequestMapping注解找到执行的具体方法,并根据执行方法的返回值,再根据配置的视图解析器,去指定的目录下查找指定名称的JSP文件,最后服务器做出响应渲染视图,显示跳转后的页面。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201024103924701.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0x6eTQxMDk5Mg==,size_16,color_FFFFFF,t_70#pic_left)
**DispatcherServlet:前端控制器**
DispatcherServlet是整个Spring MVC的核心,同时也是整个流程的控制中心。它负责接收HTTP请求组织协调SpringMVC的各个组成部分。其存在降低了组件之间的耦合性,其主要工作有以下三项:
* 截获符合特定格式的URL请求。
* 初始化DispatcherServlet上下文对应WebApplicationContext,并将其与业务层、持久化层的WebApplicationContext建立关联。
* 初始化Spring MVC的各个组成组件,并装配到DispatcherServlet中。
# 最后
**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](
)**
![](https://img-blog.csdnimg.cn/img_convert/71286461e55fb91d5a4fb2b208a69003.png)
![](https://img-blog.csdnimg.cn/img_convert/801bde00a1f20b10252e5a8800f1960b.png)
RL请求。
* 初始化DispatcherServlet上下文对应WebApplicationContext,并将其与业务层、持久化层的WebApplicationContext建立关联。
* 初始化Spring MVC的各个组成组件,并装配到DispatcherServlet中。
# 最后
**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](
)**
[外链图片转存中...(img-7hNYHGSO-1631183622730)]
[外链图片转存中...(img-eBp1rkdn-1631183622732)]
![](https://img-blog.csdnimg.cn/img_convert/75d03bc41b86865b384048958c2070a0.png)