如何在Spring MVC中基于控制器和操作方法创建URL?

问题描述:

我正在使用Spring MVC 3.0

I am using Spring MVC 3.0

我有一个guestbook.jsp页面,我想创建一个指向GuestBookController登录方法的链接。

I have a guestbook.jsp page where I want to create a link that points to GuestBookController's login method.

这是一个简单的任务,大多数Web框架都会处理这个问题(例如Grails用g:link标签做)但我在官方SpringMVC文档中找不到任何相关文档。

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.

所以我摸不着头脑 - 这个功能在某些标签库中吗?框架是否暴露了它?我是否必须扩展框架才能使其正常工作?

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.

UPDATE
Spring MVC不提供此功能。虽然有一张JIRA票。你可以在这里投票 https://jira.springsource.org/browse/SPR-5779

简短的回答是否定的,你目前无法用Spring MVC做到这一点。

The short answer is no, you can't do this with Spring MVC currently.

这很遗憾,因为你可以在其他框架中做到这一点,包括Grails(在引擎盖下使用Spring MVC)。

It's a shame because you can do this in other frameworks including Grails (which uses Spring MVC under the hood).

参见讨论在其中包含指向添加此功能的Spring功能请求(投票支持!)

See the discussion here which includes a link to a Spring feature request to add this (vote for it!)