控制器和RestFul
控制器和RestFul
控制器Controller
- 控制器复杂提供访问应用程序的行为,通常通过接口或注解定义两种方法实现。
- 控制器负责解析用户的请求并将其转换为一个模型。
- 在Spring MVC中一个控制器类可以包含多个方法
- 在Spring MVC中,对于Controller的配置方式有很多种
注解@Controller
-
@Controller注解类型用于声明Spring类的实例是一个控制器(在讲IOC时还提到了另外3个注解Component,Service,Repository);
-
Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类,为了保证Spring能找到你的控制器,需要在配置文件中声明组件扫描。
<!-- 自动扫描指定的包,下面所有注解类交给IOC容器管理 --> <context:component-scan base-package="com.controller"/>
-
增加一个ControllerTest类,使用注解实现;
//@Controller注解的类会自动添加到Spring上下文中 @Controller//代表这个类会被Spring接管,这个类中的方法如果返回值是String,并且有具体的页面可以跳转,那么就会被视图解析器解析; public class ControllerTest{ //映射访问路径 @RequestMapping("/test") public String test(Model model){ //Spring MVC会自动实例化一个Model对象用于向视图中传值 model.addAttribute("msg", "ControllerTest"); //返回视图位置 return "test"; //前缀/WEB-INF/jsp/ test +后缀 .jsp } }
RequestMapping
@RequestMapping
-
@RequestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
-
只注解在方法上面
@Controller public class TestController { @RequestMapping("/h1") public String test(){ return "test"; } }
访问路径:http://localhost:8080 / h1
-
同时注解类与方法
@Controller @RequestMapping("/admin") public class TestController { @RequestMapping("/h1") public String test(){ return "test"; } }
访问路径:http://localhost:8080 / admin /h1 , 需要先指定类的路径再指定方法的路径;
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
@Controller
public class ControllerTest{
@RequestMapping("/add")
//原来的 http://localhost:8080/add?a=2&b=2
public String test1(int a,int b,Model model){
int res=a+b;
model.addAttribute("msg","结果为"+res);
return "test";
}
//RestFul风格
@RequestMapping("/add2/{a}/{b}")
//现在的的 http://localhost:8080/add2/1/2
public String test2(@PathVariable int a,@PathVariable String b, Model model){
String result=a+b;
model.addAttribute("msg","结果为"+result);
return "test";
}
}
@GetMapping 是一个组合注解,平时使用的会比较多!
它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。
@Controller
public class RestfulController {
//RestFul风格 通过不同的method 提交方式实现同一个URL而实现不同功能
//第一种 : @RequestMapping(value="/book",method= RequestMethod.GET)
//第二种: @GetMapping(value="/book")
@GetMapping(value="/book")
public String test( Model model){
String result="查询全部图书";
model.addAttribute("msg","结果为"+result);
return "test";
}
@PostMapping("/book/{a}")
public String test2(@PathVariable int a, Model model){
String result="post请求添加图书";
model.addAttribute("msg","结果为"+result);
return "test";
}
@PutMapping("/book/{a}")
public String test3(@PathVariable int a, Model model){
String result="修改图书";
model.addAttribute("msg","结果为"+result);
return "test";
}
@DeleteMapping("/book/{a}")
public String test4(@PathVariable int a, Model model){
String result="删除图书";
model.addAttribute("msg","结果为"+result);
return "test";
}
}
界面
<html>
<head>
<title>RestFul风格</title>
</head>
<body>
<form action="/book" method="get">
<input type="submit" value="get添加图书"/>
</form>
<form action="/book/1" method="post">
<input type="submit" value="post添加图书"/>
</form>
<form action="/book/1" method="post">
<!-- 表示这是put请求 -->
<input type="hidden" name="method" value="PUT"/>
<input type="submit" value="put修改图书"/>
</form>
<form action="/book/1" method="post">
<!-- 表示这是DELETE请求 -->
<input type="hidden" name="method" value="DELETE"/>
<input type="submit" value="DELETE 删除图书"/>
</form>
</body>
</html>