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

Spring MVC 中的基于注解的 Controller

系統 2145 0
??????? 終于來到了基于注解的 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>
Xml代碼 復制代碼 ? 收藏代碼
  1. <!--?切記,這不是必需的!除非你把注解的?Controller?一個個的注冊到容器中。相信大家還是喜歡用?context:compnent-scan?吧。不要認為在?Spring?MVC?中才提到?context:component-scan,就認為它只能掃描?@Controller。component-scan?默認掃描的注解類型是?@Component,不過,在?@Component?語義基礎上細化后的?@Repository,?@Service?和?@Controller?也同樣可以獲得?component-scan?的青睞?--> ??
  2. < context:component-scan ? base-package = "org.zachary.spring3.anno.web" ? /> ??
        <!-- 切記,這不是必需的!除非你把注解的 Controller 一個個的注冊到容器中。相信大家還是喜歡用 context:compnent-scan 吧。不要認為在 Spring MVC 中才提到 context:component-scan,就認為它只能掃描 @Controller。component-scan 默認掃描的注解類型是 @Component,不過,在 @Component 語義基礎上細化后的 @Repository, @Service 和 @Controller 也同樣可以獲得 component-scan 的青睞 -->
<context:component-scan base-package="org.zachary.spring3.anno.web" />

      

● HandlerMapping
Xml代碼 復制代碼 ? 收藏代碼
  1. < bean ? class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" > ??
  2. ?? < description > ??
  3. ??這點是必需的還是非必需的呢? ??
  4. ??如果定義了?DefaultAnnotationHandlerMapping,它就可以將請求來的?url?和被注解了?@RequesMapping?的指進行匹配。當然,說這句話的前提是定義?DefaultAnnotationHandlerMapping?的優先級比定義了其它的?HandlerMapping?的優先級要高(如果定義了其它的話)。 ??
  5. ??如果沒有定義?DefaultAnnotationHandlerMapping,并不代表不能映射到相應的?handler?上。因為如果你定義了其它的?HandlerMapping,請求過來的?url?和注解了的?@RequestMapping?里的值正好能匹配上,那么沒有?DefaultAnnotationHandlerMapping,@Controller?一樣可以如魚得水的被捕獲到。 ??
  6. ??當然,如果你要使用基于注解的?@Controller,最好還是老老實實地注冊?DefaultAnnotationHandlerMapping。 ??
  7. ?? </ description > ??
  8. </ bean > ??
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <description>
  這點是必需的還是非必需的呢?
  如果定義了 DefaultAnnotationHandlerMapping,它就可以將請求來的 url 和被注解了 @RequesMapping 的指進行匹配。當然,說這句話的前提是定義 DefaultAnnotationHandlerMapping 的優先級比定義了其它的 HandlerMapping 的優先級要高(如果定義了其它的話)。
  如果沒有定義 DefaultAnnotationHandlerMapping,并不代表不能映射到相應的 handler 上。因為如果你定義了其它的 HandlerMapping,請求過來的 url 和注解了的 @RequestMapping 里的值正好能匹配上,那么沒有 DefaultAnnotationHandlerMapping,@Controller 一樣可以如魚得水的被捕獲到。
  當然,如果你要使用基于注解的 @Controller,最好還是老老實實地注冊 DefaultAnnotationHandlerMapping。
  </description>
</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 > ??
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <description>
  和上面的 HandlerMapping 一樣,是必需的還是非必需的呢?
  Spring MVC 中,如果我們沒有注冊任何 HandlerAdaptor 到容器中,注意,我說的是任何。那么 DispatcherServlet 將啟用后備的幾個默認使用的 HandlerAdaptor 實現,包括:
  org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
  org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
  org.springframework.web.servlet.mvc.AnnotationMethodHandlerAdaptor

   看見沒,如果我們沒有注冊任何的 HandlerAdaptor,框架會準備 AnnotationMethodHandlerAdaptor 的。可是由于某些原因,我們需要為某些 HandlerAdaptoer 進行一些定制化,即在容器中注冊了某個 HandlerAdaptor,那么很抱歉,框架只會啟用你注冊的那個,而框架本身準備的不會被啟用。所以,你一旦為某個 HandlerMapping 進行了定制化,請別忘了把其它的 HandlerAdaptor 也注冊進來,即便這些不需要定制化。否則的話,后果你是可以想象的。當然,除非你確保你真的只需要那一個你注冊進容器的 HandlerAdaptor,否則,我再啰嗦一遍,別忘了把其它的 HandlerAdaptor 也注冊進來。
  </description>
</bean>

      


??????? 好了,有了以上幾點準備工作,我們就可以開始基于注解的 Controller 之旅了。下面我們來一個一個注解的來講解。

● @Controller
Java代碼 復制代碼 ? 收藏代碼
  1. /** ?
  2. ?*?@Controller,類級別上的注解。我們定義的類可以只是一個?javabean,不需要實現任何接口。標注了 ?
  3. ?*?@Controller,借助?<context:component-scan>,框架能自動識別到這就是一個?Controller ?
  4. ?*/ ??
  5. @Controller ??
  6. public ? class ?MyController?{ ??
  7. ?? //?...... ??
  8. }??
        /**
 * @Controller,類級別上的注解。我們定義的類可以只是一個 javabean,不需要實現任何接口。標注了
 * @Controller,借助 <context:component-scan>,框架能自動識別到這就是一個 Controller
 */
@Controller
public class MyController {
  // ......
}

      

● @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. }??
        /**
 * @RequestMapping 可以出現在類級別上,也可以出現在方法上。如果出現在類級別上,那請求的 url 為 類級別
 * 上的 @RequestMapping + 方法級別上的 @RequestMapping,否則直接取方法級上的 @RequestMapping。
 * 類級別的 @RequestMapping 不是必需的。
 */
@Controller
@RequestMapping("/my")
public class MyController {
  
  /**
   * 由于類級別上定義了 @RequestMapping,那么想匹配到這個方法來處理請求,url 必須為 /my/somelist。
   * 如果沒有定義類級別上的 @RequestMapping,url 為 /somelist 即可。同時,請求方法必須為 POST
   */
  @RequestMapping(value="/somelist", method=RequestMethod.POST);
  public String getSomeList() {...}

    /**
     * 在方法級別使用 @RequestMapping 來限定請求處理的時候,可以指定兩個屬性。除了我們在上面剛使用過的
     * method 屬性,還有一個 params 屬性。使用 params 屬性,可以達到與使用
     * ParameterMethodNameResolver 作為 MethodResolver的 MultiActionController 類似的功能。
     *
     * params 有兩種表達形式,這里先說第一種:"parameterName=parameterValue"
     *
     * 請求方法為 GET 或 POST,且具有 hello 參數,且值為 world 的請求才能匹配到該方法,如:
     *   /my?hello=world
     */
    @RequestMapping(params="hello=world", method={RequestMethod.GET, RequestMethod.POST})
    public String helloworld() {...}

    /**
     * 請求方法為 GET 或 POST,且具有 hello 參數,且值為 java 的請求才能匹配到該方法,如:
     *   /my?hello=java
     */
    @RequestMapping(params="hello=java", method={RequestMethod.GET, RequestMethod.POST})
    public String hellojava() {...}

    /**
     * params 屬性的另外一種表達形式為:"parameter"
     *
     * 請求方法為 GET,且具有請求參數 java 即匹配此方法,而不管 java 參數的值是什么,如:
     *   /my?java=anything
     */
    @RequestMapping(params="java", method={RequestMethod.GET})
    public String java() {...}

    /**
     * 請求方法為 GET,且具有請求參數 cplusplus 即匹配此方法,而不管 cplusplus 參數的值是什么,如:
     *   /my?cplusplus=anything
     */
    @RequestMapping(params="cplusplus", method={RequestMethod.GET})
    public String cplusplus() {...}

    /**
     * @RequestMapping 還有一個參數化 headers,它和 params 非常相似,也有兩種表達式,只不過它是對
     * 請求頭做限制罷了。大家可以通過 telnet 或 http-client 來發類似的請求以檢驗。以 telnet 為例:
     * 
     * telnet localhost 8080
     * POST /contextPath/my HTTP/1.1
     * Host: localhost
     * hello: world # 這個就是自定義請求頭,和標準的請求頭的寫法別無二致
     * 【回車】
     * 【回車】
     */
    @RequestMapping(headers="hello=world", method={RequestMethod.POST})
    public String cplusplus() {...}
}

      

● @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. }??
        @Controller
@RequestMapping("/my")
public class MyController {

  /**
   * 注意,這里的方法有一個參數。若請求 url 為 /my/test,會匹配此方法。這里的方法的參數名為 userId,
   * 那么請求參數中一定有名為 userId 的參數,且值為整數。這也是默認的綁定行為,它是根據名稱匹配原則進行
   * 的數據綁定。當請求中的參數名與方法名一致的時候,相應的參數值將被綁定到相應的方法參數上。
   * 
   * 如果沒有傳遞 userId 參數,框架會傳入 null。可是這里我們定義的是 primitive type,異常伺候!若
   * 要解決此問題,需要將 primitive type 定義成相應的 wrapper type 即可,這里使用 Integer 就行了。
   *
   * 如果傳遞了 userId 參數,但值不是整數,你叫 test 怎么辦呢?這種情況下,框架借助 PropertyEditor 
   * 數據類型轉換失敗,ExceptionResolver 會接手處理,請求是不會進入 test 方法的。
   *
   * 這種方式下,默認的綁定行為需要我們嚴格遵守命名一致性原則。如果我們對此不滿,想自定義綁定關系,可以求
   * 助于 @RequestParam。
   */
  @RequestMapping("/test")
  public String test(int userId) { ... }

  /**
   * 當我們不想使用 userId 作為方法的參數名,即不想使用默認的數據綁定方式。如果我們要使用 id 作為方法
   * 的參數,為了保證名稱為 userId 的請求參數可以綁定到新的名稱為 id 的方法參數上,我們就可以使用 
   * @RequestParam 對這一參數進行標注。@RequestParam 只可以標注于方法參數上。
   *
   * 如果請求參數中有 age,和方法的參數名稱一致,故 age 參數不需要 @RequestParam 標注。如果沒有傳遞
   * age,我們又不想定義成 Integer,很顯然框架會注入 null 值,報錯是必然的。這是由于 @RequestParam 
   * 的 required 屬性決定的,默認就是 true。如果我們定義成 false,
   * 即 @RequestParam(required=false) int age
   * 這個時候定義成 int 型的 age,即便請求參數沒有 age 參數,也是沒問題的。
   *
   * 同時,這里還能綁定 Date 類型,User 對象類型等等。如 date=2011-01-01&userName=Tom&userAge=18
   * 這里,User 類的屬性需要為 userName 和 userAge,以免和 age,name 混淆。所以,Spring MVC 對對象
   * 的數據綁定就沒有 Struts2 做的那么好了,Strtus2 可以這樣:user.age=18&user.name=Tom
   */
  @RequestMapping("/test2")
  public String test2(@RequestParam("userId") int id, int age, Date date, User user) { ... }
}

      

● @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. }??
        @Controller
@RequestMapping("/my")
public class MyController {

  /**
   * @PathVariable 是 url 模板,需要和 @RequestMapping 配合起來使用,這是 Spring 3.0 之后引入的。
   *
   * 在這個例子中,請求的 url 必須滿足類似 /my/user/zhangsan/18 這樣的格式才能匹配方法。url 模板里
   * 的參數名和方法參數名的綁定規則和 @RequestParam 類似,這里就不再贅述了。
   *
   * @PathVariable 和 @RequestParam 的區別在于:
   *   @PathVariable 的 url:/my//user/zhangsan/18
   *   @RequestParam 的 url:/my//user?nickname=zhangsan&age=18
   */
  @RequestMapping("/user/{nickname}/{age}");
  public String getUserInfo(@PathVariable("nickname") String name, @PathVariable int age) {...}
}

      

● @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. }??
        /**
 * 來看一個 http 請求:
 * (請求行) POST /my HTTP/1.1
 * (請求頭) Host: localhost
 * (請求頭) Content-Type: text/plain
 * (請求頭) Content-Length: 5
 *
 * (請求體) hello
 *
 * 這里的 hello,就是請求體,也稱 request message。若有請求體,則必須提供請求體的類型和長度,這些信
 * 息是寫在請求頭里的,即 Content-Type 和 Content-Length
 */
@Controller
@RequestMapping("/my")
public class MyController {

  /**
   * 我們定義的 body 的數據類型是 String,請求體嘛,肯定是 String。實際上,@RequestBody 是用于將請
   * 求體的內容綁定到方法參數上,數據類型不一定是 String。Spring MVC 是通過 HttpMessageConverter
   * 來完成這種轉換的。AnnotationMethodHandlerAdapter 默認注冊了一些 HttpMessageConverters:
   *   ByteArrayHttpMessageConverter - converts byte arrays
   *   StringHttpMessageConverter - converts strings
   *   FormHttpMessageConverter - converts form data to/from MultiValueMap<String,String>
   *   SourceHttpMessageConverter - convert to/from a javax.xml.transform.Source
   *   MappingJacksonHttpMessageConverter - converts json
   *   MarshallingHttpMessageConverter - convert to/from an object using the 
   *                                     org.springframework.oxm package.
   *
   * 正如上所述,HttpMessageConverter 用于從請求正文綁定到對象和把對象序列化成 String 予客戶端響應。
   * 即 HttpMessageConverter is responsible for converting from the HTTP request message to
   * an object and converting from an object to the HTTP response body
   *
   * 我們可以在 AnnotationMethodHandlerAdapter 定義任意多的 HttpMessageConverters。
   *
   * 既然 HttpMessageConverter 可以用于雙向 convert,這里討論的是 @RequestBody,那這部分我們只講 
   * converting from the HTTP request message to an object。
   *
   * 假設我們只向 AnnotationMethodHandlerAdapter 注入了 MappingJacksonHttpMessageConverter 和
   * MarshallingHttpMessageConverter。處理請求的方法有如下簽名:
   *     public String test(@RequestBody User user) { ... }
   *
   * 不管請求正文的內容是什么,對于客戶端和服務器而言,它們只是用文本來互相通信。把字符串轉為 User 對
   * 象,該用哪個 HttpMessageConverter 來完成此項工作呢?
   *
   * 在定義 HttpMessageConverters 時,我們可以為其指定 supportedMediaTypes。對于將請求正文轉為對象
   * 這個方向的操作,HttpMessageConverters 會從請求頭得到 Content-Type 頭信息,看其是否隸屬于其定義
   * 的 supportedMediaTypes。若沒有匹配上,則會使用下一個 HttpMessageConverter 做同樣的判斷。只要
   * 某個 HttpMessageConverter 支持請求頭中的 Content-Type,那么就會應用此 HttpMessageConverter
   * 來將 String 轉為 Object。當然,若請求正文并沒有按照 Content-Type 所規定的格式來編寫,必然要收到
   * 500 的響應。同時請注意,請求頭中還必須提供 Content-Length,否則拿不到請求正文。
   *
   * 如果所有的 HttpMessageConverters 中定義的 supportedMediaTypes 均不能匹配上 Content-Type 請
   * 求頭中的類型,那么就會收到 415 Unsupported Media Type 響應。
   */
  @RequestMapping("/user/body");
  public String getBody(@RequestBody String body) {

    // 這里的 body 的內容就是 hello
    System.out.println(body);
    return null;
  }
}

      

● @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. }??
        /**
 * 上面的 @RequestBody 講了 HttpMessageConverter 從請求正文到對象轉換的方向,現在來講講另外一個方
 * 向,@ResponseBody,此時,HttpMessageConverter 用于將處理完請求后返回的對象序列化成字符串,即
 * converting from an object to the HTTP response body.
 */
@Controller
@RequestMapping("/my")
public class MyController {

  /**
   * 該方法的返回類型是 User,并不符合含有 @RequestMapping 的注解所需的簽名方式。但它仍然是合法的,因
   * 為在返回類型前有 @ResponseBody 注解,此注解將告知框架,將 User 對象作為影響正文返回?什么?對象
   * 作為響應正文!所以,HttpMessageConverter 在這里就起到作用了。這里討論的是 @ResponseBody,所以
   * 這里我們只講 converting from an object to the HTTP response body。
   *
   * User 對象要轉成什么樣的 String,或者說要轉成什么格式的 String?這個時候需要從請求頭中獲得此信息
   * 了,這里,就是請求頭的 Accept 頭。Accept 頭可以使用逗號分隔定義多個類型,用以告知服務器我只接受
   * 哪些類型的響應。AnnotationMethodHandlerAdapter 中同樣注入了多個 HttpMessageConverter,每個 
   * HttpMessageConverter 都可以定義各自的 supportedMediaTypes。這個時候該用哪個 
   * HttpMessageConverter 來完成對象到文本的序列化操作呢?
   *
   * 遍歷 Accept 頭中的每種媒體類型,在定義的多個 HttpMessageConverters 中依次去匹配,若匹配上,就使
   * 用該 HttpMessageConverter 來完成序列化操作,并且響應頭的 Content-Type 并不是請求頭 Accept 頭
   * 的諸多類型中第一個被匹配的類型,而是匹配到的 HttpMessageConverter 定義的 supportedMediaTypes
   * 中的第一個類型。
   *
   * 如果所有的 HttpMessageConverters 中定義的 supportedMediaTypes 均不能匹配上 Accept 請求頭中
   * 的諸多的類型,那么就會收到 406 Not Acceptable 響應。
   */
  @RequestMapping("/user")
  public @ResponseBody User getUser() {
    return new User(18, "Jack", "計算機");
  }
}

      

● @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. }??
        /**
 * @ModelAttribute 可以為視圖渲染提供更多的模型數據,而不需要在處理請求的方法里添加 ModelMap 或
 * Model 類型的參數。
 *
 * @ModelAttribute 可以標注在方法(存數據)上,也可以標注在方法參數(取數據)上。
 */
@Controller
@RequestMapping("/my")
public class MyController {

  /**
   * 在處理該請求時,方法的返回類型是 User,貌似不符合返回類型的規范。由于這里使用了 @ModelAttribute
   * 注解,表示將返回的對象以 "user" 為 key 放入模型數據里。這里的 key 值默認值是返回的數據類型首字母
   * 小寫的結果。如果想自定義 key,可以寫成 @ModelAttribute("myAttribute"),那么模型數據將會將 
   * User 對象綁定到 key 為 "myAttribute" 上。
   * 
   * jsp 里可以這樣訪問模型里的數據:
   *   age: ${user.age}
   *   name: ${user.name}
   *   job: ${user.job}
   *
   * 當然,這里只是提到了 @ModelAttribute 存數據的操作。
   */
  @RequestMapping("/user")
  @ModelAttribute
  public User getUser() {
    return new User(18, "Jack", "計算機");
  }
  
  /**
   * 這里將 @ModelAttribute 標注在方法參數上,表示要從模型數據里取 key 為 "user" 的對象,綁定在方法
   * 參數上。如果這樣做的話,其實你是得不到上面的那個請求放入的 User 對象,得到的是另外一個對象。其實
   * 也好理解,這是兩個互相獨立的請求,作用域不一樣。要想達到我們的目的,即能夠從模型數據里取數據,需要
   * 求助于 @SessionAttributes
   */
  @RequestMapping("/user2")
  public String showUser(@ModelAttribute User user) {
    System.out.println(user);
    return null;
  }
}

      

● @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. }??
        /**
 * @SessionAttributes 和 @ModelAttribute 類似,只不過 @SessionAttributes 是將數據存放于 session 
 * 中或從 session 中取數據。
 *
 * @SessionAttributes 只能應用在類型聲明上。比如下面的類的聲明中,只有屬性名為 "the-attribute" 的數
 * 據才會納入到 session 的管理。
 *
 * @SessionAttributes 允許以屬性名名稱或者類型兩種方法,來表明將哪些數據通過 session 進行管理。這里
 * 我們使用的是指定屬性名稱的方式,但通過類型來指定也是可行的,如:
 *   @SessionAttributes(types=User.class)
 */
@Controller
@RequestMapping("/my")
@SessionAttributes("the-attribute")
public class MyController {

  @RequestMapping("/getUser")
  public String getUser(int userId, Model model) {
    /**
     * 注意,這里將 User 對象添加到屬性名為 "the-attribute" 上,所以 User 對象將納入到 session 的
     * 管理。如果這里添加的對象的屬性名不是 "the-attribute",那么它只會作用于當前請求,而不會納入到 
     * session 的管理中。
     */
    User user = userService.getUserById(userId);
    model.addAtrribute("the-attribute", user);
    return "userinfo";
  }
  
  /**
   * 將模型里的 "the-attribute" 為 key 的對象綁定到 User 類上。由于在類級別上聲明了只有 "the-
   * attribute" 的屬性名才會納入到 session 的管理,所以就解決了在 @ModelAttribute 注解中講解中最
   * 后提到的問題。
   *
   * 另外,這個方法還有兩個參數,BindingResult 和 SessionStatus。由于這里有綁定數據的動作,我們可以
   * 根據 BindingResult 對象獲得數據綁定結果以決定后繼流程該如何處理。SessionStatus 在這里用于處理
   * 完請求后,清空 session 里的數據。
   */
  @RequestMapping("/updateUser")
  public String updateUser(@ModelAttribute("the-attribute") User user, 
  			BindingResult result, SessionStatus status) {

    if (result.hasErrors) {
      return "error";
    }
    
    userService.updateUser(user);
    // 我們通過調用 status.setComplete() 方法,該 Controller 所有放在 session 級別的模型屬性數據
    // 將從 session 中清空
    status.setComplete();
    return "redirect:getUser?userId=" + user.getId();
  }
}

      


??????? 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. class ,? new ?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. }??
        /**
 * 初始化方法不能有返回值,而且至少應該有一個類型為 org.springframework.web.bind.WebDataBinder 的
 * 方法參數。同時,一個典型的基于注解的 Controller 的處理方法可以使用的方法參數中,除了 Command 對象
 * 以及相關的 Errors/BindingResult 對象作為方法的參數外,都可以作為初始化方法的參數。
 *
 * 這里,我們沒有必要為日期再定制自定義綁定規則,Spring 已經為我們提供了 CustomDateEditor,這里只是演
 * 示如何提供自定義數據綁定規則。
 *
 * 這里的 WebDataBinder,是不是很像 PropertyEditorRegistry?
 */
@InitBinder
public void initBinder(WebDataBinder binder) {
  binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
  
    final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
      try {
        Date date = sf.parse(text);
        setValue(date);
      } catch (ParseException e) {
        Date data = sf.parse(text);
        throw new IllegalArgumentException(e);
      }
    }
  })
}

      

??????? 在 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. }??
        public class MyBindingInitializer implements WebBindingInitializer {

  public void initBinder(WebBinder binder, WebRequest request) {
    binder.registerCustomEditor(SomeDataType.class, somePropertyEditor)
    // 如果需要,這里可以繼續注冊更多的 propertyEditor
    // ......
  }
}

      

Xml代碼 復制代碼 ? 收藏代碼
  1. < bean ? class ="" org .springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter > ??
  2. ?? < property ? name = "webBindingInitializer" > ??
  3. ???? < bean ? class = "...MyBindingInitializer" ? /> ??
  4. ?? </ property > ??
  5. </ bean > ??
        <bean class=""org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter>
  <property name="webBindingInitializer">
    <bean class="...MyBindingInitializer" />
  </property>
</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 /> 移除了。

Spring MVC 中的基于注解的 Controller


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 麻豆国产精品高中生视频 | 特级毛片免费视频观看 | 国产精品二区三区 | 一本大道久久香蕉成人网 | 一级毛片特黄久久免费看 | 亚洲欧美精品成人久久91 | 久久久噜久噜久久gif动图 | 国产资源精品一区二区免费 | 八戒久久精品一区二区三区 | sese在线播放 | 中文字幕在线观看免费视频 | 人人爱人人草 | 欧美大片国产在线永久播放 | 久久精品免费大片国产大片 | 四虎免费大片aⅴ入口 | 亚洲综合图片区 | 中文字幕在线精品视频万部 | 国产成人精品男人免费 | 国产精品欧美亚洲韩国日本 | 久久99国产精品成人 | 经典国产乱子伦精品视频 | 精品色视频| 手机看福利| se色综合视频 | 日日操日日操 | 日本一级毛片高清免费观看视频 | 男人手机天堂 | 日本精品久久久久久久 | 女性下面全部视频免费 | 国产福利视频在线播放 | 99爱网站| 久久午夜综合久久 | 色老头xxxxbbbb视频 | 性欧美极品xxxx欧美一区二区 | 欧美一级一极性活片免费观看 | 久久精品亚洲牛牛影视 | 国产福利视频一区二区三区 | 可以免费观看一级毛片黄a 可以直接看的毛片 | 国产精品一在线观看 | 蕾丝视频永久在线入口香蕉 | 99er久久|