一.什么是MVC
-
MVC是模型(Model)、视图(View)、控制器(Controller)的简写,是软件设计规范。
- 将业务逻辑、数据、显示分离的方法来组织代码。
- MVC不是一种设计模式,而是一种架构模式。不同MVC存在差异
二.回顾Servlet
1.创建一个Maven工程,然后导入依赖:
<!-- 导入依赖 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.11</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.创建Moudle,创建好后添加框架(Web app)支持(选择web时记得版本为4.0,其他版本可能会出一点问题)
3.导入servlet和jsp 的jar依赖(一般来说maven中有它们的依赖,保险一点导入进去)
<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>
4.编写一个Servlet类,用来处理用户的请求(servlet包下的类)
package com.lzy.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 {
//1.获取前端参数
String method = req.getParameter("method");
if (method.equals("add")){
req.getSession().setAttribute("msg","执行了add方法");
}
if (method.equals("delete")){
req.getSession().setAttribute("msg","执行了delete方法");
}
//2.调用业务层Service
//3.视图转发或者重定向
req.getRequestDispatcher("/WFB-INF/jsp/test.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
5.编写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>
6.在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>hello</servlet-name>
<servlet-class>com.lzy.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<!-- <!– 配置超时时间 –> -->
<!-- <session-config> -->
<!-- <session-timeout>15</session-timeout> -->
<!-- </session-config> -->
</web-app>
7.配置Tomcat,并启动测试
出现Error running 'Tomcat 9.0.39': Address localhost:1099 is already in use错误怎么办?
说这个端口产生占用
cmd打开命令
netstat -aon | find "1099"
记住最后这个数字,会变的
执行(后面输入自己返回的数字)
tasklist | find "5680"
在资源管理器中删除这个进程
三、SpringMVC
springmvc的特点:
1.轻量级,简单
2.高效的MVC框架
3.与spring兼容性好,无缝结合
4.约定优于配置
5.功能强大:数据验证、数据格式化
6.简洁灵活
1.新建一个moudle,添加web的支持
2.导入SpringMVC的依赖
3.配置web.xml,注册DispatcherServlet
4.编写springmvc-servlet.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.添加 处理映射器(springmvc-servlet.xml里)
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
6.添加 处理适配器(同上)
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
7.添加 视图处理器(同上)
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
8.写一个HelloController
package com.lzy.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView 模型视图
ModelAndView mv = new ModelAndView();
//封装对象,放在ModelAndView中
mv.addObject("msg","HelloSpringMVC!");
//封装要跳转的视图.放在ModelAndView中
mv.setViewName("hello");///WEB-INF/jsp/hello.jsp
return mv;
}
}