SpringMVC
系列文章目录
springMVC技术的学习
文章目录
- 系列文章目录
- 前言
- 一、SpringMVC简介
-
- 1.SpringMVC概述
- 2.SpringMVC入门案例
- 3.SpringMVC入门案例工作流程分析
- 4.Controller加载控制
- 5.PostMan
- 二、请求与响应
-
- 1.请求映射路径
- 2.请求参数
- 3.JSON数据参数传递
- 4.日期类型参数传递
- 5.响应
- 总结
前言
一、SpringMVC简介
1.SpringMVC概述
2.SpringMVC入门案例
项目结构
pom.xml
<?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-01-quickstart</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><dependencies><!--1.导入Servlet与SpringMVC的坐标--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.21</version></dependency></dependencies>
</project>
UserController
package org.example.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;//2.定义Controller
//2.1使用@Controller定义Bean
@Controller
public class UserController {//2.2当前访问路径@RequestMapping("/save")//2.3设置当前操作的返回值类型@ResponseBodypublic String save(){System.out.println("user save ..");return "{'module':'springmvc'}";}
}
SpringMvcConfig
package org.example.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;//3.加载SpringMVC的配置文件,加载Controller对应的Bean
@Configuration
@ComponentScan("org.example")
public class SpringMvcConfig {
}
ServletContainersInitConfig
package org.example.config;import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;//4.定义一个Servlet容器启动的配置类,在里面加载Spring的配置
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {//加载SpringMVC容器配置protected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}//设置哪些请求归属SpringMVC处理protected String[] getServletMappings() {return new String[]{"/"};}//加载Spring容器配置protected WebApplicationContext createRootApplicationContext() {return null;}
}
3.SpringMVC入门案例工作流程分析
4.Controller加载控制
5.PostMan
二、请求与响应
1.请求映射路径
2.请求参数
3.JSON数据参数传递
4.日期类型参数传递
5.响应
总结
本节主要讲解了对SpringMVC的基本使用和在SpringMVC中的请求和响应不同参数时怎样去做。
参考视频