`
du_bo
  • 浏览: 39440 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类

Spring mvc 构造RESTful URL 详细讲解

 
阅读更多

springmvc 3.0 中增加 RESTful URL功能,构造出类似javaeye现在的URL。 rest介绍 , 这里还有struts2 rest构造的一篇文章: 使用 Struts 2 开发 RESTful 服务

简单例子如下,比如如下URL

Java代码 
  1. /blog/1  HTTP GET =>    得到id = 1的blog  
  2. /blog/1  HTTP DELETE => 删除 id = 1的blog  
  3. /blog/1  HTTP PUT  =>   更新id = 1的blog  
  4. /blog     HTTP POST =>   新增BLOG  

 

 

以下详细解一下spring rest使用.

 

首先,我们带着如下个问题查看本文。
1. 如何在java构造没有扩展名的RESTful url,如 /forms/1,而不是 /forms/1.do

2. 由于我们要构造没有扩展名的url本来是处理静态资源的容器映射的,现在被我们的spring占用了,冲突怎么解决?
3. 浏览器的form标签不支持提交delete,put请求,如何曲线解决?

 

springmvc rest 实现


springmvc的resturl是通过@RequestMapping 及@PathVariable annotation提供的,通过如@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)即可处理/blog/1 的delete请求.

Java代码 
  1. @RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)  
  2. public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {  
  3.     blogManager.removeById(id);  
  4.     return new ModelAndView(LIST_ACTION);  
  5. }  

 

@RequestMapping @PathVariable如果URL中带参数,则配合使用,如

Java代码 
  1. @RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)  
  2. public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {  
  3. }  

 

 spring rest配置指南

1. springmvc web.xml配置

Xml代码 
  1. <!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->  
  2. <servlet-mapping>  
  3.     <servlet-name>default</servlet-name>  
  4.     <url-pattern>/static  
  5. @Controller  
  6. @RequestMapping("/userinfo")  
  7. public class UserInfoController extends BaseSpringController{  
  8.     //默认多列排序,example: username desc,createTime asc  
  9.     protected static final String DEFAULT_SORT_COLUMNS = null;   
  10.       
  11.     private UserInfoManager userInfoManager;  
  12.       
  13.     private final String LIST_ACTION = "redirect:/userinfo";  
  14.       
  15.       
  16.     public void setUserInfoManager(UserInfoManager manager) {  
  17.         this.userInfoManager = manager;  
  18.     }  
  19.       
  20.       
  21.     @RequestMapping  
  22.     public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {  
  23.         PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);  
  24.         //pageRequest.getFilters(); //add custom filters  
  25.           
  26.         Page page = this.userInfoManager.findByPageRequest(pageRequest);  
  27.         savePage(page,pageRequest,request);  
  28.         return new ModelAndView("/userinfo/list","userInfo",userInfo);  
  29.     }  
  30.       
  31.       
  32.     @RequestMapping(value="/new")  
  33.     public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {  
  34.         return new ModelAndView("/userinfo/new","userInfo",userInfo);  
  35.     }  
  36.       
  37.       
  38.     @RequestMapping(value="/{id}")  
  39.     public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
  40.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);  
  41.         return new ModelAndView("/userinfo/show","userInfo",userInfo);  
  42.     }  
  43.       
  44.       
  45.     @RequestMapping(value="/{id}/edit")  
  46.     public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
  47.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);  
  48.         return new ModelAndView("/userinfo/edit","userInfo",userInfo);  
  49.     }  
  50.       
  51.       
  52.     @RequestMapping(method=RequestMethod.POST)  
  53.     public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {  
  54.         userInfoManager.save(userInfo);  
  55.         return new ModelAndView(LIST_ACTION);  
  56.     }  
  57.       
  58.       
  59.     @RequestMapping(value="/{id}",method=RequestMethod.PUT)  
  60.     public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
  61.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);  
  62.         bind(request,userInfo);  
  63.         userInfoManager.update(userInfo);  
  64.         return new ModelAndView(LIST_ACTION);  
  65.     }  
  66.       
  67.       
  68.     @RequestMapping(value="/{id}",method=RequestMethod.DELETE)  
  69.     public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {  
  70.         userInfoManager.removeById(id);  
  71.         return new ModelAndView(LIST_ACTION);  
  72.     }  
  73.   
  74.       
  75.     @RequestMapping(method=RequestMethod.DELETE)  
  76.     public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {  
  77.           
  78.         for(int i = 0; i < items.length; i++) {  
  79.               
  80.             userInfoManager.removeById(items[i]);  
  81.         }  
  82.         return new ModelAndView(LIST_ACTION);  
  83.     }  
  84.       
  85. }  

 

 

上面是rapid-framework 新版本生成器生成的代码,以后也将应用此规则,rest url中增删改查等基本方法与Controller的方法映射规则

Java代码 
  1. /userinfo           => index()  
  2. /userinfo/new       => _new()  
  3. /userinfo/{id}      => show()  
  4. /userinfo/{id}/edit         => edit()  
  5. /userinfo   POST        => create()  
  6. /userinfo/{id}  PUT => update()  
  7. /userinfo/{id}  DELETE  => delete()  
  8. /userinfo   DELETE      => batchDelete()  

 注(不使用 /userinfo/add  => add() 方法是由于add这个方法会被maxthon浏览器当做广告链接过滤掉,因为包含ad字符)

 

4. jsp 编写

Html代码 
  1. <form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">  
  2. </form:form>  

 生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求

Java代码 
  1. <form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">  
  2.     <input type="hidden" name="_method" value="put"/>  
  3. </form>  

 

另外一种方法是你可以使用ajax发送put,delete请求.

 

5. 静态资源的URL重写

   如上我们描述,现因为将default servlet映射至/static/的子目录,现我们访问静态资源将会带一个/static/前缀.

   如 /foo.gif, 现在访问该文件将是 /static/foo.gif.
   那如何避免这个前缀呢,那就是应用URL rewrite,现我们使用 http://tuckey.org/urlrewrite/, 重写规则如下

 

Xml代码 
  1. <urlrewrite>  
  2.     <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->  
  3.     <rule>  
  4.         <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>  
  5.         <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>  
  6.         <from>^(/.*\..*)$</from>  
  7.         <to>/static$1</to>  
  8.     </rule>  
  9. </urlrewrite>  

转自:http://blog.sina.com.cn/s/blog_3eb0a8bc0100qmtf.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics