我可以在spring controller类中使用path变量吗? [英] Can I use path variable in spring controller class?

查看:212
本文介绍了我可以在spring controller类中使用path变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 {

}

我们可以这样使用吗?如果是,我们如何读取{version}变量?实际上,根据我要响应的版本,我需要这种方法.如果无法采用上述方法,您能否建议我一个解决该问题的设计?

Can we use like this? If yes how do we read {version} variable? 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?

推荐答案

可以.如果要在其中访问它,只需在您的方法中将其声明为@PathVariable.

Yes you can. Just declare it as @PathVariable in your methods if you want to access it there.

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

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

}

这篇关于我可以在spring controller类中使用path变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆