文章目录
SpringMVC简介
SpringMVC概述
SpringMVC快速入门
开发步骤
需求案例
- 客户端发起请求,服务器端接受请求,执行逻辑并进行视图跳转
- 咱们使用上一次的Spring三层架构环境项目继续演示
导入MVC坐标
注意,版本要一致
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
编写web.xml
配置SpringMVC的前端控制器
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
创建类
controller.UserController.java
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* create by 刘鸿涛
* 2022/4/16 18:19
*/
@SuppressWarnings({"all"})
@Controller
public class UserController {
@RequestMapping("/quick")
public String save(){
System.out.println("Controller save running...");
//跳转视图
return "success.jsp";
}
}
编写一个跳转目标
使用jsp文件
<%--
Created by IntelliJ IDEA.
User: guigui
Date: 2022/4/16
Time: 18:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success!!</h1>
</body>
</html>
编写Spring_mvc.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--Controller的组件扫描-->
<context:component-scan base-package="com.taotao.controller"/>
</beans>
成功运行测试
jsp页面正常
servlet正常
quick执行正常