Spring MVC请求映射,这可以动态/可配置吗? [英] Spring MVC Request mapping, can this be dynamic/configurable?
问题描述
使用Spring MVC,我知道如何在每个控制器和方法/动作中设置RequestMapping
.
With Spring MVC, I know how you set the RequestMapping
in every controller and method/action.
但是,如果我希望它是可配置的,那么例如,我使用以下控制器:
But what if I wanted this to be configurable, so for example I the following controllers:
BlogController
- with methods for listing blogs entries, single entry, new, update, etc.
ArticleController
- with methods for listing articles entries, single entry, new, update, etc.
现在,在我的应用程序中,管理员可以为webiste设置2个博客,并为文章部分设置1个,因此其网址应为:
Now in my application, the administrator can setup 2 blogs for the webiste, and 1 article section so the urls would be like:
www.example.com/article_section1/ - uses ArticleController
www.example.com/blog1/ - uses BlogController
www.example.com/blog2/ - uses BlogController
也许过了一会,管理员需要另一个文章部分,因此他们只是用新的部分来配置它,例如:
Maybe after a while the administrator wants another article section, so they just configure that with a new section like:
www.example.com/article_section2/
这必须动态/即时运行,而不必重启应用程序.
我的问题仅与如何处理到控制器的url映射有关.
My question is only concerned with how I will handle url mappings to my controllers.
Spring MVC怎么可能?
How would this be possible with Spring MVC?
我只知道如何在控制器或方法级别使用@RequestMapping("/helloWorld")
将url映射到控制器,但这会使url映射固定且无法像我想要的那样进行配置.
I only know how to map urls to controllers using @RequestMapping("/helloWorld")
at the controller or method level, but this makes the url mappings fixed and not configurable like how I want it.
更新:
我将路径存储在数据库中,并映射到控制器的类型,例如:
I will be storing the paths in the database, and with the mapping to the type of controller so like:
path controller
/article_section1/ article
/blog1/ blog
/blog2/ blog
..
有了以上信息,我如何将请求分派到正确的控制器上?
With the above information, how could I dispatch the request to the correct controller?
再次,不希望重新加载/重新部署,我意识到这将需要更多的工作,但在规范中是这样的:)
Again, not looking to reload/redeploy, and I realize this will require more work but its in the spec :)
推荐答案
这种URL映射对您有用吗?
Would this sort of URL mapping work for you?
www.example.com/blog/1/
www.example.com/blog/2/
If yes, then that's easy: Spring 3 supports path variables: http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestmapping-advanced
或者,您可以创建一个通用的请求映射和您自己的子调度程序,以读取配置文件,但是我认为这可能比它值得的工作还要多.
Alternatively, you can create a generic request mapping and your own sub-dispatcher that reads a config file, but I think that's probably more work than it's worth.
这篇关于Spring MVC请求映射,这可以动态/可配置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!