SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解
SpringMVC
ssm:Mybatis+Spring+SpringMVC MVC三層架構(gòu)
JavaSE:認真學(xué)習(xí),老師帶,入門快
JavaWeb:認真學(xué)習(xí),老師帶,入門快
SSM框架:研究官方文檔,鍛煉自學(xué)能力,筆記能力,項目能力
SpringMVC+Vue+SpringBoot+SpringCloud+Linux
SSM = JavaWeb做項目;
Spring:IOC和AOP
SpringMVC:SpringMVC的執(zhí)行流程!【重點】
SpringMVC:SSM框架整合!
1. 回顧MVC
什么是MVC?
- Model+View+Controller=MVC
- 將業(yè)務(wù)邏輯、數(shù)據(jù)、顯示分離的方法來組織代碼;
- 降低了視圖與業(yè)務(wù)邏輯間的雙向耦合
- 不是設(shè)計模式,是一種架構(gòu)模式。

新建一個Maven工程當作父工程!pom依賴!
<!--依賴-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
建立一個Moudle:springmvc-01-servlet,添加web app的支持。
導(dǎo)入servlet和jsp依賴:已經(jīng)包含在父依賴
編寫一個Servlet類,用來處理用戶的請求
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.獲取前端參數(shù)
String method = req.getParameter("method");
if(method.equals("add")){
req.getSession().setAttribute("msg","執(zhí)行了add方法");
}
if(method.equals("delete")){
req.getSession().setAttribute("msg","執(zhí)行了delete方法");
}
//2.調(diào)用業(yè)務(wù)層
//3.頁面跳轉(zhuǎn)
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
編寫Hello.jsp,在WEB-INF目錄下新建一個jsp的文件夾,新建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
#{msg}
</body>
</html>
在web.xml中注冊servlet
<servlet> <servlet-name>hello</servlet-name> <servlet-class>com.kuang.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
配置Tomcat,并啟動測試!localhost:8080?hello/method=addlocalhost:8080?hello/method=delete
注意點:
每個模塊需要配置到tomcat-deployment,必須部署到服務(wù)器上!!!
MVC框架要做哪些事情?
- 將url映射到j(luò)ava類或類中的方法;
- 封裝用戶提交的數(shù)據(jù);
- 處理請求---調(diào)用相關(guān)業(yè)務(wù)處理--封裝響應(yīng)數(shù)據(jù);
- 將響應(yīng)數(shù)據(jù)進行渲染 .jsp/html等顯示層數(shù)據(jù)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項目整合druid數(shù)據(jù)庫連接池的實現(xiàn)
這篇文章主要介紹了springboot項目整合druid數(shù)據(jù)庫連接池的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
MyBatis-Plus實現(xiàn)字段自動填充功能的示例
本文主要介紹了MyBatis-Plus實現(xiàn)字段自動填充功能的示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Java static(靜態(tài)變量)和私有化功能與用法分析
這篇文章主要介紹了Java static(靜態(tài)變量)和私有化功能與用法,結(jié)合具體實例形式分析了Java static(靜態(tài)變量)和私有化的相關(guān)概念、原理、使用方法及操作注意事項,需要的朋友可以參考下2019-07-07
mybatis resultType自帶數(shù)據(jù)類型別名解讀
MyBatis為了簡化開發(fā),通過org.apache.ibatis.type.TypeAliasRegistry為常見類定義了別名,這些別名包括基本數(shù)據(jù)類型及其數(shù)組、集合類型等,如string對應(yīng)java.lang.String,int對應(yīng)java.lang.Integer等,此外,還有特殊前綴的別名如_int對應(yīng)int類型2024-10-10
SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問題
這篇文章主要介紹了SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

