简介:和servlet功能相同,但是更加的简单
含义:MVC是一种架构思想,将软件按照模型、视图、控制器来划分
- M:Model、模型层,指工程中的javaBean,作用是处理数据
- V:View、视图层,指工程中html或jsp页面,作用是与用户进行交互,展示数据
- C:Controller、控制层,指工程中的servlet,作用是接收请求和响应浏览器
-
SpringMVC的基本组件
- DispatcherServlet:前端控制器,用于统一处理请求和响应,整个流程的控制中心,由它调用其他组件处理用户的请求
- HandlerMapping:处理器映射器,其会根据请求的URL,method等信息查找Handler,即控制器方法
- Handler:处理器,需要工程师开发;其作用是在DispatcherServlet的控制下Handler对具体的用户请求进行处理
- HandlerAdapter:处理器适配器,他会通过处理器适配器对控制方法进行执行(就是调用处理器)
- ViewResolver:视图解析器,主要功能是进行视图解析,得到相应的视图
- View:视图,作用是将模型数据通过页面展示给用户跑啥
SpringMVC的使用
- 创建maven的web工程并导入对应的依赖
- 在web.xml文件中配置springmvc的前端控制器DispatcherServlet以及springmvc配置文件
- 创建请求控制器
<dependencies>
<!--springmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
配置springmvx.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.letstalk.controller"/>
<mvc:default-servlet-handler/>
<!-- 开启mvc注解驱动 -->
<mvc:annotation-driven/>
<!-- 文件上传解析器 -->
<!-- 自定义文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传的默认编码格式-->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 显示文件上传的大小-->
<property name="maxUploadSize" value="3000000000"></property>
</bean>
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/html/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<bean class="com.letstalk.config.Swagger2Config"/>
<!--重要!配置swagger资源不被拦截-->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars" />
</beans>
web.xml配置
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
开始使用
//请求控制器
@Controller
public class HelloController {
@RequestMapping("/")
public String protal(){
//将逻辑视图返回
return "success";
}
}
注解讲解
@RequestMapping注解
前言:从注解名字上我们可以看出,@RequestMapping注解的作用就是将请求和处理请求的控制器方法进行关联,建立起映射联系;当springmvc接收到这个请求,那么就会来找到在映射关系中对应的控制器方法来处理这个请求。
Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
所有的请求默认都会是 HTTP GET 类型的。
为了能降一个请求映射到一个特定的 HTTP 方法,你需要在 @RequestMapping 中使用 method 属性来声明 HTTP 请求所使用的方法类型,也可以使用等价的组合注解。
如下所示:
请求 | 组合注解 | 共享注解 |
---|---|---|
GET | @GetMapping | @RequestMapping(method = RequestMethod.GET) |
POST | @PostMapping | @RequestMapping(method = RequestMethod.POST) |
PUT | @PutMapping | @RequestMapping(method = RequestMethod.PUT) |
DELETE | @DeleteMapping | @RequestMapping(method = RequestMethod.DELETE) |
PATCH | @PatchMapping | @RequestMapping(method = RequestMethod.PATCH) |
需要注意的是,控制器方法都应该映射到一个特定的HTTP方法,即使用组合注解,而不是使用@RequestMapping共享映射。因为组合注解减少了在应用程序上要配置的元数据,并且代码功能更清晰。
SpringMVC支持ant风格的路径
路径中的占位符(在@RequestMapping的value属性内)
- ?:表示任意的单个字符,但不包括?本身
- *:表示任意的0个或多个字符,但不包括?和/
- **:表示任意层数的任意目录,只能将**写在双斜线中,前后以及中间不能有任意其他字符
restful风格
rest(representational state transfer):表现层资源状态转移
restful风格提倡url地址使用统一的风格设计,从前到后各个单词使用/分开,不使用问号键值对等方式携带请求参数,而是将要发送给服务器的数据作为URL地址的一部分,以保证整体风格的一致性。