SpringMVC中的@RequestMapping

1.类上与方法上都可以

@RequestMapping("/user")
@Controller
public class requesttest {
    @RequestMapping(value = "/registe",method = RequestMethod.GET)
    public String registe(){
        System.out.println("registe~ok");
        return "login_ok";
    }
}

2.可以指定请求方式(不指定method默认为post和get,指定后要按需请求,不然报错)

    @RequestMapping(value = "/type",method = RequestMethod.POST)
    public String type(){
        System.out.println("successtype~ok");
        return "success";
    }

image

3.可以指定params

    @RequestMapping(value = "/params",params = "bookId=100",method = RequestMethod.GET)
    public String params(String bookId){
        System.out.println("bookId="+bookId);
        return "success";
    }

给出参数和参数值,若参数值不一致也会报错
image

4.支持Ant风格资源地址

1.?匹配文件名中的一个字符
2.匹配文件名中的任意字符
3.**匹配多层路径

    @RequestMapping(value = "/path/**",method = RequestMethod.GET)
    public String path(){
        System.out.println("多层路径");
        return "success";
    }

5.@PathVariable映射URL绑定占位符,不需要url带参数了,更加简洁

    @RequestMapping(value = "/path/{username}/{age}",method = RequestMethod.GET)
    public String pathVariable(@PathVariable("username") String name,@PathVariable("age") String age){
        System.out.println("username=" +name
        + "age="+age);
        return "success";
    }

注意事项

1,映射url不能重复
2.请求的简写形式
@GetMapping @PostMapping等等
3.若前端提交的字段和后台要求的参数名一致,不用写params

    @RequestMapping(value = "/mypath",method = RequestMethod.GET)
    public String myEmail(String email){
        System.out.println("email="+email);
        return "success";
    }
posted @ 2024-03-17 21:15  尤所不同  阅读(16)  评论(0编辑  收藏  举报