簡體   English   中英

Spring全局范圍可選路徑變量

[英]Spring global scope optional path variables

我需要使用Spring在我的網站上創建一個全局范圍的可選路徑變量,以便同一控制器可以處理類似URL的請求

https://website.com/something

https://website.com/something/5

我設法做到了

@RequestMapping("/something")
public class SomethingController {

  @RequestMapping(value = {"", "/{id}) //<-- important line here
  public Model something([...]) {
    return [...]

但是,假設我的應用程序中有50個控制器,並且我不想修改每個控制器。 有沒有更簡單的方法來滿足我的需求? (應用程序范圍或控制器類范圍)

您可以嘗試從@ControllerMapping注釋的基本控制器中擴展此控制器(不夠靈活),也可以嘗試使用Decorator Pattern之類的設計模式。 https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM