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(視圖):負責(zé)模型的展示,即用戶看到的界面,例如JSP
- Controller(控制器):接收請求,委托給model進行處理,等到處理完畢之后,將數(shù)據(jù)模型返回給視圖,由視圖負責(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ù)進行渲染 . 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中進行處理
-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<!-- session過期時間,以分鐘為單位 -->
<session-config>
<session-timeout>5</session-timeout>
</session-config>
<!-- 默認歡迎頁 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
最后我們配置一下tomcat,進行測試


最后我們使用http://localhost:8080/servlet/hello?method=add這個鏈接進行測試
結(jié)果如下,符合我們的預(yù)期

到此這篇關(guān)于SpringMVC MVC架構(gòu)與Servlet使用詳解的文章就介紹到這了,更多相關(guān)SpringMVC MVC架構(gòu)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis利用association或collection傳遞多參數(shù)子查詢
今天小編就為大家分享一篇關(guān)于mybatis利用association或collection傳遞多參數(shù)子查詢,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Mybatis中collection和association的使用區(qū)別詳解
這篇文章主要介紹了Mybatis中collection和association的使用區(qū)別詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/i
這篇文章主要介紹了IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置,我們可以通過自定義配置數(shù)據(jù)庫配置類來解決這個問題,方式有很多,不同的業(yè)務(wù)采用的方式也不同,下面我簡單的介紹我們項目的使用的方法2022-06-06
徹底搞懂java并發(fā)ThreadPoolExecutor使用
這篇文章主要為大家介紹了徹底搞懂java并發(fā)ThreadPoolExecutor使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Java結(jié)構(gòu)型設(shè)計模式中的適配器模式與橋接模式解析
這篇文章主要介紹了Java結(jié)構(gòu)型設(shè)計模式中的適配器模式與橋接模式,結(jié)構(gòu)型設(shè)計模式是從程序的結(jié)構(gòu)上解決模塊之間的耦合問題,需要的朋友可以參考下2016-02-02
SpringCloud @FeignClient參數(shù)的用法解析
這篇文章主要介紹了SpringCloud @FeignClient參數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

