Spring 请求将 URL 映射到一个控制器
我最近问了一个关于映射到同一控制器的类似网址的问题。 在这种情况下,我必须给它 3 或 4 个 url,这很好。
该示例包含以下内容。
@RequestMapping({"/protect", "/protekt", "/proteckt", "/protext"})
我现在的问题是有 100-150 个 url 基本上会通过一个控制器。这些 URL 将包含在数据库中。我正在寻找一种优雅的方法来在请求映射中维护它,而不是放置 100 个不同的 URL(可能会更改),特别是因为我正在工作的客户不想对后端进行任何更改。有没有一种优雅的方法来做到这一点?谢谢。
I have asked a recent question regarding a similar urls mapping to the same controller.
In this case I had to give it 3 or 4 urls which is fine.
The example contained the following.
@RequestMapping({"/protect", "/protekt", "/proteckt", "/protext"})
My issue now is there are 100-150 urls that weill essentially go through ONE controller. These URL's are to be contained in a database. I am looking for an elegant way to maintain this in the Request Mapping rather than putting 100 different URLS(that may change) particularly because the client I am working for doesn't want to make any changes to the back end. Is there an elegant way to do this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能会首先考虑提供
SimpleUrlHandlerMapping
子类,从数据库中获取其值列表。它非常简单,使您无需进行任何扫描,并使配置保持本地化到一小块 XML(加上数据库内容)。(我认为您只需要实现
HandlerMapping
但从SimpleUrlHandlerMapping
开始可能会更快 - 这将是明天的测试黑客我。)你没有说这些 URL 是否需要在运行时更改;如果他们这样做,那么你需要能够在更深的层次上连接来更新实际的映射——我不知道如何做到这一点。
I'd probably start by looking at ways to provide a
SimpleUrlHandlerMapping
subclass that grabbed its list of values from the DB. It's pretty straight-forward, keeps you from having to hook into any scanning, and keeps the configuration pretty localized to one chunk of XML (plus DB stuff).(I think you only need to implement a
HandlerMapping
but it might be quicker to start from aSimpleUrlHandlerMapping
--that'll be tomorrow's test hack for me.)You don't say whether or not these URLs need to change at runtime; if they do then you'd need the ability to hook in at a deeper level to update the actual mappings--not sure how to do that off the top of my head.