简体   繁体   English

Spring全局范围可选路径变量

[英]Spring global scope optional path variables

i need to create a global scope optional path variable on my website using Spring, so the same controller could handle a requests with URL's like 我需要使用Spring在我的网站上创建一个全局范围的可选路径变量,以便同一控制器可以处理类似URL的请求

https://website.com/something

and

https://website.com/something/5

And i managed to do that this way 我设法做到了

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

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

But let's say that i have 50 controllers in my application and i don't wan't to modify each and every one of them. 但是,假设我的应用程序中有50个控制器,并且我不想修改每个控制器。 Is there any simpler way to accomplish what i need? 有没有更简单的方法来满足我的需求? (application-scope or controller class-scope) (应用程序范围或控制器类范围)

You can try to extend this controllers from Base Controller that is annotated with @RequestMapping (not so flexible), or you can try design patterns like Decorator Pattern. 您可以尝试从@ControllerMapping注释的基本控制器中扩展此控制器(不够灵活),也可以尝试使用Decorator Pattern之类的设计模式。 https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM