1.@RequestMappering
@RequestMapping是一个用来处理请求地址到处理器controller功能方法映射规则的注解,这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法controller上,可用于类或方法上。注解在类上,表示类中的所有响应请求的方法都是以该地址作为父路径(模块路径)。
意思是说这个注解可以处理客户端请求的地址,将地址映射到类上或者方法上面,
1.url路径映射:
@RequestMapping("/xxx")
@RequestMapping(value = "/xxx")
@RequestMapping(path = "/xxx")
@RequestMapping(value = {"/xxx","/yyy"})//可以将多个url映射到同于1个类上或方法上
@RequestMapping(path = {"/xxx","/yyy"})//可以将多个url映射到同于1个类上或方法上
//method属性是指定请求类型,GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等
@RequestMapping (path = "/xxx",method = RequestMethod.GET)
1.只注解在方法上面:
访问路径:http://localhost:8080 / 项目名+RequestMapping注解参数
@Controller//代表这个类会被spring接管 被这个注解的类中的所有方法如果值是String 并且有具体的页面可以跳转,就会被视图器解析
public class ControllerTest1{
@RequestMapping("/t1")//请求地址 等价于@RequestMapping(value="/t1")、@RequestMapping(path="/t1")
public String test1(Model model){
model.addAttribute("msg","ControllerTest1test1");
return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.sp
}
//访问地址:http://localhost:8080/t1
@RequestMapping("/t2")//请求地址
public String test2(Model model){
model.addAttribute("msg","ControllerTest1test2");
return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.jsp
}
//访问地址:http://localhost:8080/t2
}
http://localhost:8080/t1
http://localhost:8080/t2
2.同时注解在类名和方法名上面
访问路径:http://localhost:8080 / 项目名+类注解参数 +方法注解参数 , 需要先指定类的路径再指定方法的路径;
@RequestMapping("t/")
public class ControllerTest2 {
@RequestMapping("/t1")//请求地址
public String test1(Model model){
model.addAttribute("msg","ControllerTest2test1");
return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.jsp
}
//访问地址:http://localhost:8080/t/t1
@RequestMapping("/t2")//请求地址
public String test2(Model model){
model.addAttribute("msg","ControllerTest2test2");
return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.jsp
}
//访问地址:http://localhost:8080/t/t2
}
http://localhost:8080/t/t1
http://localhost:8080/t/t2
3.多个url映射到同于1个类上或方法上
//其中的path可换成value
@Controller
@RequestMapping(path={"/c","/d"})
public class ControllerTest3 {
@RequestMapping(path={"/c3/t1","/c3/t11"})
public String test1(Model model){
model.addAttribute("msg","ControllerTest3");
return "test";
}
}
//访问地址:http://localhost:8080/c/c3/t1、http://localhost:8080/c/c3/t11
//http://localhost:8080/d/c3/t1、http://localhost:8080/d/c3/t11
2.RestFul风格
概念
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能
资源:互联网所有的事物都可以被抽象为资源
资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
分别对应 添加、 删除、修改、查询。
传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get
http://127.0.0.1/item/queryItem.action?id=1 查询,GET
http://127.0.0.1/item/saveItem.action 新增,POST
http://127.0.0.1/item/updateItem.action 更新,POST
http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST
使用RESTful操作资源 :可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!
http://127.0.0.1/item/1 查询,GET
http://127.0.0.1/item 新增,POST
http://127.0.0.1/item 更新,PUT
http://127.0.0.1/item/1 删除,DELETE
1.新建一个类Controller
@Controller
public class RestfulController {
}
2.使用 @PathVariable将 URL 中占位符参数绑定到控制器处理方法的入参中
//Restful:http://localhost:8080/add/a/b
//映射访问路径,通过GET方式提交的请求:
@RequestMapping (path = "add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a, @PathVariable String b, Model model){
String res=a+b;
model.addAttribute("msg","结果1:"+res);
return "test";
}
}
测试:
访问地址:http://localhost:8080/add/1/2
浏览器默认使用Get方式提交请求,所以能显示结果
将method改为post
//映射访问路径,POST方式提交
@RequestMapping (path = "add/{a}/{b}",method = RequestMethod.POST)
public String test1(@PathVariable int a, @PathVariable String b, Model model){
String res=a+b;
model.addAttribute("msg","结果1:"+res);
return "test";
}
设置请求方式设置为Get 所以post请求不被允许执行方法
与
@RequestMapping(method = RequestMethod.XXX)
等价的组合注解:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
//映射访问路径,Get方式提交
@RequestMapping (path = "add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a, @PathVariable String b, Model model){
String res=a+b;
model.addAttribute("msg","结果1:"+res);
return "test";
}
//或
//@GetMapping ("add/{a}/{b}")
//当映射路径中的参数与控制器方法中的参数名不同时
//public String test2(@PathVariable("a") int a1, @PathVariable("b") String b1, Model model){
// String res=a1+b1;
// model.addAttribute("msg","结果2:"+res);
// return "test";
//}