文章目录
-
- 1.基本介绍
- 2.快速入门
- 3.SpringMVC执行流程
- 4.RequestMapping注解使用方式(不要背!)
- 5.课后练习
1.基本介绍
1.特点
![](https://file.jishuzhan.net/article/1785205378900824065/90e92043b544674914e1b90411c2326e.webp)
2.SpringMVC跟SpringBoot的关系
![](https://file.jishuzhan.net/article/1785205378900824065/a54f2bfc5665aabffa3c1bdd5ff0fb3f.webp)
2.快速入门
1.需求分析
![](https://file.jishuzhan.net/article/1785205378900824065/9e4032138610d588efe53f9e80454aea.webp)
2.图解
![](https://file.jishuzhan.net/article/1785205378900824065/6e868cb5d90917f24907aa74b71cd6aa.webp)
3.环境搭建
1.创建普通java工程
![](https://file.jishuzhan.net/article/1785205378900824065/cbbc06c179eba40d27a0b2b3e0f03346.webp)
2.添加web框架支持
![](https://file.jishuzhan.net/article/1785205378900824065/6d712d83f9849fc397ff22e07b03ae41.webp)
3.配置lib文件夹
1.导入jar包
![](https://file.jishuzhan.net/article/1785205378900824065/e7bcece22d5e6c8a2c9bf6c76354b7ed.webp)
2.Add as Library
![](https://file.jishuzhan.net/article/1785205378900824065/8fe7582ec395ae4602cdbbf6f084d979.webp)
3.以后自动添加
![](https://file.jishuzhan.net/article/1785205378900824065/8150c43464c447e516ced26837fb8694.webp)
4.配置tomcat
1.配置上下文路径
![](https://file.jishuzhan.net/article/1785205378900824065/1c21f3c6e2ccce5515a21a0be0fd4390.webp)
2.配置热加载
![](https://file.jishuzhan.net/article/1785205378900824065/a7e0ad5b0ef000a1635953b6ef097151.webp)
5.src下创建Spring配置文件applicationContext-mvc.xml
![](https://file.jishuzhan.net/article/1785205378900824065/5143fa3ec764ee4da907e0df21d4c43d.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/b5ab5d09701be0d9ea5d1042f5a7628e.webp)
6.配置中央控制器web.xml读取Spring配置文件
- 服务器启动则自动装载这个servlet,实例化servlet,调用init方法,读取spring配置文件
xml
<?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就调用init方法读取spring的配置文件,并且接收所有请求-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--init方法可以通过ServletConfig来读取这个初始化参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--读取spring配置文件-->
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--服务器启动就装载这个servlet,直接创建servlet实例,调用init方法-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--所有的请求都交给这servlet处理-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.具体实现
文件目录
![](https://file.jishuzhan.net/article/1785205378900824065/d2df10729d13926f102c39f7f6535954.webp)
1.编写首页面login.jsp
jsp
<%--
Date: 2024/2/23
Time: 20:44
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="">
username:<input name="username" type="text">
password:<input name="password" type="password">
<input type="submit" value="登录">
</form>
</body>
</html>
2.编写控制器UserServlet.java
java
package com.sun.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 孙显圣
* @version 1.0
*/
//表示这是一个控制器
@Controller
public class UserServlet {
//这个方法的网址是上下文路径 + login
@RequestMapping(value = "/login")
public String login() {
System.out.println("login ok...");
//视图解析器配置的前缀是/WEB-INF/pages/ 后缀是.jsp所以这个资源的路径就是/WEB-INF/pages/login_ok.jsp
return "login_ok"; //返回的这个值会交给视图解析器,指定要跳转的页面
}
}
3.编写视图解析器要跳转的页面login_ok.jsp
jsp
<%--
Date: 2024/2/23
Time: 20:54
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>ok!</h1>
</body>
</html>
4.配置视图解析器和容器扫描applicationContext-mvc.xml
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--容器扫描-->
<context:component-scan base-package="com.sun.web"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--配置前缀和后缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.配置login.jsp的action
![](https://file.jishuzhan.net/article/1785205378900824065/23fd2c07ddd779776a11403275032e0e.webp)
5.结果展示
1.login.jsp
![](https://file.jishuzhan.net/article/1785205378900824065/a66c461d5baf7207f5b7df9c02175e71.webp)
2.点击登录
![](https://file.jishuzhan.net/article/1785205378900824065/e129af353a5ef7c0c87a2d89f3e5f948.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/f414da84b9fafb3e91ad5d1590ff7819.webp)
6.注意事项和细节说明
1.控制器UserServlet指定url的时候可以省略value
![](https://file.jishuzhan.net/article/1785205378900824065/e7c7e4203d511dbd1d0d9a2c48be2d77.webp)
2.关于web.xml文件中配置读取Spring的配置文件
![](https://file.jishuzhan.net/article/1785205378900824065/b0d16b1f5efbb88d7b10f545c12e1938.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/ece64d4e5b6332af0ec25717c3c7aa01.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/1ebdbb3140514e8496f30f9e38f022d9.webp)
3.SpringMVC执行流程
![](https://file.jishuzhan.net/article/1785205378900824065/f1fe43eb1186053f96168aee3e03b189.webp)
4.RequestMapping注解使用方式(不要背!)
1.@RequestMapping可修饰方法和类
文件目录
![](https://file.jishuzhan.net/article/1785205378900824065/5cb94376704f9d0172a62cdb82e92d60.webp)
1.编写控制器UserHandler.java
java
package com.sun.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author 孙显圣
* @version 1.0
*/
@Controller
@RequestMapping(value = "/user")
public class UserHandler {
/**
* 1.requestMethod支持四个常用选项,get,post,delete,put
* 2.如果不写method参数则默认支持get和post
* 3.目前这个buy方法的访问路径是上下文路径 + user/buy
*/
@RequestMapping(value = "/buy", method = RequestMethod.POST)
public String buy() {
System.out.println("buy方法被调用");
return "success";
}
}
2.编写success.jsp
java
<%--
Date: 2024/2/24
Time: 12:40
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
3.编写request.jsp
java
<%--
Date: 2024/2/24
Time: 12:43
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user/buy" method="post">
<input type="submit" value="提交">
</form>
</body>
</html>
4.结果展示
1.首页
![](https://file.jishuzhan.net/article/1785205378900824065/1f354851977544aea561aff2b350dac5.webp)
2.点击提交
![](https://file.jishuzhan.net/article/1785205378900824065/55bb73c7a3f814bae2f3b0255539fc2e.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/9a361aea7ba10cf4821480401c0bdb4e.webp)
5.注意事项
1.如果指定了post请求,使用其他请求方式则会报错
![](https://file.jishuzhan.net/article/1785205378900824065/ce5b6a2641049fedcf3c834320a8cfb8.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/548bc1a27dce7abbf47deef968459976.webp)
2.如果没有指定请求方式则get或post都可以
2.@RequestMapping可指定params支持简单表达式
文件目录
![](https://file.jishuzhan.net/article/1785205378900824065/588ed27ca1ade5662245fb3755d4cb6e.webp)
1.基本介绍
![](https://file.jishuzhan.net/article/1785205378900824065/20014017a82f981cb9d0bf44a28b03e9.webp)
2.请求必须包含某个参数
1.UserHandler.java添加方法
java
/**
* 会将参数bookid自动填充到方法中
* @param bookid
* @return
*/
@RequestMapping(value = "/find", method = RequestMethod.POST, params = "bookid")
public String search(String bookid) {
System.out.println("bookid=" + bookid);
return "success";
}
2.编写request.jsp
jsp
<%--
Date: 2024/2/24
Time: 12:43
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user/find" method="post">
输入bookid:<input name="bookid" type="text">
<input type="submit" value="提交">
</form>
</body>
</html>
3.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/69143e939579797531a543a84adffc4b.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/91d75a517964d79d7ac6b75304f9dfb6.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/3e18f52985e225edd0a402f738786f8f.webp)
3.请求必须包含参数并且指定参数值
1.修改UserHandler.java
![](https://file.jishuzhan.net/article/1785205378900824065/ef7819af40fdcba21b2cff7906f71d09.webp)
2.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/ae9fab87a9c6218ac6673f6d587eb3d8.webp)
4.需要有参数并且值不等于什么
1.修改UserHandler.java
![](https://file.jishuzhan.net/article/1785205378900824065/1b3388e768a535eb18c64e507951380e.webp)
2.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/1605cdb9d02afc74ecfb0f5e53faffb4.webp)
3.@RequestMapping支持Ant风格资源地址
1.基本介绍
![](https://file.jishuzhan.net/article/1785205378900824065/12bce4d54ff952aacc4e63409eaa25cb.webp)
2.匹配多层路径
1.修改UserHandler.java
java
/**
* 要求可以匹配 /user/message/aa, /user/message/aa/bb/cc
* @return
*/
@RequestMapping(value = "/message/aa/**")
public String im() {
System.out.println("匹配成功");
return "success";
}
2.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/80867f70f1ac9d050e770daa99be581d.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/9c2e6ea5047eed00bf34ee255c04bedc.webp)
4.@RequestMapping可配合@PathVariable映射URL绑定的占位符
1.基本介绍
![](https://file.jishuzhan.net/article/1785205378900824065/2cd580c1d8f8e99707aaea4b36ef9ae6.webp)
2.通过路径变量获取参数
1.修改UserHandler.java
java
/**
* 可以编写路径变量,直接在路径中接受变量,结合@PathVariable来进行参数传递
*
* @return
*/
@RequestMapping(value = "/reg/{username}/{password}")
public String register(@PathVariable("username") String name, @PathVariable("password") String password) {
System.out.println("username = " + name + " password = " + password);
return "success"; //请求转发
}
2.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/53ab5940496a994653ba9139881cf807.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/7eeab05d8d7aa3eb4c933f80f326a56d.webp)
5.@RequestMapping注意事项和细节
1.映射的URL不能重复
1.修改UserHandler.java
java
/**
* 两个重复的url
*/
@RequestMapping("/l1")
public String h1() {
return "success";
}
@RequestMapping("/l1")
public String h2() {
return "success";
}
2.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/035932a58d98514215e944a05ecc64e7.webp)
2.请求方式简写
1.基本介绍
2.修改UserHandler.java
java
/**
* 这样表示的就是get类型的请求,请求地址为"/buy1"
*/
@GetMapping("/buy1")
public String buy_() {
return "success";
}
3.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/4b22a7ddece514d100df8efd5dcdabd8.webp)
3.提交数据简写
1.基本介绍
![](https://file.jishuzhan.net/article/1785205378900824065/20f4ef4caeb835131ba3c1410db0d83a.webp)
2.修改UserHandler.java
java
//提交的信息如果包含这个参数则会自动填充,否则会传入一个空值
@GetMapping("/hello")
public String hello(String email) {
System.out.println(email);
return "success";
}
3.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/cfbe2dbcd69f15b41942454cdf753a5b.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/dda8f96a0fc6e4a2eb1935fd4c8d145f.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/60bffec676474a7408c615a724f95860.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/c3adeff479410026c454195a28209b55.webp)
5.课后练习
1.题目
![](https://file.jishuzhan.net/article/1785205378900824065/c2b97f193158995b3bac916fbdb81287.webp)
2.第一题
![](https://file.jishuzhan.net/article/1785205378900824065/4b5497ea36b247ba860b5556bffab8a1.webp)
3.第三题
1.修改UserHandler.java
java
//课后练习
@PostMapping("/computer")
public String computer(String brand, String price, String num) {
System.out.println("brand=" + brand + " price=" + price + " num=" + num);
return "success";
}
2.homework01.jsp
jsp
<%--
Date: 2024/2/24
Time: 15:21
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user/computer" method="post">
品牌:<input name="brand" type="text"><br>
价格:<input name="price" type="text"><br>
数量:<input name="num" type="text"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
3.结果展示
![](https://file.jishuzhan.net/article/1785205378900824065/71367f00397f7531491b67914acb7446.webp)
![](https://file.jishuzhan.net/article/1785205378900824065/7cf4016315af489e6558dae43f2eed9a.webp)