简体   繁体   English

我可以在spring控制器类中使用路径变量吗?

[英]Can I use path variable in spring controller class?

Can I use path variable for spring controller class? 我可以在弹簧控制器类中使用路径变量吗?

I know that we can use path variables in controller's methods. 我知道我们可以在控制器的方法中使用路径变量。 In the same fashion can we use it for entire class? 我们可以用同样的方式将它用于全班吗?
Eg: 例如:

@Controller
@RequestMapping(value = "{version}/test")
class TestController {

}

Can we use like this? 我们可以这样使用吗? If yes how do we read {version} variable? 如果是,我们如何阅读{version}变量? Actually i need this kind of approach, based on the version i'll respond. 实际上我需要这种方法,基于我会回应的版本。 If the above approach is not possible can you please suggest me a design to solve this problem? 如果上述方法无法实现,请您建议我设计一下来解决这个问题吗?

Yes you can. 是的你可以。 Just declare it as @PathVariable in your methods if you want to access it there. 如果要在那里访问它,只需在方法中将其声明为@PathVariable即可。

@Controller
@RequestMapping(value = "{version}/test")
class TestController {

    @RequestMapping(value="/something")
    public ModelAndView doSomething(@PathVariable String version) {
       // do something here with the version
    }

}

暂无
暂无

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

相关问题 如何在Spring Controller类中使用路径变量? - How to use path variable in a spring controller class? 我可以在 spring webfilter 中使用通配符作为路径变量吗? - Can i use wildcard for path variable in spring webfilter? 使用Spring将路径变量传递给控制器 - Passing A Path Variable To A Controller With Spring 这个 Spring Boot controller 方法注释有什么问题? (请求的路径在 class 级别定义,路径变量在方法中定义) - What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method) Spring - 在 controller 检查 null 的路径变量 - Spring - checking path variable for null at controller Spring controller 测试路径变量给出 404 - Spring controller test path variable gives 404 Spring-注释-控制器-在控制器类中使用setRedirectAction - Spring - Annotation - Controller - Use setRedirectAction in controller class 使用Spring 4.2,我如何将路径变量作为可选项 - With Spring 4.2 how can i make the path variable as optional 我可以在spring-mvc中使用相同ViewViewsolver的相同类两次,并使用不同的“ order”属性来重新整理控制器的结果吗? - can I use the same class of some ViewResolver in spring-mvc two times with different property “order” to resovler the result from controller? 如何在不使用注释的情况下创建Spring控制器? - How can I create a Spring controller without the use of the annotations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM