java 如何在 Spring MVC 中基于控制器和操作方法创建 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6022980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a URL based on controller and action method in Spring MVC?
提问by
I am using Spring MVC 3.0
我正在使用 Spring MVC 3.0
I have a guestbook.jsp page where I want to create a link that points to GuestBookController's login method.
我有一个guestbook.jsp 页面,我想在其中创建一个指向GuestBookController 登录方法的链接。
This is a simple task that most web frameworks handle this (e.g grails does it with g:link tag) but I couldn't find any documentation on this in the official SpringMVC docs.
这是大多数 Web 框架处理此问题的简单任务(例如,grails 使用 g:link 标记来完成),但我在 SpringMVC 官方文档中找不到任何关于此的文档。
So I am scratching my head - Is this functionality in some tag library? Does the framework expose it? Do I have to extend the framework to get this to work?
所以我摸不着头脑——这个功能在某个标签库中吗?框架是否公开它?我是否必须扩展框架才能使其工作?
Note, I am not taking about hardcoding the url (which is an obvious but weak solution) but rather generating it based on controller and action name.
请注意,我不是要对 url 进行硬编码(这是一个明显但很弱的解决方案),而是根据控制器和操作名称生成它。
UPDATE: Spring MVC doesn't provide this functionality. There is a JIRA ticket though. You can vote here https://jira.springsource.org/browse/SPR-5779
更新:Spring MVC 不提供此功能。不过有一张 JIRA 票。你可以在这里投票https://jira.springsource.org/browse/SPR-5779
采纳答案by sourcedelica
The short answer is no, you can't do this with Spring MVC currently.
简短的回答是否定的,您目前无法使用 Spring MVC 执行此操作。
It's a shame because you can do this in other frameworks including Grails (which uses Spring MVC under the hood).
很遗憾,因为您可以在包括 Grails(在底层使用 Spring MVC)在内的其他框架中执行此操作。
See the discussion herewhich includes a link to a Spring feature request to add this (vote for it!)
回答by Abdullah Jibaly
Spring MVC uses the standard JSTL tags in JSPs so:
Spring MVC 在 JSP 中使用标准的 JSTL 标记,因此:
<c:url value="/guestBook.html" var="guestBookLink" />
<a href="${guestBookLink}">Guest Book</a>
In your controller:
在您的控制器中:
@RequestMapping(value = "/guestBook")
public String handleGuestBook() { ... }
回答by Adam Gent
For a long time I have thought about implementing something like this using CGLib proxies but was too lazy. It appears Spring HATEOS librarywill allow you to do it the proxy way I wnated to and variety of other ways.
很长一段时间以来,我一直在考虑使用 CGLib 代理来实现这样的东西,但太懒了。看来Spring HATEOS 库将允许您以我所使用的代理方式和其他各种方式来执行此操作。
回答by GriffeyDog
Annotate your login method with @RequestMapping, like so:
使用 @RequestMapping 注释您的登录方法,如下所示:
@Controller
public class GuestBookController {
...
@RequestMapping(value="/mycontextroot/login", method = RequestMethod.GET)
public String login() {
...
}
...
}
Then, in your JSP, create a link something like this:
然后,在您的 JSP 中,创建一个如下所示的链接:
<c:url var="loginlink" value="/mycontextroot/login.html">
</c:url>
<a href="${loginlink}">Login</a>
This assumes that your dispatcher servlet is looking for *.html URLs.
这假设您的调度程序 servlet 正在寻找 *.html URL。