SpringMVC:restFul风格学习笔记
以前通过url传参需要:localhost:8080/t1?a=1&b=2
这样写url.
使用restFul风格:localhost:8080/t1/1/2
让你的url看起来相对简洁和相对安全
package com.huang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class RestFulTest01 {
// @RequestMapping(value = "/t1/{a}/{b}",method= RequestMethod.GET)等价于下面这种
@GetMapping("/t1/{a}/{b}")
//@PathVariable设置restFul风格url传参
public String test(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","@GetMapping结果为:"+result);
return "Test";
}
@PostMapping("/t1/{a}/{b}")
// @DeleteMapping
// @PutMapping
public String test2(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","@PostMapping结果为:"+result);
//return "redirect:/index.jsp";重定向
return "Test";
}
}