@RequestMapping
类上的RequestMapping是/a,方法上是/b,访问时需要是/a/b
@RequestMapping("/test/{id}/{username}")
public String index(@PathVariable String id,@PathVariable String username){
System.out.println(id);
System.out.println(username);
return "index";
}
接收参数
在DispatcherServlet调用控制器方法的过程中,根据控制器方法的形参为他们注入值
# 控制器方法中写了HttpServletRequest,HttpSession参数,会自动注入
# 页面是以get方式url后问号拼接的参数(接收的时候直接写即可)
public String testParam(String username,String password)
# 传过来的参数的键名有重复的(后台用数组接收)
# 控制器方法形参的参数名需要和传过来的参数名一致,不一致的话后台使用@RequestParam
# @RequestParam注解中的defaultValue属性当参数没传或者为空字符串时有效
# @RequestHeader将请求头键名和控制器参数名创建映射关系
# @CookieValue将cookie键名和控制器参数名创建映射关系
服务器三大组件(监听器,过滤器,servlet)
servletContextListener加载最早,然后是filter,最后是servlet
监听servletContext创建和销毁,这两个方法都只执行一次
过滤器每次都会执行
域对象
request一次请求,session一次会话,servletContext整个应用的范围(服务器开启到关闭)
# 向request域对象共享数据
// 原生方式
@GetMapping("/test")
public String test(HttpServletRequest request){
request.setAttribute("test","dddq");
return "xxx";
}
// 使用ModelAndView
@GetMapping("/test1")
public ModelAndView test1(){
// 在控制器方法中使用了ModelAndView,返回值必须是ModelAndView
ModelAndView mav = new ModelAndView();
// 向request请求域共享数据
mav.addObject("test2","cff");
// 设置视图名称
mav.setViewName("xxx");
return mav;
}
// 使用Model
@GetMapping("/test2")
public String test2(Model model){
model.addAttribute("adfsf","dw");
return "xxx";
}
// 使用Map
@GetMapping("/test3")
public String test3(Map<String,Object> map){
map.put("grg","gege");
return "xxx";
}
// 使用ModelMap
@GetMapping("/test4")
public String test4(ModelMap modelMap){
modelMap.addAttribute("grge","qqw");
return "xxx";
}
// Model,ModelMap,Map类型的参数本质是BindingAwareModelMap类型的
// BindingAwareModelMap实现了Model接口,继承了ModelMap
# 向session域对象共享数据
@GetMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("cwefw","cwd");
return "xxx";
}
# 向servletContext域共享数据
@GetMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext servletContext = session.getServletContext();
servletContext.setAttribute("cscs","cwd");
return "xxx";
}
视图
SpringMvc中的视图是View接口
SpringMvc视图的种类有很多,默认有转发视图InternalResourceView和重定向视图RedirectView
若使用的视图技术为Thymeleaf,在SpringMvc的配置文件中配置了Thymeleaf的视图解析器,由此解析器解析得到的是ThymeleafView
控制器方法返回的视图名称没有任何前缀时,才会被Thymeleaf的视图解析器解析
默认情况下,没有Thymeleaf时,控制器方法返回的视图名称没有任何前缀时,得到的是InternalResourceView
请求方式
当浏览器不支持put和delete时,使用post方式,带上一个参数_method,指定是put或delete或patch
SpringMvc中的HiddenHttpMethodFilter会获取请求参数_method的值,帮助将请求转为put或delete或patch方式
注意:要将CharacterEncodingFilter放在HiddenHttpMethodFilter之前,因为设置编码之前不能获取任何参数,否则无效
HttpMessageConverter
报文消息转换器,Java对象和数据的转换
// RequestEntity
@PostMapping("test2")
public String test2(RequestEntity<String> requestEntity){
System.out.println("header:" + requestEntity.getHeaders());
System.out.println("body:" + requestEntity.getBody());
return "test.jsp";
}
@RequestBody:接收请求体中的数据(不只是JSON数据)
@RequestParam:接key1=value1&key2=value2这样的参数列表,可以是url后边的,也可以是x-www-form-urlencoded数据
如果使用了@RequestParam,但没传对应的参数则会报错,想不报错,则将属性required设为false
@PathVariable:接路径变量{variable}的形式
@RequestParam和@PathVariable的区别