一.Spring MVC实现Controller的多种方法
主要有三种:
-
实现接口Controller定义控制器是较老的办法:
-
缺点是:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比较麻烦;
-
使用注解@Controller:(重点)
-
RestFul风格:(重点)
-
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
1.实现接口Controller定义控制器是较老的办法
步骤:
-
spring-04-controller子项目中创建控制器
-
创建视图
-
测试
spring-04-controller子项目中创建控制器
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//定义控制器
//注意点:不要导错包,实现Controller接口,重写方法;
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","Test1Controller");
mv.setViewName("test");
return mv;
}
}
创建视图
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
${msg}
</body>
</html>
2.使用注解@Controller
步骤:
-
springmvc-servlet.xml中添加注解
-
编写控制器
springmvc-servlet.xml中添加注解
<!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
<context:component-scan base-package="ustc.wzh.controller"/>
编写控制器
-
RequestMapping可以定义在类上和方法上来定义访问路径:访问路径:http://localhost:8080 / 项目名 / 类名上的路径/方法上的路径
-
返回类型为String默认表示为视图
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//@Controller注解的类会自动添加到Spring上下文中
@Controller
public class ControllerTest2{
//映射访问路径
@RequestMapping("/t2")
public String index(Model model){
//Spring MVC会自动实例化一个Model对象用于向视图中传值
model.addAttribute("msg", "ControllerTest2");
//返回视图位置
return "test";
}
}
- 可以发现,我们的两个请求都可以指向一个视图,但是页面结果的结果是不一样的,从这里可以看出视图是被复用的,而控制器与视图之间是弱偶合关系。
3.RestFul 风格
(1)功能
-
资源:互联网所有的事物都可以被抽象为资源
-
资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
-
分别对应 添加、 删除、修改、查询。
(2)传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,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
(3)使用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
(4)步骤:
-
编写控制器
-
测试
编写控制器
-
@RequestMapping("/commit/{p1}/{p2}") 与 @PathVariable int p1, @PathVariable int p2 对应使用生成RestFul风格
-
method = {RequestMethod.GET} 表示使用的是什么请求方式
-
使用method属性指定请求类型
-
用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等
//@Controller注解的类会自动添加到Spring上下文中
@Controller
@RequestMapping("/Test3")
public class ControllerTest3{
//映射访问路径:RestFul风格
@RequestMapping("/commit/{p1}/{p2}")
public String index(@PathVariable int p1, @PathVariable int p2, Model model){
int result = p1+p2;
//Spring MVC会自动实例化一个Model对象用于向视图中传值
model.addAttribute("msg", "结果:"+result);
//返回视图位置
return "test";
}
//映射访问路径,必须是Get请求:RestFul风格
@RequestMapping(value = "/hello",method = {RequestMethod.GET})
public String index2(Model model){
model.addAttribute("msg", "hello!");
return "test";
}
}
(5)补充
-
所有的地址栏请求默认都会是 HTTP GET 类型的。
-
方法级别的注解变体有如下几个: 组合注解
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@GetMapping("/getMapping/hello")
public String index3(Model model){
model.addAttribute("msg","getMapping");
return "test";
}