在 Spring MVC 中使用混合註解和 XML 方法進行請求對映

工程 | Rossen Stoyanchev | 2008 年 3 月 24 日 | ...

在 Spring 2.5 中,可以使用註解來配置 Web 應用程式的所有部分。 在 Web 層中看到應用註解特別有趣,因為在那裡開發人員傳統上依賴 SimpleFormController 和 MultiActionController 來處理表單頁面。 註解的引入建立了第三種選擇,它不需要基類,同時仍提供以前方法的靈活性。

雖然很容易看到使用註解的 POJO 來實現 Controller 的優雅之處,但在 URL 到 Controller 對映方面,這種優勢並不明顯。 使用註解定義所有 URL 對映規則會是什麼樣子? 實際上,這是集中式配置在 Spring MVC 應用程式的開發人員中運作良好的一個領域。

讓我們回顧一下 Spring 2.0 中 URL 到 Controller 對映的選項

  1. bean 名稱方法 (BeanNameUrlHandlerMapping)。 每個 bean 的名稱都包含它所服務的路徑。 儘管很簡單,但如果與粗粒度的 servlet 對映(例如 "/browse/*"、"/order/*"、"/reports/*" 等)結合使用,此方法可以擴充套件。
  2. 集中式方法 (SimpleUrlHandlerMapping)。 一箇中心位置,用於檢視 URL 模式和控制器對映。
  3. 約定優於配置方法 (ClassNameUrlHandlerMapping)。 將 URL 路徑與類名匹配。 因此,“/accounts/*”對映到 AccountsController 型別的 MultiActionController。 無需顯式對映。

Spring 2.5 以 @RequestMapping 註解的形式添加了第四個選項,該註解可以放在類或方法上。 當同時放置時,方法級別的對映會縮小類級別的對映。

這是一個 SimpleFormController 風格的工作流程,其中方法級別的對映透過請求方法縮小


@Controller 
@RequestMapping("/editAccount")
public class EditAccountController {

    @RequestMapping(method=RequestMethod.GET)
    public Account setupForm(@RequestParam("id") Long id) {
        ...
        return account;
    }

    @RequestMapping(method=RequestMethod.POST)
    public String processSubmit(Account account) {
        ...
        return "redirect:/success.htm";
    }
}

這是一個 MultiActionController 風格的委託,其中方法級別的對映透過請求方法和相對路徑縮小


@Controller 
@RequestMapping("/accounts/*")
public class AccountsController {

    @RequestMapping(method=RequestMethod.GET)
    public List<Account> list() {...}

    @RequestMapping(method=RequestMethod.GET)
    public Account show(@RequestParam("id") Long id) {...}

    @RequestMapping(method=RequestMethod.POST)
    public String create(Account account) {...}
    ... 
}

如您所見,由於 “/accounts/*” 對映嵌入在程式碼中,因此很難強制執行應用程式範圍內的 Controller 對映約定。 至少沒有嚴格的紀律是不行的。 幸運的是,有一種方法可以將外部 HandlerMapping 與方法級別的 @RequestMapping 註解相結合。 以下示例說明了此方法的工作方式


<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /accounts/*=accountsController
        </value>
    <property>
</bean>

@Controller
public class AccountsController {

    @RequestMapping(method=RequestMethod.GET)
    public List<Account> list() {...}

    @RequestMapping(method=RequestMethod.GET)
    public Account show(@RequestParam("id") Long id) {...}

    @RequestMapping(method=RequestMethod.POST)
    public String create(Account account) {...}
    ... 
}

這裡的控制器對映駐留在基於 XML 的中心對映中,而操作方法對映透過註解指定。 這種方法可以描述為具有基於註解的方法分發的 POJO MultiActionController。 事實上,在 SpringSource 最近的一次溝通中,Juergen 指出,提供基於註解的 MultiActionController 替代方案是 Spring 2.5 的一個明確設計目標,因此我想這並不令人驚訝! 此外,目前正在進行的工作允許您將方法級別的 @RequestMapping 註解與 ControllerClassNameHandlerMapping 約定結合使用(請參閱 SPR-4129)。

那麼這一切的意義是什麼?

配置 Web 層的完全基於 XML 的方法可能會變得繁瑣,但集中式、外部化配置確實有其用武之地。 從具有深度繼承層次結構的特定於框架的基類擴充套件以實現控制邏輯也可能變得繁瑣,並且我們通常認為如果可以避免,就應該避免。 在 Spring MVC 2.5 中,註解可以透過將方法對映規則封裝在 Controller 類中,以及允許您將 Controller 實現為 POJO,來幫助解決這兩個問題。 此外,上述混合方法展示瞭如何獲得外部化配置和基於註解的配置的最佳效果。

獲取 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

遙遙領先

VMware 提供培訓和認證,以加速您的進步。

瞭解更多

獲得支援

Tanzu Spring 在一個簡單的訂閱中為 OpenJDK™、Spring 和 Apache Tomcat® 提供支援和二進位制檔案。

瞭解更多

即將舉行的活動

檢視 Spring 社群中所有即將舉行的活動。

檢視全部