restful 风格

maoshine / 2023-08-23 / 原文

代码示例:

  • 访问路径:http://localhost:8080/t1/Xiaoming
    http://localhost:8080/t2/1/2

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class Test1 {
    @RequestMapping("/t1/{name}")
    public String test1(@PathVariable String name, Model model){
        model.addAttribute("msg","name: "+name);
        return "test";
    }
    @RequestMapping("/t2/{p1}/{p2}")
    public String test2(@PathVariable int p1,@PathVariable int p2, Model model){
        model.addAttribute("msg","Result: "+ (p1+p2));
        return "test";
    }
}