亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

spring 3.0 應(yīng)用springmvc 構(gòu)造RESTful URL 詳

系統(tǒng) 1885 0

在線springmvc_rest demo

?

?

由于下一版本的 rapid-framwork 需要集成spring RESTful URL,所以研究了一下怎么搭建. 并碰到了一下問(wèn)題。

?

springmvc 3.0 中增加 RESTful URL功能,構(gòu)造出類似javaeye現(xiàn)在的URL。 rest介紹 ?, 這里還有struts2 rest構(gòu)造的一篇文章:? 使用 Struts 2 開發(fā) RESTful 服務(wù)

簡(jiǎn)單例子如下,比如如下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??

?

?

以下詳細(xì)解一下spring rest使用.

?

首先,我們帶著如下 個(gè)問(wèn)題 查看本文。
1. 如何在java構(gòu)造沒有擴(kuò)展名的RESTful url,如 /forms/1,而不是 /forms/1.do

2. 由于我們要構(gòu)造沒有擴(kuò)展名的url本來(lái)是處理靜態(tài)資源的容器映射的,現(xiàn)在被我們的spring占用了,沖突怎么解決?
3. 瀏覽器的form標(biāo)簽不支持提交delete,put請(qǐng)求,如何曲線解決?

?

springmvc rest 實(shí)現(xiàn)


springmvc的resturl是通過(guò)@RequestMapping 及@PathVariable annotation提供的,通過(guò)如@RequestMapping(value="/blog /{id}",method=RequestMethod.DELETE)即可處理/blog/1 的delete請(qǐng)求.

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中帶參數(shù),則配合使用,如

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等容器提供,將靜態(tài)資源映射從/改為/static/目錄,如原來(lái)訪問(wèn)?http://localhost /foo.css?,現(xiàn)在http://localhost/static/foo.css?--> ??
  2. < servlet-mapping > ??
  3. ???? < servlet-name > default </ servlet-name > ??
  4. ???? < url-pattern > /static/* </ url-pattern > ??
  5. </ servlet-mapping > ??
  6. < servlet > ??
  7. ???? < servlet-name > springmvc </ servlet-name > ??
  8. ???? < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class > ??
  9. ???? < load-on-startup > 1 </ load-on-startup > ??
  10. </ servlet > ??
  11. ??
  12. <!--?URL重寫filter,用于將訪問(wèn)靜態(tài)資源http://localhost/foo.css?轉(zhuǎn)為http://localhost/static/foo.css?--> ??
  13. < filter > ??
  14. ???? < filter-name > UrlRewriteFilter </ filter-name > ??
  15. ???? < filter-class > org.tuckey.web.filters.urlrewrite.UrlRewriteFilter </ filter-class > ??
  16. ???? < init-param > ??
  17. ???????????? < param-name > confReloadCheckInterval </ param-name > ??
  18. ???????????? < param-value > 60 </ param-value > ??
  19. ???????? </ init-param > ??
  20. ???? < init-param > ??
  21. ???????????????? < param-name > logLevel </ param-name > ??
  22. ???????????????? < param-value > DEBUG </ param-value > ??
  23. ???????? </ init-param > ???????
  24. </ filter > ??
  25. < filter-mapping > ??
  26. ???? < filter-name > UrlRewriteFilter </ filter-name > ??
  27. ???? < url-pattern > /* </ url-pattern > ??
  28. </ filter-mapping > ??
  29. ??
  30. <!--?覆蓋default?servlet的/,?springmvc?servlet將處理原來(lái)處理靜態(tài)資源的映射?--> ??
  31. < servlet-mapping > ??
  32. ???? < servlet-name > springmvc </ servlet-name > ??
  33. ???? < url-pattern > / </ url-pattern > ??
  34. </ servlet-mapping > ??
  35. ??
  36. <!--?瀏覽器不支持put,delete等method,由該filter將/blog?_method=delete轉(zhuǎn)換為標(biāo)準(zhǔn)的http?delete方法?--> ??
  37. < filter > ??
  38. ???? < filter-name > HiddenHttpMethodFilter </ filter-name > ??
  39. ???? < filter-class > org.springframework.web.filter.HiddenHttpMethodFilter </ filter-class > ??
  40. </ filter > ??
  41. ??
  42. < filter-mapping > ??
  43. ???? < filter-name > HiddenHttpMethodFilter </ filter-name > ??
  44. ???? < servlet-name > springmvc </ servlet-name > ??
  45. </ filter-mapping > ??

?

?

2. webapp/WEB-INF/springmvc-servlet.xml配置,使用如下兩個(gè)class激活@RequestMapping annotation

Java代碼 ? 收藏代碼
  1. <bean? class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />??
  2. <bean? class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />??

?

完整配置

Java代碼 ? 收藏代碼
  1. <beans? default -autowire= "byName" ???>??
  2. ??
  3. ????<!--?自動(dòng)搜索 @Controller 標(biāo)注的類?-->??
  4. ????<context:component-scan?base- package = "com.**.controller" />??
  5. ??????
  6. ????<bean? class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />??
  7. ??
  8. ????<bean? class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />??
  9. ??
  10. ????<!--?Default?ViewResolver?-->??
  11. ????<bean?id= "viewResolver" ? class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >??
  12. ????????<property?name= "viewClass" ?value= "org.springframework.web.servlet.view.JstlView" />??
  13. ????????<property?name= "prefix" ?value= "/pages" />??
  14. ????????<property?name= "suffix" ?value= ".jsp" ></property>??
  15. ????</bean>??
  16. ??????
  17. ????<bean?id= "messageSource" ? class = "org.springframework.context.support.ResourceBundleMessageSource" ?p:basename= "i18n/messages" />??
  18. ??
  19. ????<!--?Mapping?exception?to?the?handler?view?-->??
  20. ????<bean?id= "exceptionResolver" ? class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >??
  21. ????????<!--?to?/commons/error.jsp?-->??
  22. ????????<property?name= "defaultErrorView" ?value= "/commons/error" />??
  23. ????????<property?name= "exceptionMappings" >??
  24. ????????????<props>??
  25. ????????????</props>??
  26. ????????</property>??
  27. ????</bean>??
  28. ??????????
  29. </beans>??

?

?

3. Controller編寫

Java代碼 ? 收藏代碼
  1. /** ?
  2. ?*?@RequestMapping("/userinfo")?具有層次關(guān)系,方法級(jí)的將在類一級(jí)@RequestMapping之一, ?
  3. ?*?如下面示例,?訪問(wèn)方法級(jí)別的@RequestMapping("/new"),則URL為?/userinfo/new ?
  4. ?*/ ??
  5. @Controller ??
  6. @RequestMapping ( "/userinfo" )??
  7. public ? class ?UserInfoController? extends ?BaseSpringController{??
  8. ???? //默認(rèn)多列排序,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. ?????*?通過(guò)spring自動(dòng)注入 ?
  17. ?????**/ ??
  18. ???? public ? void ?setUserInfoManager(UserInfoManager?manager)?{??
  19. ???????? this .userInfoManager?=?manager;??
  20. ????}??
  21. ??????
  22. ???? /**?列表?*/ ??
  23. ???? @RequestMapping ??
  24. ???? public ?ModelAndView?index(HttpServletRequest?request,HttpServletResponse?response,UserInfo?userInfo)?{??
  25. ????????PageRequest<Map>?pageRequest?=?newPageRequest(request,DEFAULT_SORT_COLUMNS);??
  26. ???????? //pageRequest.getFilters();?//add?custom?filters ??
  27. ??????????
  28. ????????Page?page?=? this .userInfoManager.findByPageRequest(pageRequest);??
  29. ????????savePage(page,pageRequest,request);??
  30. ???????? return ? new ?ModelAndView( "/userinfo/list" , "userInfo" ,userInfo);??
  31. ????}??
  32. ??????
  33. ???? /**?進(jìn)入新增?*/ ??
  34. ???? @RequestMapping (value= "/new" )??
  35. ???? public ?ModelAndView?_new(HttpServletRequest?request,HttpServletResponse?response,UserInfo?userInfo)? throws ?Exception?{??
  36. ???????? return ? new ?ModelAndView( "/userinfo/new" , "userInfo" ,userInfo);??
  37. ????}??
  38. ??????
  39. ???? /**?顯示?*/ ??
  40. ???? @RequestMapping (value= "/{id}" )??
  41. ???? public ?ModelAndView?show( @PathVariable ?Long?id,HttpServletRequest?request,HttpServletResponse?response)? throws ?Exception?{??
  42. ????????UserInfo?userInfo?=?(UserInfo)userInfoManager.getById(id);??
  43. ???????? return ? new ?ModelAndView( "/userinfo/show" , "userInfo" ,userInfo);??
  44. ????}??
  45. ??????
  46. ???? /**?編輯?*/ ??
  47. ???? @RequestMapping (value= "/{id}/edit" )??
  48. ???? public ?ModelAndView?edit( @PathVariable ?Long?id,HttpServletRequest?request,HttpServletResponse?response)? throws ?Exception?{??
  49. ????????UserInfo?userInfo?=?(UserInfo)userInfoManager.getById(id);??
  50. ???????? return ? new ?ModelAndView( "/userinfo/edit" , "userInfo" ,userInfo);??
  51. ????}??
  52. ??????
  53. ???? /**?保存新增?*/ ??
  54. ???? @RequestMapping (method=RequestMethod.POST)??
  55. ???? public ?ModelAndView?create(HttpServletRequest?request,HttpServletResponse?response,UserInfo?userInfo)? throws ?Exception?{??
  56. ????????userInfoManager.save(userInfo);??
  57. ???????? return ? new ?ModelAndView(LIST_ACTION);??
  58. ????}??
  59. ??????
  60. ???? /**?保存更新?*/ ??
  61. ???? @RequestMapping (value= "/{id}" ,method=RequestMethod.PUT)??
  62. ???? public ?ModelAndView?update( @PathVariable ?Long?id,HttpServletRequest?request,HttpServletResponse?response)? throws ?Exception?{??
  63. ????????UserInfo?userInfo?=?(UserInfo)userInfoManager.getById(id);??
  64. ????????bind(request,userInfo);??
  65. ????????userInfoManager.update(userInfo);??
  66. ???????? return ? new ?ModelAndView(LIST_ACTION);??
  67. ????}??
  68. ??????
  69. ???? /**?刪除?*/ ??
  70. ???? @RequestMapping (value= "/{id}" ,method=RequestMethod.DELETE)??
  71. ???? public ?ModelAndView?delete( @PathVariable ?Long?id,HttpServletRequest?request,HttpServletResponse?response)?{??
  72. ????????userInfoManager.removeById(id);??
  73. ???????? return ? new ?ModelAndView(LIST_ACTION);??
  74. ????}??
  75. ??
  76. ???? /**?批量刪除?*/ ??
  77. ???? @RequestMapping (method=RequestMethod.DELETE)??
  78. ???? public ?ModelAndView?batchDelete( @RequestParam ( "items" )?Long[]?items,HttpServletRequest?request,HttpServletResponse?response)?{??
  79. ??????????
  80. ???????? for ( int ?i?=? 0 ;?i?<?items.length;?i++)?{??
  81. ??????????????
  82. ????????????userInfoManager.removeById(items[i]);??
  83. ????????}??
  84. ???????? return ? new ?ModelAndView(LIST_ACTION);??
  85. ????}??
  86. ??????
  87. }??

?

?

上面是 rapid-framework 新版本生成器生成的代碼,以后也將應(yīng)用此規(guī)則,rest url中增刪改查等基本方法與Controller的方法映射規(guī)則

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這個(gè)方法會(huì)被maxthon瀏覽器當(dāng)做廣告鏈接過(guò)濾掉,因?yàn)榘琣d字符)

?

4. jsp 編寫

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

?生成的html內(nèi)容如下, 生成一個(gè)hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服務(wù)端將post請(qǐng)求改為put請(qǐng)求

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

?

另外一種方法是你可以使用ajax發(fā)送put,delete請(qǐng)求.

?

5. 靜態(tài)資源的URL重寫

?? 如上我們描述,現(xiàn)因?yàn)閷efault servlet映射至/static/的子目錄,現(xiàn)我們?cè)L問(wèn)靜態(tài)資源將會(huì)帶一個(gè)/static/前綴.

?? 如 /foo.gif, 現(xiàn)在訪問(wèn)該文件將是 /static/foo.gif.
?? 那如何避免這個(gè)前綴呢,那就是應(yīng)用URL rewrite,現(xiàn)我們使用 http://tuckey.org/urlrewrite/ , 重寫規(guī)則如下

?

Xml代碼 ? 收藏代碼
  1. < urlrewrite > ??
  2. ???? <!--?訪問(wèn)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 > ??

?? 另筆者專門寫了一個(gè) RestUrlRewriteFilter來(lái)做同樣的事件,以后會(huì)隨著 rapid-framework 一起發(fā)布. 比這個(gè)更加輕量級(jí).

?

并且 該代碼 已經(jīng)貢獻(xiàn)給spring,不知會(huì)不會(huì)在下一版本發(fā)布

?

?

在線DEMO地址 :? http://demo.rapid-framework.org.cn:8080/springmvc_rest_demo/userinfo

spring 3.0 應(yīng)用springmvc 構(gòu)造RESTful URL 詳細(xì)講解


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 久久久久免费 | 国产特黄一级一片免费 | 特级毛片免费视频观看 | 日本欧美久久久久免费播放网 | 日本高清中文字幕一区二区三区 | 尤物精品国产福利网站 | 亚洲在线精品视频 | 亚洲欧美综合区自拍另类 | 久热视线观看免费视频 | 亚洲精品色一区色二区色三区 | 免费中文字幕视频 | 一本久久a久久精品vr综合 | 另类videossexo老妇 | 四虎影视库永久在线地址 | 国产精品免费一区二区区 | 在线成人国产 | 91免费福利视频 | 91成人免费福利网站在线 | 拍拍拍精品视频在线观看 | 中文字幕中韩乱码亚洲大片 | 久夜tv| 亚洲国产97在线精品一区 | 天天干天天操天天做 | 午夜精品久久久久久久99热 | 四虎免费影视 | 久久这里只有精品6 | 亚洲欧洲日本在线 | 欧美一区二区视频 | 草草影院第一页yycccom | 波多野结衣久久一区二区 | 日韩欧美视频免费观看 | 香蕉网伊在线中文慕大全 | 亚洲这里只有精品 | 日本一线一区二区三区免费视频 | 日日摸夜夜添夜夜添毛片 | 亚洲大胆精品337p色 | 国产精品视频男人的天堂 | 国产精品久久久亚洲动漫 | 国产高清一级毛片在线不卡 | 日本不卡毛片一二三四 | 高清人人天天夜夜曰狠狠狠狠 |