SpringMVC MVC架構(gòu)與Servlet使用詳解
一、MVC架構(gòu)
1、MVC是什么
- MVC是模型Model、視圖View和控制器Controller的簡稱,是一種架構(gòu)規(guī)范
- 降低了業(yè)務(wù)邏輯與視圖之間的雙向耦合
2、MVC三層的主要構(gòu)成
- Model(模型):包括數(shù)據(jù)和業(yè)務(wù),主要是Service和Dao
- View(視圖):負(fù)責(zé)模型的展示,即用戶看到的界面,例如JSP
- Controller(控制器):接收請求,委托給model進(jìn)行處理,等到處理完畢之后,將數(shù)據(jù)模型返回給視圖,由視圖負(fù)責(zé)展示,也就是說他起到一個中間人的作用,例如Servlet
3、MVC框架的作用
- 將url映射到j(luò)ava類或java類的方法
- 封裝用戶提交的數(shù)據(jù)
- 處理請求–調(diào)用相關(guān)的業(yè)務(wù)處理–封裝響應(yīng)數(shù)據(jù)
- 將響應(yīng)的數(shù)據(jù)進(jìn)行渲染 . jsp / html 等表示層數(shù)據(jù)
二、回顧Servlet
1、什么是servlet
Servlet 是指任何實現(xiàn)了這個 Servlet 接口的類,它解決了當(dāng)瀏覽器發(fā)送請求到服務(wù)器時,服務(wù)器按照請求尋找哪個Servlet類下的代碼,怎么執(zhí)行的問題
了解一下重定向和轉(zhuǎn)發(fā)的異同點:
相同點:頁面都會實現(xiàn)跳轉(zhuǎn)
不同點:轉(zhuǎn)發(fā)的地址欄url不會變,重定向會變
2、簡單的servlet實例
首先我們需要在父工程的maven依賴中導(dǎo)入
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.18</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>
接著我們建立一個Moudle:springmvc-01-servlet , 右鍵添加Web app的支持
然后我們新建一個MyServlet,繼承HttpServlet (實際上還是實現(xiàn)了Servlet這個接口 )
package com.decade.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; // 實現(xiàn)servlet接口 public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 獲取請求參數(shù) String method = req.getParameter("method"); if ("add".equals(method)) { req.getSession().setAttribute("msg", "執(zhí)行了add方法"); } else if ("delete".equals(method)) { req.getSession().setAttribute("msg", "執(zhí)行了delete方法"); } // 視圖跳轉(zhuǎn) req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req, resp); // 重定向使用rsp.sendRedirect("/index.jsp"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
然后編寫test.jsp,在WEB-INF目錄下新建一個jsp的文件夾,新建test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
接著我們需要在web.xml中注冊一下servlet,指定服務(wù)啟動展示的首頁以及session過期時間
<?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"> <!-- 配置servlet --> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.decade.servlet.MyServlet</servlet-class> </servlet> <!-- 配置指定url將請求轉(zhuǎn)發(fā)到對應(yīng)的servlet 此處就是為什么后面的http://localhost:8080/servlet/hello能將請求轉(zhuǎn)發(fā)到MyServlet中進(jìn)行處理 --> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- session過期時間,以分鐘為單位 --> <session-config> <session-timeout>5</session-timeout> </session-config> <!-- 默認(rèn)歡迎頁 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
最后我們配置一下tomcat,進(jìn)行測試
最后我們使用http://localhost:8080/servlet/hello?method=add這個鏈接進(jìn)行測試
結(jié)果如下,符合我們的預(yù)期
到此這篇關(guān)于SpringMVC MVC架構(gòu)與Servlet使用詳解的文章就介紹到這了,更多相關(guān)SpringMVC MVC架構(gòu)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud Feign配置應(yīng)用詳細(xì)介紹
這篇文章主要介紹了SpringCloud Feign配置應(yīng)用,feign是netflix提供的服務(wù)間基于http的rpc調(diào)用框架,在spring cloud得到廣泛應(yīng)用2022-09-09解決MyBatisPlus的updateBatchById()批量修改失效問題
這篇文章主要介紹了解決MyBatisPlus的updateBatchById()批量修改失效問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08struts2實現(xiàn)文件上傳顯示進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了struts2實現(xiàn)文件上傳顯示進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05解決@Validated注解無效,嵌套對象屬性的@NotBlank無效問題
這篇文章主要介紹了解決@Validated注解無效,嵌套對象屬性的@NotBlank無效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10