javascript
SpringMVC学习01之回顾Servlet
前言
ssm : mybatis + Spring + SpringMVC MVC三層架構(gòu)
前提:
JavaSE:認(rèn)真學(xué)習(xí)
JavaWeb:認(rèn)真學(xué)習(xí)
后期如何學(xué)習(xí):
SSM框架:研究官方文檔,鍛煉自學(xué)能力,鍛煉筆記能力,鍛煉項(xiàng)目能力
SpringMVC + Vue + SpringBoot + SpringCloud + Linux
今天我們只學(xué)習(xí)ssm框架中的最后一個(gè):SpringMVC
一、回顧MVC是什么?
- MVC是模型(Model)、視圖(View)、控制器(Controller)的簡(jiǎn)寫,是一種軟件設(shè)計(jì)規(guī)范。
- 是將業(yè)務(wù)邏輯、數(shù)據(jù)、顯示分離的方法來(lái)組織代碼。
- MVC主要作用是降低了視圖與業(yè)務(wù)邏輯間的雙向偶合。
- MVC不是一種設(shè)計(jì)模式,MVC是一種架構(gòu)模式。當(dāng)然不同的MVC存在差異。
Model(模型):數(shù)據(jù)模型,提供要展示的數(shù)據(jù),因此包含數(shù)據(jù)和行為,可以認(rèn)為是領(lǐng)域模型或JavaBean組件(包含數(shù)據(jù)和行為),不過(guò)現(xiàn)在一般都分離開(kāi)來(lái):Value Object(數(shù)據(jù)Dao) 和 服務(wù)層(行為Service)。也就是模型提供了模型數(shù)據(jù)查詢和模型數(shù)據(jù)的狀態(tài)更新等功能,包括數(shù)據(jù)和業(yè)務(wù)。
View(視圖):負(fù)責(zé)進(jìn)行模型的展示,一般就是我們見(jiàn)到的用戶界面,客戶想看到的東西。
Controller(控制器):接收用戶請(qǐng)求,委托給模型進(jìn)行處理(狀態(tài)改變),處理完畢后把返回的模型數(shù)據(jù)返回給視圖,由視圖負(fù)責(zé)展示。也就是說(shuō)控制器做了個(gè)調(diào)度員的工作。
SSM = JavaWeb做項(xiàng)目;
Mybatis:數(shù)據(jù)庫(kù)操作,底層操作
Spring : IOC和AOP
SpringMVC: SpringMVC的執(zhí)行流程!SpringMVC : SSM框架整合!
二、使用步驟
1.準(zhǔn)備的環(huán)境
(1)新建一個(gè)Maven工程當(dāng)做父工程!pom依賴!
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</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>(2)添加Web app的支持!
(3)導(dǎo)入servlet 和 jsp 的 jar 依賴
2.編寫代碼
編寫一個(gè)Servlet類,用來(lái)處理用戶的請(qǐng)求
package com.shan.servlet;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username = req.getParameter("username");String password = req.getParameter("password");String method = req.getParameter("method");if (method.equals("登錄")){if (username.equals("zxs")&&password.equals("111")){req.getSession().setAttribute("msg","登錄成功");req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);}else {req.getSession().setAttribute("msg","密碼不正確或帳戶名不正確");req.getRequestDispatcher("index.jsp").forward(req,resp);}}else if (method.equals("注冊(cè)")){req.getSession().setAttribute("msg","進(jìn)入到注冊(cè)頁(yè)面");req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }歡迎頁(yè)index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html><head><title>系統(tǒng)</title></head><body><form method="get" action="/hs">帳戶 : <input type="text" name="username"><br>密碼 : <input type="password" name="password"><br><input type="submit" name="method" value="登錄" > <input type="submit" name="method" value="注冊(cè)"></form><p style="color: red"> ${msg} </p></body> </html>在web-inf下創(chuàng)建一個(gè)jsp目錄,在jsp里面創(chuàng)建要跳轉(zhuǎn)到的hello.jsp這個(gè)頁(yè)面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>servlet</title> </head> <body> ${msg} </body> </html>在web.xml中注冊(cè)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.shan.servlet.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/hs</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>配置Tomcat,并啟動(dòng)測(cè)試
localhost:8080
3.測(cè)試
在這里進(jìn)行一個(gè)方法判斷,如果你點(diǎn)擊了登錄就會(huì)進(jìn)行登錄判斷,若帳戶和密碼不正確則會(huì)顯示密碼不正確或帳戶名不正確
如果你點(diǎn)擊了注冊(cè)按鈕則會(huì)跳轉(zhuǎn)到注冊(cè)頁(yè)面(這里注冊(cè)功能沒(méi)有實(shí)現(xiàn),由于是回顧Servlet,不需要數(shù)據(jù)庫(kù),也沒(méi)去實(shí)現(xiàn)注冊(cè)功能)
如果帳戶密碼正確則登陸成功
作者有話說(shuō)
博客創(chuàng)作不易,希望看到這里的讀者動(dòng)動(dòng)你的小手點(diǎn)個(gè)贊,如果喜歡的小伙伴可以一鍵三連,作者大大在這里給大家謝謝了。
總結(jié)
以上是生活随笔為你收集整理的SpringMVC学习01之回顾Servlet的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring学习12之整合Mybatis
- 下一篇: SpringMVC学习03之使用注解开发