【SpringMVC】使用 @RequestMapping 映射请求

时间:2020-08-15 23:54:15   收藏:0   阅读:84

使用 @RequestMapping 映射请求

使用 @RequestMapping 映射请求示例

@Controller
//类定义处标记的 @RequestMapping 限定了处理 器类可以处理所有 URI 为 /hello 的请求,它相对于 WEB 容器部署的根路径
@RequestMapping("/hello")
public class HelloWorld {
    //处理器类可以定义多个处理方法,处理来自/hello 下的请求
    @RequestMapping("/SpringMVC")
    public String helloworld() {
        System.out.println("helloworld");
        return "success";
    }
}

映射请求参数、请求方法或请求头

@RequestMapping(value="/delete"),method=RequestMethod.POST,params="userId")
public String test1() {
    //...
    return "user/test1";
}
@RequestMapping(value="/show"),headers="contentType=text/*")
public String test2() {
    //...
    return "user/test2";
}

使用 @RequestMapping 映射请求

Ant 风格资源地址支持 3 种匹配符:

@RequestMapping 还支持 Ant 风格的 URL:

@PathVariable 映射 URL 绑定的占位符

@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
    UserDao.delete(id);
    return "redirect:/user/list.action";
}

REST

REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用


示例:

HiddenHttpMethodFilter:浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。

@PathVariable 绑定 URL 占位符到入参

@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
    UserDao.delete(id);
    return "redirect:/user/list.action";
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!