RestFul就是一个资源定位及资源操作的风格,基于这种风格设计的软件可以更简洁,更有层次,更易于实现缓存机制。
@Controller
public class RestFulController {
//第一种在RequestMapping中加入method参数
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.POST)
public String test(@PathVariable int a, @PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为:"+result);
return "test";
}
//第二种直接使用XXXMaping注解
@GetMapping("/add/{a}/{b}")
public String test2(@PathVariable int a, @PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为2 :"+result);
return "test";
}
}
默认走的是get方法,post方法的话可以增加一个a.jsp实现
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/add/1/2" method="post">
<input type="submit">
</form>
</body>
</html>
虽然后续路径都是localhost:8080/add/1/2,但是提交方式确实不同的。
无视图解析器时的转发和重定向
@Controller
public class ModelTest01 {
@RequestMapping("/m1/t1")
public String test(Model model){
//没有视图解析器的情况下
//转发
model.addAttribute("msg","modelTest01");
// return "forward:/WEB-INF/jsp/test.jsp";
//重定向
return "redirect:/index.jsp";
}
}
有视图解析器默认是走的转发,url不发生变化