文章目录
- 1. springMVC基本功能
- (1) maven坐标导入
- (2) 编写表现层
- (3) springMVC配置类编写
- (4) 部署tomcat访问
- 2. 各种请求方法
- get请求
- post请求
- put请求
- delete请求
- 请求参数提取
- 3. 请求参数接收
- (1) param参数接受
- 封装到对象中
- (2) 路劲参数接收
- 集合接受
- 时间类型接收
- json参数接收
- maven导包
- 开启功能
- 接收数据
- 4. 响应数据
- 5. 全局异常处理
- 6. springMvc拦截器(Interceptor)
1. springMVC基本功能
与servlet技术功能相等,实现表现层开发:
(1) maven坐标导入
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.11</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
(2) 编写表现层
@Controller // 使用controller定义bean
public class test {
@GetMapping("/hello")
@ResponseBody // 直接返回给浏览器
public String testMVC(){
System.out.println("接收到请求");
return "hello world!";
}
}
(3) springMVC配置类编写
容器配置:
package com.xjy.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan({"com.xjy.controller"})
public class MvcConfig {
}
定义 Spring MVC应用程序的初始化器:
package com.xjy.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
public class MvcContainerInit extends AbstractDispatcherServletInitializer {
//创建DispatcherServlet的应用程序上下文(ApplicationContext)
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(MvcConfig.class);
return ctx;
}
//指定DispatcherServlet的映射路径
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
public WebApplicationContext context(){
return this.createServletApplicationContext();
}
}
(4) 部署tomcat访问
注意对应的版本:
部署tomcat程序:
启动程序后浏览器访问:
2. 各种请求方法
get请求
@GetMapping("/get") // 接受get请求
@ResponseBody
public String get(){
System.out.println("接受到请求");
return "get";
}
post请求
@PostMapping("/post")
@ResponseBody
public String post(){
System.out.println("接受到请求");
return "post";
}
put请求
@PutMapping("/put")
@ResponseBody
public String put(){
System.out.println("接受到请求");
return "put";
}
delete请求
@DeleteMapping("/delete")
@ResponseBody
public String delete(){
System.out.println("接受到请求");
return "delete";
}
请求参数提取
@Controller
@RequestMapping("/user")
public class userController {
@RequestMapping("/hello")
@ResponseBody
public String save(){
System.out.println("接受到请求");
return "hello word!";
}
}
访问:
3. 请求参数接收
(1) param参数接受
直接参数接收:
@GetMapping("/get")
@ResponseBody
public String get(int id){
System.out.println("接受到请求"+id);
return "get";
}
封装到对象中
@GetMapping("/get")
@ResponseBody
public String get(student stu){
System.out.println("接受到请求"+stu.getName()+stu.getAge());
return "get";
}
发送请求:
(2) 路劲参数接收
@GetMapping("/get/{id}")
@ResponseBody
public String get(@PathVariable int id){
System.out.println("接受到路劲参数:"+id);
return "get";
}
发送请求:
集合接受
@PostMapping("/post")
@ResponseBody
public String post(@RequestParam List<Integer> list){
System.out.println("接受到请求"+list.get(0));
return "ok";
}
发送请求:
时间类型接收
@GetMapping("/get")
@ResponseBody
public String get(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date){
System.out.println("接受到时间参数:"+date);
return "get";
}
发送:
json参数接收
maven导包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
开启功能
@Configuration
@ComponentScan({"com.xjy.controller"})
@EnableWebMvc
public class MvcConfig {
}
接收数据
@PostMapping("/post")
@ResponseBody
public String post(@RequestBody student stu){
System.out.println("接受到请求"+ stu.getName());
return "{'status','ok'}";
}
发送数据:
4. 响应数据
方法中直接return
返回json: @ResponseBody 加上注解
@RestController -> @controller + @ResponseBody
5. 全局异常处理
注解需要配包扫描:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public String exceptionHandler(Exception ex){
System.out.println("处理到异常");
return "err";
}
}
模拟异常:
6. springMvc拦截器(Interceptor)
自定义拦截:
public class interceptorDo implements HandlerInterceptor {
@Override // 拦截方法
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截到请求");
return true;//返回true放行,返回不放行;
}
@Override //资源运行后执行
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override //视图渲染完毕后执行
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
配置拦截器:
@Configuration
@ComponentScan({"com.xjy.controller"})
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
@Bean
public interceptorDo getInteceptor(){
return new interceptorDo();
}
@Autowired
private interceptorDo interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor)
.excludePathPatterns("/long");//过滤登录拦截
}
}