Spring MVC 复盘 | 工作原理及配置详解

简介: Spring MVC 复盘 | 工作原理及配置详解

1、Sping MVC 工作原理


旧文提过,不再赘述。请务必通读以下文章:


https://mp.weixin.qq.com/s/z-fhmDa9iBwSG6OJx1x5hw


2、IDEA 创建 web 项目


640.jpg

640.png

640.png


项目配置:

640.jpg

640.jpg

640.jpg

640.jpg

640.png


详细配置见:


https://www.cnblogs.com/shuaishuai1993/p/9795227.html


3、Spring MVC Hello World


IDEA 创建项目并配置:


  • 创建一个名为 mvc-hello 的动态 Web 项目,并在创建的项目中的 src 文件夹下创建一个包 com.nasus.hello。
  • 加入 web 相关 pom 依赖。
  • 在 com.nasus.hello 包下创建一个 HelloController 类。
  • 在  webapp/WEB-INF 文件夹下创建 Spring 配置文件 web.xml 和 hello-servlet.xml。
  • 在 webapp/WEB-INF 文件夹下创建一个名为 jsp 的子文件夹。
  • 在此子文件夹下创建视图文件 hello.jsp。


640.png


pom 依赖


1        <!-- 1.Spring核心依赖 -->
 2        <dependency>
 3            <groupId>org.springframework</groupId>
 4            <artifactId>spring-core</artifactId>
 5            <version>5.0.7.RELEASE</version>
 6        </dependency>
 7
 8        <dependency>
 9            <groupId>org.springframework</groupId>
10            <artifactId>spring-beans</artifactId>
11            <version>5.0.7.RELEASE</version>
12        </dependency>
13
14        <dependency>
15            <groupId>org.springframework</groupId>
16            <artifactId>spring-context</artifactId>
17            <version>5.0.7.RELEASE</version>
18        </dependency>
19
20        <dependency>
21            <groupId>org.springframework</groupId>
22            <artifactId>spring-aop</artifactId>
23            <version>5.0.7.RELEASE</version>
24        </dependency>
25
26        <!-- 3.Spring web依赖 -->
27        <dependency>
28            <groupId>org.springframework</groupId>
29            <artifactId>spring-web</artifactId>
30            <version>5.0.7.RELEASE</version>
31        </dependency>
32
33        <dependency>
34            <groupId>org.springframework</groupId>
35            <artifactId>spring-webmvc</artifactId>
36            <version>5.0.7.RELEASE</version>
37        </dependency>


HelloController 类:


1@Controller
 2@RequestMapping("/hello")
 3public class HelloController {
 4
 5    @RequestMapping(method = RequestMethod.GET)
 6    public String sayHello(ModelMap model) {
 7        model.addAttribute("data", "Hello World");
 8        return "hello";
 9    }
10
11}


HelloController 指定了映射路径,以及 http 访问方式。并返回一个 ModelMap  和 hello 以供视图解析器解析。


web.xml


1<web-app id="WebApp_ID" version="2.4"
 2         xmlns="http://java.sun.com/xml/ns/j2ee"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 5   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 6
 7  <display-name>Spring MVC Application</display-name>
 8
 9  <servlet>
10    <servlet-name>hello</servlet-name>
11    <servlet-class>
12      org.springframework.web.servlet.DispatcherServlet
13    </servlet-class>
14    <!-- 加载顺序 -->
15    <load-on-startup>1</load-on-startup>
16  </servlet>
17
18  <servlet-mapping>
19    <servlet-name>hello</servlet-name>
20    <url-pattern>/</url-pattern>
21  </servlet-mapping>
22
23</web-app>


web.xml 定义一个前端控制器 DispatcherServlet ,servlet-name 为 hello 。url-pattern 标签定义这个 servlet 将拦截所有的 url ,而 SpringMVC 默认将会从 webapp/WEB-INF 文件夹下加载 servlet-name 标签指定的名称 + -servlet.xml 格式的 servlet 配置文件。这里加载的 servlet 配置文件正是 hello-servlet.xml 。


hello-servlet.xml:


1<beans xmlns="http://www.springframework.org/schema/beans"
 2       xmlns:context="http://www.springframework.org/schema/context"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4       xsi:schemaLocation="
 5   http://www.springframework.org/schema/beans
 6   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7   http://www.springframework.org/schema/context
 8   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9
10    <!-- 自动扫描 -->
11    <context:component-scan base-package="com.nasus.hello" />
12
13    <!-- 视图解析器 -->
14    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15        <property name="prefix" value="/WEB-INF/jsp/" />
16        <property name="suffix" value=".jsp" />
17    </bean>
18
19</beans>


做了两件事,一是指定这个 servlet 自动扫描哪些包,并激活带 web 相关注解,@Controller、@RequestMapping 的类。


InternalResourceViewResolver 是视图解析器,它定义了解析视图名称的规则。根据上面定义的规则,hello 的逻辑视图将委托给位于 /WEB-INF/jsp/hello.jsp 这个视图来实现。


hello.jsp


1<%@ page contentType="text/html; charset=UTF-8" %>
2<html>
3<head>
4    <title>Hello World</title>
5</head>
6<body>
7    <h2>${data}</h2>
8</body>
9</html>


HelloController 返回的视图名称 "hello",匹配到了 hello.jsp 。同时,hello.jsp 还取得返回的 model 中的内容。最后再由 DispatcherServlet 渲染视图,返回浏览器,呈现给用户。


访问 http://127.0.0.1:8080/hello](http://127.0.0.1:8080/hello ,效果如下:


640.png


4、一些 Bug


IDEA 使用 tomcat 中文乱码问题,解决方法:


https://blog.csdn.net/u012611878/article/details/80723491


IDEA 编译时出现,javacTask: 源发行版 1.8 需要目标发行版 1.8,解决方法:


https://www.cnblogs.com/wormday/p/8424855.html


https://www.cnblogs.com/benjieqiang/p/11298294.html


5、源码地址


https://github.com/turoDog/review_spring


本文主要复盘了 Spring MVC 的工作原理,创建了一个 Spring MVC 项目来辅助理解原理,具体更多 Spring MCV 的用法,请见后续文章。

相关文章
|
16天前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
104 26
|
2月前
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
183 73
|
19天前
|
SQL Java 数据库连接
对Spring、SpringMVC、MyBatis框架的介绍与解释
Spring 框架提供了全面的基础设施支持,Spring MVC 专注于 Web 层的开发,而 MyBatis 则是一个高效的持久层框架。这三个框架结合使用,可以显著提升 Java 企业级应用的开发效率和质量。通过理解它们的核心特性和使用方法,开发者可以更好地构建和维护复杂的应用程序。
110 29
|
29天前
|
监控 Java 数据库连接
Spring c3p0配置详解
在Spring项目中配置C3P0数据源,可以显著提高数据库连接的效率和应用程序的性能。通过合理的配置和优化,可以充分发挥C3P0的优势,满足不同应用场景的需求。希望本文的详解和示例代码能为开发者提供清晰的指导,帮助实现高效的数据库连接管理。
50 10
|
2月前
|
Java Spring
【Spring配置相关】启动类为Current File,如何更改
问题场景:当我们切换类的界面的时候,重新启动的按钮是灰色的,不能使用,并且只有一个Current File 项目,下面介绍两种方法来解决这个问题。
|
2月前
|
Java Spring
【Spring配置】idea编码格式导致注解汉字无法保存
问题一:对于同一个项目,我们在使用idea的过程中,使用汉字注解完后,再打开该项目,汉字变成乱码问题二:本来a项目中,汉字注解调试好了,没有乱码了,但是创建出来的新的项目,写的注解又成乱码了。
|
2月前
|
Java Spring
【Spring配置】创建yml文件和properties或yml文件没有绿叶
本文主要针对,一个项目中怎么创建yml和properties两种不同文件,进行配置,和启动类没有绿叶标识进行解决。
|
2月前
|
NoSQL Java Redis
Spring Boot 自动配置机制:从原理到自定义
Spring Boot 的自动配置机制通过 `spring.factories` 文件和 `@EnableAutoConfiguration` 注解,根据类路径中的依赖和条件注解自动配置所需的 Bean,大大简化了开发过程。本文深入探讨了自动配置的原理、条件化配置、自定义自动配置以及实际应用案例,帮助开发者更好地理解和利用这一强大特性。
153 14
|
2月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
74 6
|
2月前
|
XML Java 数据格式
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
164 3