根据视频学习的知识总结
1.RequestMapping
(1)注释类
(2)注释类中的方法
类注释,则类是相当于根目录 ; 若类不注释,则方法中的注释相对根目录
2.RequestMapping的映射参数
(1)value(请求URL)
(2)method(请求方法)
(3)params(请求参数)
params={
param1(表示必须包含此参数),
!param1(表示必须不能包含此参数)
param1 = str (表示必须包含这个参数,而且必须是str)
param1 != str (表示必须包含这个参数,但是不能是str)
}
(4)heads(请求头)
请求头类似于上面的,不做多写
package com.springmvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping(“/Hello”)
@Controller
public class HelloController {
/*
* (1)value(请求URL)
* (2)method(请求方法)
* (3)params(请求参数)
* (4)heads(请求头)
*/
@RequestMapping(value="/home") /*(1)*/
public String index() {
return "/home/index";
}
@RequestMapping(value="/home2",method=RequestMethod.POST) /*(2)*/
public String index2() {
return "/home/index";
}
@RequestMapping(value="/home3",params={"username","age!=10"}) /*(2)*/
public String index3() {
return "/home/index";
}
}
(5)Ant风格的正则表达式映射
- “?” : 代表一个字符
- “*” : 代表所有字符
“**”: 匹配多层路径
package com.springmvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@RequestMapping(“/Hello”)
@Controller
public class HelloController {/** * ant风格的正则表达式映射 */ @RequestMapping("/home4/*/abc") public String index4(){ return "/home/index"; }
}
(6)@PathVariable的使用,字段占位符
package com.springmvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/Hello")
@Controller
public class HelloController {
/**
* @PathVariable 中占位符的使用
*/
@RequestMapping("/home5/{id}")
public String index5(@PathVariable("id") Integer id) {
System.out.println("@PathVariable"+id);
return "/home/index";
}
}
(7)REST风格的映射
曾删改查:
GET : order/1 //获取
DELETE: order/1 //删除
PUT : order/1 //修改
POST : order //增加
发送PUT或者DELETE的请求是,要多发送一个隐藏域name=”_method”,value=”DELETE/PUT”
配置web.xml
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
编写java类
package com.springmvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@RequestMapping(“/Hello”)
@Controller
public class HelloController {/** * HiddenHttpMethodFilter 转换GET */ @RequestMapping(value="/home6/{id}",method=RequestMethod.GET) public String index6(@PathVariable("id") Integer id) { System.out.println("filter Get:"+id); return "/home/index"; } /** * HiddenHttpMethodFilter 转换POST */ @RequestMapping(value="/home7",method=RequestMethod.POST) public String index7() { System.out.println("filter POST:"); return "/home/index"; } /** * HiddenHttpMethodFilter 转换DELETE */ @RequestMapping(value="/home8/{id}",method=RequestMethod.PUT) public String index8(@PathVariable("id") Integer id) { System.out.println("filter PUT:"+id); return "/home/index"; } /** * HiddenHttpMethodFilter 转换PUT */ @RequestMapping(value="/home9/{id}",method=RequestMethod.DELETE) public String index9(@PathVariable("id") Integer id) { System.out.println("filter DELETE:"+id); return "/home/index"; }
}
(8)请求参数(@RequestParam),获取请求参数
package com.springmvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping("/Hello")
@Controller
public class HelloController {
@RequestMapping("/home10")
public String index10(@RequestParam("username") String un,@RequestParam(value="age",required=false,defaultValue="0") Integer age) {
System.out.println("@RequestParam:"+un+","+age);
return "/home/index";
}
}