世界著名程序SpringMVC完整過程
SpringMVC程序?qū)崿F(xiàn)
一、什么是 MVC
MVC 其實是一種架構思想,將軟件按照模型、視圖、控制器來劃分。
- M:是指 Model,就是模型層,指工程中的 JavaBean,作用是處理數(shù)據(jù)。
- V:是指 View,視圖層,指工程中的 html 或 jsp 等頁面,作用是與用戶進行交互,展示數(shù)據(jù)。
- C:是指 Controller,控制層,指工程中的 servlet,作用是接收請求和響應瀏覽器。
關于 M 中的 javabean,可以分為兩類:
- 實體類Bean:專門存儲業(yè)務數(shù)據(jù)的,如 Student、User 等。
- 業(yè)務處理 Bean:指 Service 或 Dao 對象,專門用于處理業(yè)務邏輯和數(shù)據(jù)訪問。
MVC 工作流程
- 用戶通過視圖層發(fā)送請求到服務器
- 在服務器中請求被 Controller 接收
- Controller 調(diào)用相應的Model層處理請求
- 處理完畢將結果返回到 Controller
- Controller再根據(jù)請求處理的結果找到相應的View視圖,渲染數(shù)據(jù)后最終響應給瀏覽器
二、什么是 SpringMVC
SpringMVC 是 Spring 的一個后續(xù)產(chǎn)品,是 Spring 的一個子項目,SpringMVC 是 Spring 為表述層開發(fā)提供的一整套完備的解決方案。
目前業(yè)界普遍選擇了 SpringMVC 作為 Java EE 項目表述層開發(fā)的首選方案。
SpringMVC 的特點
- Spring 家族原生產(chǎn)品,與 IOC 容器等基礎設施無縫對接
- 基于原生的 Servlet,通過了功能強大的前端控制器 DispatcherServlet,對請求和響應進行統(tǒng)一處理
- 表述層各細分領域需要解決的問題全方位覆蓋,提供全面解決方案
- 代碼清新簡潔,大幅度提升開發(fā)效率
- 內(nèi)部組件化程度高,可插拔式組件即插即用,想要什么功能配置相應組件即可
- 性能卓著,尤其適合現(xiàn)代大型、超大型互聯(lián)網(wǎng)項目要求
三、開發(fā)環(huán)境準備
在實現(xiàn)世界著名程序之前需要準備好開發(fā)環(huán)境。
- IDE:idea 2018.3
- 構建工具:maven3.5.4
- 服務器:tomcat8
- Spring版本:5.3.1
四、動手實現(xiàn) Hello world
一起動動手。
1. 創(chuàng)建工程
這里新建一個 project,我這取名叫 springmvc。然后在里面新建一個 module,叫 springmvc-demo1,最終完成下來。
注意,添加 web模塊,是在 File-Project structure 里。
2. 引入依賴
在模塊 springmvc-demo1 下面的 pom.xml 文件里,需要引入相關依賴。
<?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)一進行處理 --> <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í)行會嚴重影響訪問速度 因此需要通過此標簽將啟動控制DispatcherServlet的初始化時間提前到服務器啟動時 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- 設置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,先不著急前后端分離,學習要一步步地來。
4. 編寫請求控制器
由于前端控制器對瀏覽器發(fā)送的請求進行了統(tǒng)一的處理,但是具體的請求有不同的處理過程,因此需要創(chuàng)建處理具體請求的類,即請求控制器。
請求控制器中每一個處理請求的方法成為控制器方法。
因為SpringMVC的控制器由一個POJO(普通的Java類)擔任,因此需要通過 @Controller 注解將其標識為一個控制層組件,交給 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注解:處理請求和控制器方法之間的映射關系 // @RequestMapping注解的value屬性可以通過請求地址匹配請求,/表示的當前工程的上下文路徑 @RequestMapping(value = "/") public String index() { // 返回視圖名稱 return "index"; } @RequestMapping("/target") public String toTarget() { return "target"; } }
這里我直接加了 2 個方法,對應兩個頁面。
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" >訪問目標頁面 target.html </a> </body> </html>
這里加了一個<a>標簽,用來跳轉(zhuǎn)到另一個頁面 target.html。里面的th:href="@{/target}用的是 thymeleaf 里的語法,不用太過糾結與此,這不是本次學習的重點。因為后面最終還是會用前后端分離的方式進行應用的開發(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. 啟動項目
因為項目打包方式是 war 包,部署在 tomcat 里,所以要先在本地安裝個 tomcat,教程網(wǎng)上一大把。
然后,在idea 中 add run configuration。
先是 Deployment。
這里的 Application context 就是應用上下文了,比如我要訪問 /target,實際項目啟動后在瀏覽器中訪問的是:http://localhost:8080/springmvc/target 。
接下來,就是 Server 配置了。
啟動,可以run 也可以 debug,debug 下可以用來斷點調(diào)試。
啟動之后,默認會打開 http://localhost:8080/springmvc/。
點擊 index 頁的 跳轉(zhuǎn)連接,成功跳轉(zhuǎn)到 target 頁。
再回顧下請求頁面的過程:
- 瀏覽器發(fā)送請求,若請求地址符合前端控制器的 url-pattern,該請求就會被前端控制器DispatcherServlet處理。
- 前端控制器會讀取 SpringMVC 的核心配置文件,通過掃描組件找到控制器,將請求地址和控制器中 @RequestMapping 注解的 value 屬性值進行匹配,若匹配成功,該注解所標識的控制器方法就是處理請求的方法。
- 處理請求的方法需要返回一個字符串類型的視圖名稱,該視圖名稱會被視圖解析器解析,加上前綴和后綴組成視圖的路徑,通過 Thymeleaf 對視圖進行渲染,最終轉(zhuǎn)發(fā)到視圖所對應頁面。
感謝《尚硅谷》的學習資源。
以上就是世界著名程序SpringMVC完整過程的詳細內(nèi)容,更多關于SpringMVC程序?qū)崿F(xiàn)的資料請關注腳本之家其它相關文章!
相關文章
SSM+微信小程序?qū)崿F(xiàn)物業(yè)管理系統(tǒng)及實例代碼
這篇文章主要介紹了SSM+微信小程序?qū)崿F(xiàn)物業(yè)管理系統(tǒng),ssm微信小程序物業(yè)管理系統(tǒng),有網(wǎng)站后臺管理系統(tǒng),本文通過實例代碼給大家展示系統(tǒng)的功能,需要的朋友可以參考下2022-02-02spring-redis-session 自定義 key 和過期時間
這篇文章主要介紹了spring-redis-session 自定義 key 和過期時間,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12