1. restful编程风格
1.1 原来的风格
UserController类—save/update/findAll方法
请求路径:
path=”/user/save”——->save
path=”/user/update”——->update
path=”/user/findAll”——->findAll
1.2 restful方式
UserController类—save/update/findAll方法
请求路径:
path=”/user” method=”post” ——> save
path=”/user” method=”put” ——> update
path=”/user” method=”get” ——> findAll
path=”/user/{id}” method=”get” ——> findById(id)
2. restful实现方式
2.1 困难
由于浏览器 form 表单只支持 GET 与 POST 请求,而 DELETE、PUT 等 method 并不支持
2.2 解决方法
2.2.1 使用spring 3.0的过滤器—— HiddentHttpMethodFilter
第一步:在 web.xml 中配置该过滤器。
第二步:请求方式必须使用 post 请求。
第三步:按照要求提供_method 请求参数,该参数的取值就是我们需要的请求方式。
<!-- 保存 --><form action="springmvc/testRestPOST" method="post">用户名称:<input type="text" name="username"><br/><!-- <input type="hidden" name="_method" value="POST"> --><input type="submit" value=" 保存 "></form><hr/><!-- 更新 --><form action="springmvc/testRestPUT/1" method="post">用户名称:<input type="text" name="username"><br/><input type="hidden" name="_method" value="PUT"><input type="submit" value=" 更新 "></form>
/*** post 请求:保存* @param username* @return*/@RequestMapping(value="/testRestPOST",method=RequestMethod.POST)public String testRestfulURLPOST(User user){System.out.println("rest post"+user);return "success";}/*** put 请求:更新* @param username* @return*/@RequestMapping(value="/testRestPUT/{id}",method=RequestMethod.PUT)public String testRestfulURLPUT(@PathVariable("id")Integer id,User user){System.out.println("rest put "+id+","+user);return "success";}
