前言
ssm : mybatis + Spring + SpringMVC MVC三层架构
前提:
JavaSE:认真学习
JavaWeb:认真学习
后期如何学习:
SSM框架:研究官方文档,锻炼自学能力,锻炼笔记能力,锻炼项目能力
SpringMVC + Vue + SpringBoot + SpringCloud + Linux
今天我们只学习ssm框架中的最后一个:SpringMVC
一、回顾MVC是什么?
- MVC是模型(Model)、视图(View)、控制器(Controller)的简写,是一种软件设计规范。
- 是将业务逻辑、数据、显示分离的方法来组织代码。
- MVC主要作用是降低了视图与业务逻辑间的双向偶合。
- MVC不是一种设计模式,MVC是一种架构模式。当然不同的MVC存在差异。
Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数据和行为),不过现在一般都分离开来:Value Object(数据Dao) 和 服务层(行为Service)。也就是模型提供了模型数据查询和模型数据的状态更新等功能,包括数据和业务。
View(视图):负责进行模型的展示,一般就是我们见到的用户界面,客户想看到的东西。
Controller(控制器):接收用户请求,委托给模型进行处理(状态改变),处理完毕后把返回的模型数据返回给视图,由视图负责展示。也就是说控制器做了个调度员的工作。
SSM = JavaWeb做项目;
Mybatis:数据库操作,底层操作
Spring : IOC和AOP
SpringMVC: SpringMVC的执行流程!SpringMVC : SSM框架整合!
二、使用步骤
1.准备的环境
(1)新建一个Maven工程当做父工程!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)导入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>
2.编写代码
编写一个Servlet类,用来处理用户的请求
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 {
@Override
protected 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("注册")){
req.getSession().setAttribute("msg","进入到注册页面");
req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
欢迎页index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>系统</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="注册">
</form>
<p style="color: red"> ${msg} </p>
</body>
</html>
在web-inf下创建一个jsp目录,在jsp里面创建要跳转到的hello.jsp这个页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>servlet</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.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,并启动测试
localhost:8080
3.测试
在这里进行一个方法判断,如果你点击了登录就会进行登录判断,若帐户和密码不正确则会显示密码不正确或帐户名不正确
如果你点击了注册按钮则会跳转到注册页面(这里注册功能没有实现,由于是回顾Servlet,不需要数据库,也没去实现注册功能)
如果帐户密码正确则登陆成功
作者有话说
博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。