世界著名程序SpringMVC完整過程
SpringMVC程序?qū)崿F(xiàn)
一、什么是 MVC
MVC 其實(shí)是一種架構(gòu)思想,將軟件按照模型、視圖、控制器來劃分。
- M:是指 Model,就是模型層,指工程中的 JavaBean,作用是處理數(shù)據(jù)。
- V:是指 View,視圖層,指工程中的 html 或 jsp 等頁面,作用是與用戶進(jìn)行交互,展示數(shù)據(jù)。
- C:是指 Controller,控制層,指工程中的 servlet,作用是接收請求和響應(yīng)瀏覽器。
關(guān)于 M 中的 javabean,可以分為兩類:
- 實(shí)體類Bean:專門存儲業(yè)務(wù)數(shù)據(jù)的,如 Student、User 等。
- 業(yè)務(wù)處理 Bean:指 Service 或 Dao 對象,專門用于處理業(yè)務(wù)邏輯和數(shù)據(jù)訪問。
MVC 工作流程
- 用戶通過視圖層發(fā)送請求到服務(wù)器
- 在服務(wù)器中請求被 Controller 接收
- Controller 調(diào)用相應(yīng)的Model層處理請求
- 處理完畢將結(jié)果返回到 Controller
- Controller再根據(jù)請求處理的結(jié)果找到相應(yīng)的View視圖,渲染數(shù)據(jù)后最終響應(yīng)給瀏覽器
二、什么是 SpringMVC
SpringMVC 是 Spring 的一個后續(xù)產(chǎn)品,是 Spring 的一個子項目,SpringMVC 是 Spring 為表述層開發(fā)提供的一整套完備的解決方案。
目前業(yè)界普遍選擇了 SpringMVC 作為 Java EE 項目表述層開發(fā)的首選方案。
SpringMVC 的特點(diǎn)
- Spring 家族原生產(chǎn)品,與 IOC 容器等基礎(chǔ)設(shè)施無縫對接
- 基于原生的 Servlet,通過了功能強(qiáng)大的前端控制器 DispatcherServlet,對請求和響應(yīng)進(jìn)行統(tǒng)一處理
- 表述層各細(xì)分領(lǐng)域需要解決的問題全方位覆蓋,提供全面解決方案
- 代碼清新簡潔,大幅度提升開發(fā)效率
- 內(nèi)部組件化程度高,可插拔式組件即插即用,想要什么功能配置相應(yīng)組件即可
- 性能卓著,尤其適合現(xiàn)代大型、超大型互聯(lián)網(wǎng)項目要求
三、開發(fā)環(huán)境準(zhǔn)備
在實(shí)現(xiàn)世界著名程序之前需要準(zhǔn)備好開發(fā)環(huán)境。
- IDE:idea 2018.3
- 構(gòu)建工具:maven3.5.4
- 服務(wù)器:tomcat8
- Spring版本:5.3.1
四、動手實(shí)現(xiàn) Hello world
一起動動手。
1. 創(chuàng)建工程
這里新建一個 project,我這取名叫 springmvc。然后在里面新建一個 module,叫 springmvc-demo1,最終完成下來。

注意,添加 web模塊,是在 File-Project structure 里。

2. 引入依賴
在模塊 springmvc-demo1 下面的 pom.xml 文件里,需要引入相關(guān)依賴。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springmvc</artifactId>
<groupId>com.pingguo.mvc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springmvc-demo1</artifactId>
<!--打包方式-->
<packaging>war</packaging>
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring5和Thymeleaf整合包 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
</project>3. 配置 web.xml
在 webapp 下的 web.xml 中如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置SpringMVC的前端控制器,對瀏覽器發(fā)送的請求統(tǒng)一進(jìn)行處理 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通過初始化參數(shù)指定SpringMVC配置文件的位置和名稱 -->
<init-param>
<!-- contextConfigLocation為固定值 -->
<param-name>contextConfigLocation</param-name>
<!-- 使用classpath:表示從類路徑查找配置文件,例如maven工程中的src/main/resources -->
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--
作為框架的核心組件,在啟動過程中有大量的初始化操作要做
而這些操作放在第一次請求時才執(zhí)行會嚴(yán)重影響訪問速度
因此需要通過此標(biāo)簽將啟動控制DispatcherServlet的初始化時間提前到服務(wù)器啟動時
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!--
設(shè)置springMVC的核心控制器所能處理的請求的請求路徑
/所匹配的請求可以是/login或.html或.js或.css方式的請求路徑
但是/不能匹配.jsp請求路徑的請求
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>3. 配置 spring 配置文件
在 resources 下新建 配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自動掃描包 -->
<context:component-scan base-package="com.pingguo.mvc.controller"></context:component-scan>
<!-- 配置Thymeleaf視圖解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 視圖前綴 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 視圖后綴 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>這里視圖層使用 Thymeleaf,先不著急前后端分離,學(xué)習(xí)要一步步地來。
4. 編寫請求控制器
由于前端控制器對瀏覽器發(fā)送的請求進(jìn)行了統(tǒng)一的處理,但是具體的請求有不同的處理過程,因此需要創(chuàng)建處理具體請求的類,即請求控制器。
請求控制器中每一個處理請求的方法成為控制器方法。
因?yàn)镾pringMVC的控制器由一個POJO(普通的Java類)擔(dān)任,因此需要通過 @Controller 注解將其標(biāo)識為一個控制層組件,交給 Spring 的 IoC容器管理,此時 SpringMVC 才能夠識別控制器的存在。
package com.pingguo.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
// @RequestMapping注解:處理請求和控制器方法之間的映射關(guān)系
// @RequestMapping注解的value屬性可以通過請求地址匹配請求,/表示的當(dāng)前工程的上下文路徑
@RequestMapping(value = "/")
public String index() {
// 返回視圖名稱
return "index";
}
@RequestMapping("/target")
public String toTarget() {
return "target";
}
}這里我直接加了 2 個方法,對應(yīng)兩個頁面。
5. 編寫頁面文件
在 webapp/WEB-INF/templates 下,編寫 html 文件。
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<h1>Hello World</h1>
<a th:href="@{/target}" rel="external nofollow" >訪問目標(biāo)頁面 target.html </a>
</body>
</html>這里加了一個<a>標(biāo)簽,用來跳轉(zhuǎn)到另一個頁面 target.html。里面的th:href="@{/target}用的是 thymeleaf 里的語法,不用太過糾結(jié)與此,這不是本次學(xué)習(xí)的重點(diǎn)。因?yàn)楹竺孀罱K還是會用前后端分離的方式進(jìn)行應(yīng)用的開發(fā)。
target.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>target</title>
</head>
<body>
<h1>target</h1>
</body>
</html>6. 啟動項目
因?yàn)轫椖看虬绞绞?war 包,部署在 tomcat 里,所以要先在本地安裝個 tomcat,教程網(wǎng)上一大把。
然后,在idea 中 add run configuration。
先是 Deployment。

這里的 Application context 就是應(yīng)用上下文了,比如我要訪問 /target,實(shí)際項目啟動后在瀏覽器中訪問的是:http://localhost:8080/springmvc/target 。
接下來,就是 Server 配置了。

啟動,可以run 也可以 debug,debug 下可以用來斷點(diǎn)調(diào)試。

啟動之后,默認(rèn)會打開 http://localhost:8080/springmvc/。

點(diǎn)擊 index 頁的 跳轉(zhuǎn)連接,成功跳轉(zhuǎn)到 target 頁。

再回顧下請求頁面的過程:
- 瀏覽器發(fā)送請求,若請求地址符合前端控制器的 url-pattern,該請求就會被前端控制器DispatcherServlet處理。
- 前端控制器會讀取 SpringMVC 的核心配置文件,通過掃描組件找到控制器,將請求地址和控制器中 @RequestMapping 注解的 value 屬性值進(jìn)行匹配,若匹配成功,該注解所標(biāo)識的控制器方法就是處理請求的方法。
- 處理請求的方法需要返回一個字符串類型的視圖名稱,該視圖名稱會被視圖解析器解析,加上前綴和后綴組成視圖的路徑,通過 Thymeleaf 對視圖進(jìn)行渲染,最終轉(zhuǎn)發(fā)到視圖所對應(yīng)頁面。
感謝《尚硅谷》的學(xué)習(xí)資源。
以上就是世界著名程序SpringMVC完整過程的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC程序?qū)崿F(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SSM+微信小程序?qū)崿F(xiàn)物業(yè)管理系統(tǒng)及實(shí)例代碼
這篇文章主要介紹了SSM+微信小程序?qū)崿F(xiàn)物業(yè)管理系統(tǒng),ssm微信小程序物業(yè)管理系統(tǒng),有網(wǎng)站后臺管理系統(tǒng),本文通過實(shí)例代碼給大家展示系統(tǒng)的功能,需要的朋友可以參考下2022-02-02
spring-redis-session 自定義 key 和過期時間
這篇文章主要介紹了spring-redis-session 自定義 key 和過期時間,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
springboot在服務(wù)器上的幾種啟動方式(小結(jié))
這篇文章主要介紹了springboot在服務(wù)器上的幾種啟動方式(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
5分鐘教你使用java搞定網(wǎng)站登錄驗(yàn)證碼
這篇文章主要為大家介紹了使用java搞定網(wǎng)站登錄驗(yàn)證碼的快速實(shí)現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

