一、Spring MVC
1.1 Spring MVC优势
- 严格遵守了MVC分层思想
- 采用松耦合,插件式的结构;更灵活
- SpringMVC是基于Spring的扩展、提供了一套完善的MVC注解
- SpringMVC在数据绑定。视图解析都提供了多种处理方式,可灵活配置
- SpringMVC对RESTful URL设计方法提供了良好的支持
1.2 SpringMVC本质工作
- 接收并解析请求
- 处理请求
- 数据渲染、响应请求
二、SpringMVC框架部署
2.1 基于Maven创建一个Web工程
2.2 添加SpringMVC依赖
- Spring-context
- Spring-aspects
- Spring-jdbc
- Spring-web
- Spring-webmvc
- Spring-test
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springmvc-demo1</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<spring.verson>5.2.13.RELEASE</spring.verson>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.verson}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.verson}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.verson}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.verson}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.verson}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.verson}</version>
</dependency>
</dependencies>
</project>
2.3 创建SpringMVC配置文件
- 在resources目录下创建名为
spring-servlet.xml
的文件 - 添加MVC命名空间
<?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"
xmlns:mvc="http://www.springframework.org/schema/cache"
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 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--声明使用注解配置-->
<context:annotation-config/>
<!--声明Spring工厂注解的扫描范围-->
<context:component-scan base-package="com"/>
<!--声明MVC使用注解驱动-->
<mvc:annotation-driven/>
</beans>
2.4 在web.xml中配置SpringMVC的前控制器
三、SpringMVC框架的使用
3.1 创建控制器
-
创建一个名为
com
的包(包需要被SpringMVC管理)controllers -
创建一个类(无需做任何继承和实现)
-
在类上添加
@Controller
声明此类为SpringMVC的控制器 -
在类上添加
@RequestMapping("url")
声明此控制器类的请求url
3.1.1 创建控制器类
@Controller
@RequestMapping("/book")
public class BookController {
}
3.1.2 在控制器类中定义处理请求的方法
- 在一个控制器类中可以定于多个方法处理不同的请求
- 在每个方法上添加
@RequestMapping("url")
声明此当前方法请求的url
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/add")
public void add(){
System.out.println("bookadd");
}
@RequestMapping("/list")
public void list(){
System.out.println("booklist");
}
}
3.1.3 访问
http://localhost:8080/springmvc_demo1/book/add
http://localhost:8080/springmvc_demo1/book/list
3.1.4 /*和/的区别
- /*拦截所有HTTP请求,包括jsp的请求,都为控制器请求
- /拦截所有HTTP请求,但不包括jsp的请求,但不会放行静态资源(html,css,js,图片)
3.1.5 静态资源的访问(在spring-servlet.xml)
<!--放行静态资源-->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/pages/**" location="/pages/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
3.2 前端提交数据到服务器
3.2.1 创建前端页面
- bookadd
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<h3>添加图书</h3>
<form action="" method="post">
<p>图书名称<input type="text" /></p>
<p>图书作者<input type="text"/></p>
<p>图书价格<input type="text"/></p>
<p><input type="submit" value="提交"/></p>
</form>
</body>
</html>
3.2.2前端页面提交数据
- 表单提交:输入框需要提供name属性,SpringMVC控制器是通过name属性取值的
- 超链接url提交:
- AJAX提交:请求行、请求头、请求体都可以用来传值。
3.4 控制器接受前端提交的数据
3.4.1 请求行传值
- 表单提交
- URL提交
- $.ajax请求的url传值
- $.post()/$.get()
**@RequestParam
**注解用于接收请求行传递数据
- 前端提交数据
<form action="/book/add" method="post">
<p>图书名称<input type="text" name="bookName"/></p>
<p>图书作者<input type="text" name="bookAuthor"/></p>
<p>图书价格<input type="text" name="bookPrice"/></p>
<p><input type="submit" value="提交"/></p>
</form>
- 控制器接收数据
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/add")
public void add(@RequestParam("name") String a,
@RequestParam("author") String b,
@RequestParam("price") double c){
System.out.println("bookadd");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
@RequestMapping("/list")
public void list(){
System.out.println("booklist");
}
}
注意
如果控制器方法中接收数据的参数名与请求行传值的key的一致,则@RequestParam可以省略
3.4.2 请求头传值
-
ajax封装请求头数据
$.ajax({})
3.4.3 请求体传值
- ajax封装请求体数据