SpringMVC的应用小结
reference:
爱上狂神说
1.中心控制器
Spring的web框架围绕DispatcherServlet设计。DispatcherServlet的作用是将请求分发到不同的处理器。DispatcherServlet是一个实际的Servlet (它继承自HttpServlet 基类)。这里我借用狂神两张图🐶
- 1.DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心。用户发出请求,DispatcherServlet接收请求并拦截请求。
我们假设请求的url为 : http://localhost:8080/SpringMVC/hello
如上url拆分成三部分:
http://localhost:8080服务器域名
SpringMVC部署在服务器上的web站点
hello表示控制器
通过分析,如上url表示为:请求位于服务器localhost:8080上的SpringMVC站点的hello控制器。
- 2.HandlerMapping为处理器映射。DispatcherServlet调用HandlerMapping,HandlerMapping根据请求url查找Handler。
- 3.HandlerExecution表示具体的Handler,其主要作用是根据url查找控制器,如上url被查找控制器为:hello。
- 4.HandlerExecution将解析后的信息传递给DispatcherServlet,如解析控制器映射等。
- 5.HandlerAdapter表示处理器适配器,其按照特定的规则去执行Handler。
- 6.Handler让具体的Controller执行。
- 7.Controller将具体的执行信息返回给HandlerAdapter,如ModelAndView。
- 8.HandlerAdapter将视图逻辑名或模型传递给DispatcherServlet。
- 9.DispatcherServlet调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名。
- 10.视图解析器将解析的逻辑视图名传给DispatcherServlet。
- 11.DispatcherServlet根据视图解析器解析的视图结果,调用具体的视图。
- 12.最终视图呈现给用户。
2.SpringMVC注解版项目创建
-
0.必要依赖要导入
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
-
1.在
pom.xml
文件中更换镜像源<!-- 配置阿里云仓库 --> <repositories> <repository> <id>aliyun-repos</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-repos</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories>
-
2.在pom.xml中配置资源过滤
<!-- 导入资源过滤 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
-
3.配置
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> <!--1.配置DispatcherServlet是springmvc的核心 请求分发器/前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--3.DispatcherServlet要绑定springmvc的配置文件--> <init-param> <!-- 参数说明 contextConfigLocation: 根据路径来匹配 --> <param-name>contextConfigLocation</param-name> <!-- 这个配置文件放在resources下 classpath: 当前类的路径 --> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--启动级别设定为1:服务器一起动就跟着启动 启动顺序,数字越小,启动越早 --> <load-on-startup>1</load-on-startup> </servlet> <!--2.注册完DispatcherServlet类之后去注册其请求 所有请求都会被springmvc拦截 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 这里写/就完事了 "/": 只会匹配请求,不会匹配jsp页面 "/*": 匹配所有请求,包括jsp页面 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
4.添加SpringMVC配置文件:在recourse目录下创建
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns