SpringMvc day1102
ok了家人们今天我们学习SpringMvc,之后学习SpringBoot,let‘s go
六.拦截器
6.1 拦截器概述
类别
| 使用范围 | 拦截范围 |
拦截
器
|
SpringMVC
项
目
|
只会拦截访问的控制器方法的
请求
|
过滤
器
| 任何web项目 |
任何资源
(servlet,
控制
器
,jsp,html
等
)
|
6.2 自定义拦截器
- 定义拦截器
package com.cjx.Interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterceptor implements HandlerInterceptor {
//执行方法前执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
//false表示不执行该方法
return true;
}
//执行方法后执行
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
//方法执行后(如果返回一个jsp页面执行)
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
- 配置加载拦截器
package com.cjx.config;
import com.cjx.Interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
//当前类需要设置为配置类,并被扫描加载
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Autowired
private MyInterceptor myInterceptor;
//配置加载拦截器
@Override
protected void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
//注册拦截器,可以注册多个
registry.addInterceptor(myInterceptor).addPathPatterns("/emp","/emp/**");
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问/pages/xxxx时候,从/pages目录下查找内容
//http://loclahost:8080/js/axios-0.18.0.js
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
七.RESTFul风格的URL
7.1 RESTful简介
操作
| 请求方式 |
查询操作 | Get |
保存操作 |
POST
|
删除操作
| DELETE |
更新操作
| PUT |
http : //localhost:8080/springmvc02/user/queryUserById?id= 1 查询http : //localhost:8080/springmvc02/user/saveUser新增http : //localhost:8080/springmvc02/user/updateUser 更新http : //localhost:8080/springmvc02/user/deleteUserById?id=1 删除
使用RESTful操作资源:使用URL+请求方式确定具体的动作
http : //localhost:8080/springmvc02/user/1查询 ,GEThttp : //localhost:8080/springmvc02/user新增 ,POSThttp : //localhost:8080/springmvc02/user更新 ,PUThttp : //localhost:8080/springmvc02/user/1删除 ,DELETE
RESTFul风格好处:
-
安全:使用问号键值对的方式给服务器传递数据太明显,容易被人利用来对系统进行破坏。使用 REST 风格携带数据不再需要明显的暴露数据的名称。
-
风格统一: URL 地址整体格式统一,从前到后始终都使用斜杠划分各个单词,用简单一致的格式表达语义。
-
简洁:过去做增删改查操作需要设计 4 个不同的 URL ,现在一个就够了。
查询 / emp / editEmp ? empId = 2 / emp / 2 请求方式:GET保存 / emp / saveEmp / emp 请求方式:POST更新 / emp / updateEmp / emp 请求方式:PUT删除 / emp / removeEmp ? empId = 2 / emp / 2 请求方式:DELETE
7.2 RESTFul案例
@Controller
public class EmpController {
//根据ID查询
@RequestMapping(value =
"/emp/{empId}",method = RequestMethod.GET)
@ResponseBody
public Result
findById(@PathVariable("empId") Integer empId){
Result result=new Result(20001,"查询成
功",empId);
return result;
}
//查询所有
@RequestMapping(value = "/emp",method =
RequestMethod.GET)
@ResponseBody
public Result findAll(){
Result result=new Result(20002,"查询成
功",null);
return result;
}
//保存
@RequestMapping(value = "/emp",method =
RequestMethod.POST)
@ResponseBody
public Result save(@RequestBody Emp emp){
Result result=new Result(20003,"保存成
功",emp);
return result;
}
//删除
@RequestMapping(value =
"/emp/{empId}",method = RequestMethod.DELETE)
@ResponseBody
public Result delete(@PathVariable("empId")
Integer empId){
Result result=new Result(20004,"删除成
功",empId);
return result;
}
//修改
@RequestMapping(value = "/emp",method =
RequestMethod.PUT)
@ResponseBody
public Result update(@RequestBody Emp emp){
Result result=new Result(20005,"修改成
功",emp);
return result;
}
}
7.3 注解优化
@RestController
@RequestMapping("/emp")
public class EmpController {
//根据ID查询
@GetMapping("/{empId}")
@ResponseBody
public Result
findById(@PathVariable("empId") Integer empId){
Result result=new Result(20001,"查询成
功",empId);
return result;
}
//查询所有
@GetMapping
@ResponseBody
public Result findAll(){
Result result=new Result(20002,"查询成
功",null);
return result;
}
//保存
@PostMapping
@ResponseBody
public Result save(@RequestBody Emp emp){
Result result=new Result(20003,"保存成
功",emp);
return result;
}
//删除
@DeleteMapping("/{empId}")
@ResponseBody
public Result delete(@PathVariable("empId")
Integer empId){
Result result=new Result(20004,"删除成
功",empId);
return result;
}
//修改
@PutMapping
@ResponseBody
public Result update(@RequestBody Emp emp){
Result result=new Result(20005,"修改成
功",emp);
return result;
}
}
ok了家人们明天见byebye
原文地址:https://blog.csdn.net/2201_75555400/article/details/143452909
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!