1. 程式人生 > >springmvc rest風格的例項程式碼demo講解

springmvc rest風格的例項程式碼demo講解

Rest介紹

/blog /  HTTP GET =>  得到id = 1的blog
 / blog /  HTTP DELETE => 刪除 id = 1的blog
 / blog /  HTTP PUT =>  更新id = 1的blog
 / blog   HTTP POST =>   新增BLOG

    以下詳細解一下spring rest使用.

    首先,我們帶著如下兩個問題檢視本文。

        1.如何在java構造沒有副檔名的RESTful url,如 /forms/1,而不是 /forms/1.do

        2.瀏覽器的form標籤不支援提交delete,put請求,如何曲線解決

    springmvc rest 實現

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

@RequestMapping(value=" /blog/{id} " ,method=RequestMethod.DELETE)
 public  ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response)  }

    @RequestMapping @PathVariable如果URL中帶引數,則配合使用,如

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

    1.springmvc web.xml配置

<!-- 該servlet為tomcat,jetty等容器提供,將靜態資源對映從/改為/static/目錄,如原來訪問 http://localhost/foo.css ,現在http://localhost/static/foo.css  --> 
   <servlet-mapping> 
   <servlet-name> default </servlet-name> 
   <url-pattern> /static/* </url-pattern> 
   </servlet-mapping> 
   <servlet> 
     <servlet-name> springmvc </servlet-name> 
     <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> 
     <load-on-startup> </load-on-startup> 
   </servlet> 
 
   <!--  URL重寫filter,用於將訪問靜態資源http://localhost/foo.css 轉為http://localhost/static/foo.css  --> 
   <filter> 
   <filter-name> UrlRewriteFilter </filter-name> 
   <filter-class> org.tuckey.web.filters.urlrewrite.UrlRewriteFilter </filter-class> 
   <init-param> 
      <param-name> confReloadCheckInterval </param-name> 
      <param-value> </param-value> 
     </init-param> 
   <init-param> 
         <param-name> logLevel </param-name> 
         <param-value> DEBUG </param-value> 
       </init-param> 
   </filter> 
   <filter-mapping> 
   <filter-name> UrlRewriteFilter </filter-name> 
   <url-pattern> /* </url-pattern> 
   </filter-mapping> 
 
   <!--  覆蓋default servlet的/, springmvc servlet將處理原來處理靜態資源的對映  --> 
   <servlet-mapping> 
     <servlet-name> springmvc </servlet-name> 
     <url-pattern> / </url-pattern> 
   </servlet-mapping> 
 
   <!--  瀏覽器不支援put,delete等method,由該filter將/blog?_method=delete轉換為標準的http delete方法  --> 
   <filter> 
   <filter-name> HiddenHttpMethodFilter </filter-name> 
   <filter-class> org.springframework.web.filter.HiddenHttpMethodFilter </filter-class> 
   </filter> 
 
   <filter-mapping> 
   <filter-name> HiddenHttpMethodFilter </filter-name> 
   <servlet-name> springmvc </servlet-name> 
   </filter-mapping>

2.webapp/WEB-INF/springmvc-servlet.xml配置,使用如下兩個class啟用@RequestMapping annotation

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 
 

3.Controller編寫

/***/   */
 @Controller
 @RequestMapping( " /userinfo " )
 public   class  UserInfoController  extends  BaseSpringController }
 
/userinfo  => index()
   /userinfo/new => _new()
   /userinfo/{id} => show()
   /userinfo/{id}/edit  => edit()
   /userinfo POST => create()
   /userinfo/{id} PUT => update()
   /userinfo/{id} DELETE => delete()
   /userinfo DELETE => batchDelete()
 

 注(不使用 /userinfo/add  => add() 方法是由於add這個方法會被maxthon瀏覽器當做廣告連結過濾掉,因為包含ad字元)

4.jsp 編寫

<form:form action="${ctx}/userinfo${userInfo.userId}" method="put"> 
 </form:form>

生成的html內容如下, 生成一個hidden的_method=put,並於web.xml中的HiddenHttpMethodFilter配合使用,在服務端將post請求改為put請求

<form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">
   <input  type="hidden" name="_method" value="put" /> 
 </form>

另外一種方法是你可以使用ajax傳送put,delete請求.

5.靜態資源的URL重寫

        如上我們描述,現因為將default servlet對映至/static/的子目錄,現我們訪問靜態資源將會帶一個/static/字首.

        如 /foo.gif, 現在訪問該檔案將是 /static/foo.gif.

<urlrewrite>
    <!--  訪問jsp及jspx將不rewrite url,其它.js,.css,.gif等將重寫,如 /foo.gif => /static/foo.gif --> 
    <rule> 
     <condition  operator="notequal" next="and" type="request-uri">.*.jsp </condition> 
     <condition  operator="notequal" next="and" type="request-uri"> .*.jspx </condition> 
      <from> ^(/.*\..*)$ </from> 
      <to> /static$1 </to> 
    </rule> 
 </urlrewrite>