引用學習html
MVC是模型(Model)、視圖(View)、控制器(Controller)的簡寫,是一種軟件設計規範。前端
是將業務邏輯、數據、顯示,三者分離的方法來組織代碼。vue
MVC主要做用是下降了視圖與業務邏輯間的雙向偶合。java
MVC不是一種設計模式,MVC是一種架構模式。固然不一樣的MVC存在差別。react
Model(模型):數據模型,提供要展現的數據,所以包含數據和行爲,能夠認爲是領域模型或JavaBean組件(包含數據和行爲),不過如今通常都分離開來:Value Object(數據Dao) 和 服務層(行爲Service)。也就是模型提供了模型數據查詢和模型數據的狀態更新等功能,包括數據和業務。angularjs
View(視圖):負責進行模型的展現,通常就是咱們見到的用戶界面,客戶想看到的東西。web
Controller(控制器):接收用戶請求,委託給模型進行處理(狀態改變),處理完畢後把返回的模型數據返回給視圖,由視圖負責展現。 也就是說控制器作了個調度員的工做。spring
最典型的MVC就是JSP + servlet + javabean的模式。設計模式
在web早期的開發中,一般採用的都是Model1。api
Model1中,主要分爲兩層,視圖層和模型層。
Model1優勢:架構簡單,比較適合小型項目開發;
Model1缺點:JSP職責不單一,職責太重(Model1中JSP頁面身兼View和Controller兩種角色,將控制邏輯和表現邏輯混雜在一塊兒,從而致使代碼的重用性很是低,增長了應用的擴展性和維護的難度),不便於維護;
Model2把一個項目分紅三部分,包括視圖、控制、模型。
用戶發請求
Servlet接收請求數據,並調用對應的業務邏輯方法
業務處理完畢,返回更新後的數據給servlet
servlet轉向到JSP,由JSP來渲染頁面
響應給前端更新後的頁面
職責分析:
Controller:控制器
取得表單數據
調用業務邏輯
轉向指定的頁面
Model:模型
業務邏輯
保存數據的狀態
View:視圖
顯示頁面
Model2這樣不只提升的代碼的複用率與項目的擴展性,且大大下降了項目的維護成本。Model2消除了Model1的缺點。
新建一個Maven工程當作父工程! pom依賴!
<dependencies> <!-- 測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!-- servlet,jsp,jstl --> <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的支持!
導入servlet 和 jsp 的 jar 依賴
<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>
編寫一個Servlet類,用來處理用戶的請求
package com.kuang.servlet; //實現Servlet接口 public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //取得參數 String method = req.getParameter("method"); if (method.equals("add")){ req.getSession().setAttribute("msg","執行了add方法"); } if (method.equals("delete")){ req.getSession().setAttribute("msg","執行了delete方法"); } //業務邏輯 //視圖跳轉 req.getRequestDispatcher("/WEB-INF/jsp/hello.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>Kuangshen</title> </head> <body> ${msg} </body> </html>
在web.xml中註冊Servlet
<?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-name>HelloServlet</servlet-name> <servlet-class>com.kuang.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/user</url-pattern> </servlet-mapping> </web-app>
配置Tomcat,並啓動測試
localhost:8080/user?method=add
localhost:8080/user?method=delete
MVC框架要作哪些事情
將url映射到java類或java類的方法 .
封裝用戶提交的數據 .
處理請求--調用相關的業務處理--封裝響應數據 .
將響應的數據進行渲染 . jsp / html 等表示層數據 .
說明: