[轉]Spring MVC 中的基於註解的 Controller


@Controller    基於註解的 Controller    java

        終於來到了基於註解的 Spring MVC 了。以前咱們所講到的 handler,須要根據 url 並經過 HandlerMapping 來映射出相應的 handler 並調用相應的方法以響應請求。實際上,ControllerClassNameHandlerMapping, MultiActionController 和選擇恰當的 methodNameResolver(如 InternalPathMethodNameResolver) 就已經能夠在很大程度上幫助咱們省去很多的 XML 配置,誰讓 ControllerClassNameHandlerMapping 極度的擁抱了 Convention Over Configuration 呢。 

        那爲何還要用基於註解的 Controller 呢?Spring MVC 在 Spring 2.5 發佈中新添加了一種基於註解的 Controller 形式。藉助於與 Spring 2.5 一同發佈的容器內 <context:component-scan> 功能支持,基於註解的 Controller 幾乎能夠達到 XML 零配置,進而極大地提升咱們的開發效率。 

        和其它 Controller 同樣,基於註解的 Controller 一樣有相應的 HandlerMapping,那就是 DefaultAnnotationHandlerMapping。一樣,也有相應的 HandlerAdapter,那就是 AnnotationMethodHandlerAdapter。甚至,咱們均可以不把 Controller 註冊到容器裏,那麼確定須要一種機制來幫助咱們完成這點,這就是 <context:component-scan>。開發基於註解的 Controller,咱們須要作如下準備工做: 

● <context:compnent-scan> 
web

Xml代碼             spring

  1. <!-- 切 記,這不是必需的!除非你把註解的 Controller 一個個的註冊到容器中。相信你們仍是喜歡用 context:compnent- scan 吧。不要認爲在 Spring MVC 中才提到 context:component-scan,就認爲它只能掃 描 @Controller。component-scan 默認掃描的註解類型是 @Component,不過,在 @Component 語義基礎上 細化後的 @Repository, @Service 和 @Controller 也一樣能夠得到 component-scan 的青 睞 -->  json

  2. <context:component-scan base-package="org.zachary.spring3.anno.web" />  服務器



● HandlerMapping  
session

Xml代碼             mvc

  1. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  app

  2.   <description>  框架

  3.   這點是必需的仍是非必需的呢?  jsp

  4.   若是定義 了 DefaultAnnotationHandlerMapping,它就能夠將請求來的 url 和被註解了 @RequesMapping 的指進 行匹配。固然,說這句話的前提是定義 DefaultAnnotationHandlerMapping 的優先級比定義了其它 的 HandlerMapping 的優先級要高(若是定義了其它的話)。  

  5.   若是沒有定 義 DefaultAnnotationHandlerMapping,並不表明不能映射到相應的 handler 上。由於若是你定義了其它 的 HandlerMapping,請求過來的 url 和註解了的 @RequestMapping 裏的值正好能匹配上,那麼沒 有 DefaultAnnotationHandlerMapping,@Controller 同樣能夠如魚得水的被捕獲到。  

  6.   固然,若是你要使用基於註解的 @Controller,最好仍是老老實實地註冊 DefaultAnnotationHandlerMapping。  

  7.   </description>  

  8. </bean>  


● HandlerAdaptor  

Xml代碼             

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

  2.   <description>  

  3.   和上面的 HandlerMapping 同樣,是必需的仍是非必需的呢?  

  4.   Spring MVC 中,若是咱們沒有註冊任何 HandlerAdaptor 到容器中,注意,我說的是任何。那麼 DispatcherServlet 將啓用後備的幾個默認使用的 HandlerAdaptor 實現,包括:  

  5.   org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter  

  6.   org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter  

  7.   org.springframework.web.servlet.mvc.AnnotationMethodHandlerAdaptor  

  8.   

  9.    看見沒,若是咱們沒有註冊任何 的 HandlerAdaptor,框架會準備 AnnotationMethodHandlerAdaptor 的。但是因爲某些緣由,咱們須要爲某 些 HandlerAdaptoer 進行一些定製化,即在容器中註冊了某個 HandlerAdaptor,那麼很抱歉,框架只會啓用你註冊的那個,而 框架自己準備的不會被啓用。因此,你一旦爲某個 HandlerMapping 進行了定製化,請別忘了把其它的 HandlerAdaptor 也註冊 進來,即使這些不須要定製化。不然的話,後果你是能夠想象的。固然,除非你確保你真的只須要那一個你註冊進容器的 HandlerAdaptor,不然, 我再囉嗦一遍,別忘了把其它的 HandlerAdaptor 也註冊進來。  

  10.   </description>  

  11. </bean>  




        好了,有了以上幾點準備工做,咱們就能夠開始基於註解的 Controller 之旅了。下面咱們來一個一個註解的來說解。  

● @Controller  

Java代碼             

  1. /** 

  2.  * @Controller,類級別上的註解。咱們定義的類能夠只是一個 javabean,不須要實現任何接口。標註了 

  3.  * @Controller,藉助 <context:component-scan>,框架能自動識別到這就是一個 Controller 

  4.  */  

  5. @Controller  

  6. public class MyController {  

  7.   // ......  

  8. }  



● @RequestMapping  

Java代碼             

  1. /** 

  2.  * @RequestMapping 能夠出如今類級別上,也能夠出如今方法上。若是出如今類級別上,那請求的 url 爲 類級別 

  3.  * 上的 @RequestMapping + 方法級別上的 @RequestMapping,不然直接取方法級上的 @RequestMapping。 

  4.  * 類級別的 @RequestMapping 不是必需的。 

  5.  */  

  6. @Controller  

  7. @RequestMapping("/my")  

  8. public class MyController {  

  9.     

  10.   /** 

  11.    * 因爲類級別上定義了 @RequestMapping,那麼想匹配到這個方法來處理請求,url 必須爲 /my/somelist。 

  12.    * 若是沒有定義類級別上的 @RequestMapping,url 爲 /somelist 便可。同時,請求方法必須爲 POST 

  13.    */  

  14.   @RequestMapping(value="/somelist", method=RequestMethod.POST);  

  15.   public String getSomeList() {...}  

  16.   

  17.     /** 

  18.      * 在方法級別使用 @RequestMapping 來限定請求處理的時候,能夠指定兩個屬性。除了咱們在上面剛使用過的 

  19.      * method 屬性,還有一個 params 屬性。使用 params 屬性,能夠達到與使用 

  20.      * ParameterMethodNameResolver 做爲 MethodResolver的 MultiActionController 相似的功能。 

  21.      * 

  22.      * params 有兩種表達形式,這裏先說第一種:"parameterName=parameterValue" 

  23.      * 

  24.      * 請求方法爲 GET 或 POST,且具備 hello 參數,且值爲 world 的請求才能匹配到該方法,如: 

  25.      *   /my?hello=world 

  26.      */  

  27.     @RequestMapping(params="hello=world", method={RequestMethod.GET, RequestMethod.POST})  

  28.     public String helloworld() {...}  

  29.   

  30.     /** 

  31.      * 請求方法爲 GET 或 POST,且具備 hello 參數,且值爲 java 的請求才能匹配到該方法,如: 

  32.      *   /my?hello=java 

  33.      */  

  34.     @RequestMapping(params="hello=java", method={RequestMethod.GET, RequestMethod.POST})  

  35.     public String hellojava() {...}  

  36.   

  37.     /** 

  38.      * params 屬性的另一種表達形式爲:"parameter" 

  39.      * 

  40.      * 請求方法爲 GET,且具備請求參數 java 即匹配此方法,而無論 java 參數的值是什麼,如: 

  41.      *   /my?java=anything 

  42.      */  

  43.     @RequestMapping(params="java", method={RequestMethod.GET})  

  44.     public String java() {...}  

  45.   

  46.     /** 

  47.      * 請求方法爲 GET,且具備請求參數 cplusplus 即匹配此方法,而無論 cplusplus 參數的值是什麼,如: 

  48.      *   /my?cplusplus=anything 

  49.      */  

  50.     @RequestMapping(params="cplusplus", method={RequestMethod.GET})  

  51.     public String cplusplus() {...}  

  52.   

  53.     /** 

  54.      * @RequestMapping 還有一個參數化 headers,它和 params 很是類似,也有兩種表達式,只不過它是對 

  55.      * 請求頭作限制罷了。你們能夠經過 telnet 或 http-client 來發相似的請求以檢驗。以 telnet 爲例: 

  56.      *  

  57.      * telnet localhost 8080 

  58.      * POST /contextPath/my HTTP/1.1 

  59.      * Host: localhost 

  60.      * hello: world # 這個就是自定義請求頭,和標準的請求頭的寫法別無二致 

  61.      * 【回車】 

  62.      * 【回車】 

  63.      */  

  64.     @RequestMapping(headers="hello=world", method={RequestMethod.POST})  

  65.     public String cplusplus() {...}  

  66. }  



● @RequestParam(將請求參數綁定到方法參數)  

Java代碼             

  1. @Controller  

  2. @RequestMapping("/my")  

  3. public class MyController {  

  4.   

  5.   /** 

  6.    * 注意,這裏的方法有一個參數。若請求 url 爲 /my/test,會匹配此方法。這裏的方法的參數名爲 userId, 

  7.    * 那麼請求參數中必定有名爲 userId 的參數,且值爲整數。這也是默認的綁定行爲,它是根據名稱匹配原則進行 

  8.    * 的數據綁定。當請求中的參數名與方法名一致的時候,相應的參數值將被綁定到相應的方法參數上。 

  9.    *  

  10.    * 若是沒有傳遞 userId 參數,框架會傳入 null。但是這裏咱們定義的是 primitive type,異常伺候!若 

  11.    * 要解決此問題,須要將 primitive type 定義成相應的 wrapper type 便可,這裏使用 Integer 就好了。 

  12.    * 

  13.    * 若是傳遞了 userId 參數,但值不是整數,你叫 test 怎麼辦呢?這種狀況下,框架藉助 PropertyEditor  

  14.    * 數據類型轉換失敗,ExceptionResolver 會接手處理,請求是不會進入 test 方法的。 

  15.    * 

  16.    * 這種方式下,默認的綁定行爲須要咱們嚴格遵照命名一致性原則。若是咱們對此不滿,想自定義綁定關係,能夠求 

  17.    * 助於 @RequestParam。 

  18.    */  

  19.   @RequestMapping("/test")  

  20.   public String test(int userId) { ... }  

  21.   

  22.   /** 

  23.    * 當咱們不想使用 userId 做爲方法的參數名,即不想使用默認的數據綁定方式。若是咱們要使用 id 做爲方法 

  24.    * 的參數,爲了保證名稱爲 userId 的請求參數能夠綁定到新的名稱爲 id 的方法參數上,咱們就可使用  

  25.    * @RequestParam 對這一參數進行標註。@RequestParam 只能夠標註於方法參數上。 

  26.    * 

  27.    * 若是請求參數中有 age,和方法的參數名稱一致,故 age 參數不須要 @RequestParam 標註。若是沒有傳遞 

  28.    * age,咱們又不想定義成 Integer,很顯然框架會注入 null 值,報錯是必然的。這是因爲 @RequestParam  

  29.    * 的 required 屬性決定的,默認就是 true。若是咱們定義成 false, 

  30.    * 即 @RequestParam(required=false) int age 

  31.    * 這個時候定義成 int 型的 age,即使請求參數沒有 age 參數,也是沒問題的。 

  32.    * 

  33.    * 同時,這裏還能綁定 Date 類型,User 對象類型等等。如 date=2011-01-01&userName=Tom&userAge=18 

  34.    * 這裏,User 類的屬性須要爲 userName 和 userAge,以避免和 age,name 混淆。因此,Spring MVC 對對象 

  35.    * 的數據綁定就沒有 Struts2 作的那麼好了,Strtus2 能夠這樣:user.age=18&user.name=Tom 

  36.    */  

  37.   @RequestMapping("/test2")  

  38.   public String test2(@RequestParam("userId"int id, int age, Date date, User user) { ... }  

  39. }  



● @PathVariable(將 url template 裏的參數綁定到方法參數)  


Java代碼        

  1. @Controller  

  2. @RequestMapping("/my")  

  3. public class MyController {  

  4.   

  5.   /** 

  6.    * @PathVariable 是 url 模板,須要和 @RequestMapping 配合起來使用,這是 Spring 3.0 以後引入的。 

  7.    * 

  8.    * 在這個例子中,請求的 url 必須知足相似 /my/user/zhangsan/18 這樣的格式才能匹配方法。url 模板裏 

  9.    * 的參數名和方法參數名的綁定規則和 @RequestParam 相似,這裏就再也不贅述了。 

  10.    * 

  11.    * @PathVariable 和 @RequestParam 的區別在於: 

  12.    *   @PathVariable 的 url:/my//user/zhangsan/18 

  13.    *   @RequestParam 的 url:/my//user?nickname=zhangsan&age=18 

  14.    */  

  15.   @RequestMapping("/user/{nickname}/{age}");  

  16.   public String getUserInfo(@PathVariable("nickname") String name, @PathVariable int age) {...}  

  17. }  



● @RequestBody(將請求正文綁定到方法參數)  

Java代碼             

  1. /** 

  2.  * 來看一個 http 請求: 

  3.  * (請求行) POST /my HTTP/1.1 

  4.  * (請求頭) Host: localhost 

  5.  * (請求頭) Content-Type: text/plain 

  6.  * (請求頭) Content-Length: 5 

  7.  * 

  8.  * (請求體) hello 

  9.  * 

  10.  * 這裏的 hello,就是請求體,也稱 request message。如有請求體,則必須提供請求體的類型和長度,這些信 

  11.  * 息是寫在請求頭裏的,即 Content-Type 和 Content-Length 

  12.  */  

  13. @Controller  

  14. @RequestMapping("/my")  

  15. public class MyController {  

  16.   

  17.   /** 

  18.    * 咱們定義的 body 的數據類型是 String,請求體嘛,確定是 String。實際上,@RequestBody 是用於將請 

  19.    * 求體的內容綁定到方法參數上,數據類型不必定是 String。Spring MVC 是經過 HttpMessageConverter 

  20.    * 來完成這種轉換的。AnnotationMethodHandlerAdapter 默認註冊了一些 HttpMessageConverters: 

  21.    *   ByteArrayHttpMessageConverter - converts byte arrays 

  22.    *   StringHttpMessageConverter - converts strings 

  23.    *   FormHttpMessageConverter - converts form data to/from MultiValueMap<String,String> 

  24.    *   SourceHttpMessageConverter - convert to/from a javax.xml.transform.Source 

  25.    *   MappingJacksonHttpMessageConverter - converts json 

  26.    *   MarshallingHttpMessageConverter - convert to/from an object using the  

  27.    *                                     org.springframework.oxm package. 

  28.    * 

  29.    * 正如上所述,HttpMessageConverter 用於從請求正文綁定到對象和把對象序列化成 String 予客戶端響應。 

  30.    * 即 HttpMessageConverter is responsible for converting from the HTTP request message to 

  31.    * an object and converting from an object to the HTTP response body 

  32.    * 

  33.    * 咱們能夠在 AnnotationMethodHandlerAdapter 定義任意多的 HttpMessageConverters。 

  34.    * 

  35.    * 既然 HttpMessageConverter 能夠用於雙向 convert,這裏討論的是 @RequestBody,那這部分咱們只講  

  36.    * converting from the HTTP request message to an object。 

  37.    * 

  38.    * 假設咱們只向 AnnotationMethodHandlerAdapter 注入了 MappingJacksonHttpMessageConverter 和 

  39.    * MarshallingHttpMessageConverter。處理請求的方法有以下簽名: 

  40.    *     public String test(@RequestBody User user) { ... } 

  41.    * 

  42.    * 無論請求正文的內容是什麼,對於客戶端和服務器而言,它們只是用文原本互相通訊。把字符串轉爲 User 對 

  43.    * 象,該用哪一個 HttpMessageConverter 來完成此項工做呢? 

  44.    * 

  45.    * 在定義 HttpMessageConverters 時,咱們能夠爲其指定 supportedMediaTypes。對於將請求正文轉爲對象 

  46.    * 這個方向的操做,HttpMessageConverters 會從請求頭獲得 Content-Type 頭信息,看其是否隸屬於其定義 

  47.    * 的 supportedMediaTypes。若沒有匹配上,則會使用下一個 HttpMessageConverter 作一樣的判斷。只要 

  48.    * 某個 HttpMessageConverter 支持請求頭中的 Content-Type,那麼就會應用此 HttpMessageConverter 

  49.    * 來將 String 轉爲 Object。固然,若請求正文並無按照 Content-Type 所規定的格式來編寫,必然要收到 

  50.    * 500 的響應。同時請注意,請求頭中還必須提供 Content-Length,不然拿不到請求正文。 

  51.    * 

  52.    * 若是全部的 HttpMessageConverters 中定義的 supportedMediaTypes 均不能匹配上 Content-Type 請 

  53.    * 求頭中的類型,那麼就會收到 415 Unsupported Media Type 響應。 

  54.    */  

  55.   @RequestMapping("/user/body");  

  56.   public String getBody(@RequestBody String body) {  

  57.   

  58.     // 這裏的 body 的內容就是 hello  

  59.     System.out.println(body);  

  60.     return null;  

  61.   }  

  62. }  

 


● @ResponseBody(將處理完請求後返回的對象綁定到響應正文)  

Java代碼             

  1. /** 

  2.  * 上面的 @RequestBody 講了 HttpMessageConverter 從請求正文到對象轉換的方向,如今來說講另一個方 

  3.  * 向,@ResponseBody,此時,HttpMessageConverter 用於將處理完請求後返回的對象序列化成字符串,即 

  4.  * converting from an object to the HTTP response body. 

  5.  */  

  6. @Controller  

  7. @RequestMapping("/my")  

  8. public class MyController {  

  9.   

  10.   /** 

  11.    * 該方法的返回類型是 User,並不符合含有 @RequestMapping 的註解所需的簽名方式。但它仍然是合法的,因 

  12.    * 爲在返回類型前有 @ResponseBody 註解,此註解將告知框架,將 User 對象做爲影響正文返回?什麼?對象 

  13.    * 做爲響應正文!因此,HttpMessageConverter 在這裏就起到做用了。這裏討論的是 @ResponseBody,因此 

  14.    * 這裏咱們只講 converting from an object to the HTTP response body。 

  15.    * 

  16.    * User 對象要轉成什麼樣的 String,或者說要轉成什麼格式的 String?這個時候須要從請求頭中得到此信息 

  17.    * 了,這裏,就是請求頭的 Accept 頭。Accept 頭可使用逗號分隔定義多個類型,用以告知服務器我只接受 

  18.    * 哪些類型的響應。AnnotationMethodHandlerAdapter 中一樣注入了多個 HttpMessageConverter,每一個  

  19.    * HttpMessageConverter 均可以定義各自的 supportedMediaTypes。這個時候該用哪一個  

  20.    * HttpMessageConverter 來完成對象到文本的序列化操做呢? 

  21.    * 

  22.    * 遍歷 Accept 頭中的每種媒體類型,在定義的多個 HttpMessageConverters 中依次去匹配,若匹配上,就使 

  23.    * 用該 HttpMessageConverter 來完成序列化操做,而且響應頭的 Content-Type 並非請求頭 Accept 頭 

  24.    * 的諸多類型中第一個被匹配的類型,而是匹配到的 HttpMessageConverter 定義的 supportedMediaTypes 

  25.    * 中的第一個類型。 

  26.    * 

  27.    * 若是全部的 HttpMessageConverters 中定義的 supportedMediaTypes 均不能匹配上 Accept 請求頭中 

  28.    * 的諸多的類型,那麼就會收到 406 Not Acceptable 響應。 

  29.    */  

  30.   @RequestMapping("/user")  

  31.   public @ResponseBody User getUser() {  

  32.     return new User(18"Jack""計算機");  

  33.   }  

  34. }  



● @ModelAttribute  

Java代碼             

  1. /** 

  2.  * @ModelAttribute 能夠爲視圖渲染提供更多的模型數據,而不須要在處理請求的方法裏添加 ModelMap 或 

  3.  * Model 類型的參數。 

  4.  * 

  5.  * @ModelAttribute 能夠標註在方法(存數據)上,也能夠標註在方法參數(取數據)上。 

  6.  */  

  7. @Controller  

  8. @RequestMapping("/my")  

  9. public class MyController {  

  10.   

  11.   /** 

  12.    * 在處理該請求時,方法的返回類型是 User,貌似不符合返回類型的規範。因爲這裏使用了 @ModelAttribute 

  13.    * 註解,表示將返回的對象以 "user" 爲 key 放入模型數據裏。這裏的 key 值默認值是返回的數據類型首字母 

  14.    * 小寫的結果。若是想自定義 key,能夠寫成 @ModelAttribute("myAttribute"),那麼模型數據將會將  

  15.    * User 對象綁定到 key 爲 "myAttribute" 上。 

  16.    *  

  17.    * jsp 裏能夠這樣訪問模型裏的數據: 

  18.    *   age: ${user.age} 

  19.    *   name: ${user.name} 

  20.    *   job: ${user.job} 

  21.    * 

  22.    * 固然,這裏只是提到了 @ModelAttribute 存數據的操做。 

  23.    */  

  24.   @RequestMapping("/user")  

  25.   @ModelAttribute  

  26.   public User getUser() {  

  27.     return new User(18"Jack""計算機");  

  28.   }  

  29.     

  30.   /** 

  31.    * 這裏將 @ModelAttribute 標註在方法參數上,表示要從模型數據裏取 key 爲 "user" 的對象,綁定在方法 

  32.    * 參數上。若是這樣作的話,其實你是得不到上面的那個請求放入的 User 對象,獲得的是另一個對象。其實 

  33.    * 也好理解,這是兩個互相獨立的請求,做用域不同。要想達到咱們的目的,即可以從模型數據裏取數據,須要 

  34.    * 求助於 @SessionAttributes 

  35.    */  

  36.   @RequestMapping("/user2")  

  37.   public String showUser(@ModelAttribute User user) {  

  38.     System.out.println(user);  

  39.     return null;  

  40.   }  

  41. }  



● @SessionAttributes  

Java代碼             

  1. /** 

  2.  * @SessionAttributes 和 @ModelAttribute 相似,只不過 @SessionAttributes 是將數據存放於 session  

  3.  * 中或從 session 中取數據。 

  4.  * 

  5.  * @SessionAttributes 只能應用在類型聲明上。好比下面的類的聲明中,只有屬性名爲 "the-attribute" 的數 

  6.  * 據纔會歸入到 session 的管理。 

  7.  * 

  8.  * @SessionAttributes 容許以屬性名名稱或者類型兩種方法,來代表將哪些數據經過 session 進行管理。這裏 

  9.  * 咱們使用的是指定屬性名稱的方式,但經過類型來指定也是可行的,如: 

  10.  *   @SessionAttributes(types=User.class) 

  11.  */  

  12. @Controller  

  13. @RequestMapping("/my")  

  14. @SessionAttributes("the-attribute")  

  15. public class MyController {  

  16.   

  17.   @RequestMapping("/getUser")  

  18.   public String getUser(int userId, Model model) {  

  19.     /** 

  20.      * 注意,這裏將 User 對象添加到屬性名爲 "the-attribute" 上,因此 User 對象將歸入到 session 的 

  21.      * 管理。若是這裏添加的對象的屬性名不是 "the-attribute",那麼它只會做用於當前請求,而不會歸入到  

  22.      * session 的管理中。 

  23.      */  

  24.     User user = userService.getUserById(userId);  

  25.     model.addAtrribute("the-attribute", user);  

  26.     return "userinfo";  

  27.   }  

  28.     

  29.   /** 

  30.    * 將模型裏的 "the-attribute" 爲 key 的對象綁定到 User 類上。因爲在類級別上聲明瞭只有 "the- 

  31.    * attribute" 的屬性名纔會歸入到 session 的管理,因此就解決了在 @ModelAttribute 註解中講解中最 

  32.    * 後提到的問題。 

  33.    * 

  34.    * 另外,這個方法還有兩個參數,BindingResult 和 SessionStatus。因爲這裏有綁定數據的動做,咱們能夠 

  35.    * 根據 BindingResult 對象得到數據綁定結果以決定後繼流程該如何處理。SessionStatus 在這裏用於處理 

  36.    * 完請求後,清空 session 裏的數據。 

  37.    */  

  38.   @RequestMapping("/updateUser")  

  39.   public String updateUser(@ModelAttribute("the-attribute") User user,   

  40.             BindingResult result, SessionStatus status) {  

  41.   

  42.     if (result.hasErrors) {  

  43.       return "error";  

  44.     }  

  45.       

  46.     userService.updateUser(user);  

  47.     // 咱們經過調用 status.setComplete() 方法,該 Controller 全部放在 session 級別的模型屬性數據  

  48.     // 將從 session 中清空  

  49.     status.setComplete();  

  50.     return "redirect:getUser?userId=" + user.getId();  

  51.   }  

  52. }  




        Spring MVC 裏的大部分的註解,這裏基本上都講到了。往後隨着 Spring 的升級,我也會逐一補充新加的註解。其實,僅憑以上的註解,是能夠構建一個足夠強大的 RESTFul Webservices 的了。  

        這裏,補充講下被標註了 @RequestMapping 註解的請求方法的簽名。使用 @RequestMapping 標註的 web 請求處理方法的簽名比較靈活,咱們幾乎能夠聲明並使用任何類型的方法參數。不過,如下幾種類型的方法參數將擁有更多語義,它們均來自框架內部(或者說 AnnotationMethodHandlerAdapter)所管理的對象引用:  

  • request/response/session

  • org.springframework.web.context.request.WebRequest。當前處理方法中得到可用的 WebRequest 實例。

  • java.util.Locale。經過相應的 LocalResolver 所返回的對應當前 web 請求的 Locale。

  • java.io.InputStream/java.io.Reader。至關於 request.getInputStream() 或 request.getReader() 所得到的對象引用。

  • java.io.OutputStream/java.io.Writer。至關於 response.getOutputStream() 或 response.getWriter() 所得到的對象引用。

  • java.util.Map/org.springframework.ui.ModelMap。你如今可用對模型數據隨心所欲了。

  • org.springframework.validation.Errors/org.springframework.validation.BindingResult。用於對 Command 對象進行數據驗證的 Errors 或者 BindingResult 對象。聲明這兩種類型的方法參數有一個限制,它們的聲明必須緊跟着 Command 對象的定義。其它類型的方法參數是沒有任何順序限制的。

  • org.springframework.web.bind.supportt.SessionStatus。SessionStatus 主要用於管理請求處理以後 Session 的狀態,好比清除 Session 中的指定的數據。


        基於註解的 Controller 的請求處理方法返回類型能夠有以下 4 種形式(固然,前面提到的 @ResponseBody 和 @ModelAttribute 並沒下面所描述的返回類型,具體參見上面對各自注解的講解):  

  • org.springframework.web.servlet.ModelAndView。這個不用多說,視圖信息和模型信息都能經過它返回。

  • java.lang.String。該類型返回值表明邏輯視圖名,模型數據須要以其它形式提供,好比爲處理方法聲明一個 ModelMap 類型的參數。注 意,若是返回 null,並不表明向客戶端輸出空頁面(定向思惟惹的禍),這種狀況下,框架會從請求路徑中提取視圖信息。若是返回 null 就是要表示方法內部已處理完請求,也不須要通知頁面,就是想僅僅返回空白頁面,唉,我尚未想出來咋整。。。反正 writer.write("") 這樣寫能夠,還得聲明一個 Writer 類型的方法參數。

  • org.springframework.ui.ModelMap。ModelMap 類型返回值只包含了模型數據信息而沒有視圖信息,框架將根據請求的路徑來提取視圖信息。

  • void。沒有任何返回值,視圖信息將從請求路徑中提取,模型數據須要經過其它形式提供。


        String 類型的返回值爲 null, 還有返回類型爲 ModelMap 和 void,從請求路徑中如何提取視圖信息呢?框架將截取請求路徑中的最後一個 / 後面的內容,並去掉後綴名,剩下來的內容就是視圖名。如請求路徑爲 /spring3/user/welcome,那麼視圖名是 welcome,/spring3/user/welcome.action 的視圖名也是 welcome。  

        接下來來說最後一個部分,請求參數到方法參數的綁定。這個在 @RequestParam 中已經講過,不過,這裏要講的是綁定複雜的對象。在 @RequestParam 中,咱們這樣請求,date=2011-01-01 實際上是綁定不到 Date 對象的。由於不一樣的 Locale 處理日期的字符串的表達方式不同。總之,這部分涉及到字符串到對象的轉換,這很像 PropertyEditor,對吧?Spring MVC 中,能夠爲某個 Controller 定製數據綁定,即在被標註了 @InitBinder 的方法裏寫綁定邏輯,方法名能夠隨意,如:  

Java代碼             

  1. /** 

  2.  * 初始化方法不能有返回值,並且至少應該有一個類型爲 org.springframework.web.bind.WebDataBinder 的 

  3.  * 方法參數。同時,一個典型的基於註解的 Controller 的處理方法可使用的方法參數中,除了 Command 對象 

  4.  * 以及相關的 Errors/BindingResult 對象做爲方法的參數外,均可以做爲初始化方法的參數。 

  5.  * 

  6.  * 這裏,咱們沒有必要爲日期再定製自定義綁定規則,Spring 已經爲咱們提供了 CustomDateEditor,這裏只是演 

  7.  * 示如何提供自定義數據綁定規則。 

  8.  * 

  9.  * 這裏的 WebDataBinder,是否是很像 PropertyEditorRegistry? 

  10.  */  

  11. @InitBinder  

  12. public void initBinder(WebDataBinder binder) {  

  13.   binder.registerCustomEditor(Date.classnew PropertyEditorSupport() {  

  14.     

  15.     final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");  

  16.       

  17.     @Override  

  18.     public void setAsText(String text) throws IllegalArgumentException {  

  19.       try {  

  20.         Date date = sf.parse(text);  

  21.         setValue(date);  

  22.       } catch (ParseException e) {  

  23.         Date data = sf.parse(text);  

  24.         throw new IllegalArgumentException(e);  

  25.       }  

  26.     }  

  27.   })  

  28. }  



        在 Controller 裏使用 @InitBinder 標註的初始化方法只能對一個 Controller 對應的 WebBinder 作定製。若是想在整個應用中共享綁定規則,能夠爲 AnnotationMethodHandlerAdapter 指定一個自定義的 org.springframework.web.bind.support.WebBindingInitializer 實例,這樣能夠避免在每一個 Controller 中都重複定義幾乎相同邏輯的 @InitBinder 的初始化方法。  

Java代碼             

  1. public class MyBindingInitializer implements WebBindingInitializer {  

  2.   

  3.   public void initBinder(WebBinder binder, WebRequest request) {  

  4.     binder.registerCustomEditor(SomeDataType.class, somePropertyEditor)  

  5.     // 若是須要,這裏能夠繼續註冊更多的 propertyEditor  

  6.     // ......  

  7.   }  

  8. }  



Xml代碼             

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

  2.   <property name="webBindingInitializer">  

  3.     <bean class="...MyBindingInitializer" />  

  4.   </property>  

  5. </bean>  



        結束該篇文章前,咱們來看幾個容易混淆的用於簡化開發的配置: <mvc:annotation-driven />, <context:annotation-config/>, <context:component-scan />。  

        <mvc:annotation-driven /> 會作如下幾件事:  

  1. 向 spring 容器中註冊 DefaultAnnotationHandlerMapping。

  2. 向 spring 容器中註冊 AnnotationMethodHandlerAdapter。

  3. 配置一些 messageconverter。

  4. 解決了 @Controller 註解的使用前提配置,即 HandlerMapping 可以知道誰來處理請求。

        <context:annotation-config /> 會作如下幾件事:  

  1. 向 spring 容器中註冊 AutowiredAnnotationBeanPostProcessor。

  2. 向 spring 容器中註冊 CommonAnnotationBeanPostProcessor。

  3. 向 spring 容器中註冊 PersistenceAnnotationBeanPostProcessor。

  4. 向 spring 容器中註冊 RequiredAnnotationBeanPostProcessor。

  5. 使用 <context:annotationconfig />以前,必須在 <beans> 元素中聲明 context 命名空間 <context:component-scan />。<context:component-scan /> 對包進行掃描,實現註解驅動 Bean 定義。即,將 @Controller 標識的類的 bean 註冊到容器中。

        <context:component-scan/>, 不但啓用了對類包進行掃描以實施註解驅動 Bean 定義的功能,同時還啓用了註解驅動自動注入的功能(即還隱式地在內部註冊了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor)。所以當使用 <context:component-scan /> 後,除非須要使用PersistenceAnnotationBeanPostProcessor 和 RequiredAnnotationBeanPostProcessor 兩個 Processor 的功能(例如 JPA 等),不然就能夠將 <context:annotation-config /> 移除了。

相關文章
相關標籤/搜索