SpringMVC RestFul方式提交

先来说下什么是RestFul的请求方式

在 REST 样式的 Web 服务中,每个资源都有一个地址。资源本身都是方法调用的目标,方法列表对所有资源都是一样的。这些方法都是标准方法,包括 HTTP GET、POST、PUT、DELETE,还可能包括 HEADER 和 OPTIONS

GET: testRest/1 – 获取资源,id=1
POST: testRest –新增资源
PUT: testRest/1 –修改资源,id=1
DELETE: testRest/1 –删除资源,id=1
传统的Form表单只支持GET和Post方式,SpringMVC提供了一个过滤器HiddenHttpMethodFilter,可以将页面标识的put和delete方式转换为RestFul方式

配置HiddenHttpMethodFilter

这里使用JavaConfig配置,在web.xml中也可以配置

public class SplitterWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    @Override
    protected Filter[] getServletFilters() {
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        return new Filter[]{hiddenHttpMethodFilter};
    }
}

AbstractAnnotationConfigDispatcherServletInitializerWebApplicationInitializer 的子类,如果要用JavaConfig来配置SpringMVC,需要继承
AbstractAnnotationConfigDispatcherServletInitializer
或实现接口WebApplicationInitializer

定义对应的方法

使用RestFul风格的话要用注解@PathVariable和占位符,占位符中的名称必须和@PathVariable中的value值一致

   @RequestMapping(value = "/testRest/{id}",method = RequestMethod.GET)
    public String testRest(@PathVariable(value = "id") Integer id){
        System.out.println("testRest的GET:"+id);
        return "info";
    }
    @RequestMapping(value = "/testRest",method = RequestMethod.POST)
    public String testRest(){
        System.out.println("testRest的POST:");
        return "info";
    }
    @RequestMapping(value = "/testRest/{id}",method = RequestMethod.DELETE)
    public String testRestDelete(@PathVariable(value = "id") Integer id){
        System.out.println("testRest的DELETE:"+id);
        return "info";
    }
    @RequestMapping(value = "/testRest/{id}",method = RequestMethod.PUT)
    public String testRestPut(@PathVariable(value = "id") Integer id){
        System.out.println("testRest的Put:"+id);
        return "info";
    }

上面定义了4中方式对应的处理方法

用form表单实现PUT,POST,DELETE,GET

GET提交

<a href="splitter/testRest/1">splitter/testRest/1</a><P></P>

POST提交

 <form action="splitter/testRest" method="post">
       <input type="submit" value="提交POST"/>
 </form>

PUT提交
注意不能直接在form的method=”put”,这样浏览器是不支持的,使用一个隐藏域,它的value指定RestFul的方式,当springmvc的过滤器收到后,再进行相关转换

<form action="splitter/testRest/1" method="post">
    <input type="hidden" name="_method" value="PUT"/>
    <input type="submit" value="提交put"/>
</form>

DELET提交

<form action="splitter/testRest/1" method="post">
    <input type="hidden" name="_method" value="DELETE"/>
    <input type="submit" value="提交DELETE"/>
</form>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值