英文:
What are the use cases of the `name` property of the `RequestMapping` annotation in Spring Framework?
问题
抱歉,如果你觉得这个问题很愚蠢...我对Spring框架还不熟悉。我花了很多时间寻找答案...
根据官方的Spring Framework文档,你可以使用RequestMapping
注解的name
属性为其分配一个名称。
所以问题是给路由映射(route mapping)指定名称的意义是什么?
在Symfony框架中,我们可以使用映射名称生成URL。
class BlogController
{
/**
* @Route(name="BlogComments", path="blog/{blog}/comments/{page}")
*/
public function listBlogComments(Blog blog, page)
{
...
}
}
然后我们可以根据路由名称生成URL。
// 这将生成字符串 "blog/27/comments/1"。
$url = $this->generateUrl('BlogComments', [
'blog' => 27,
'page' => 1
]);
这得益于Symfony\Component\Routing\Router
组件。
在Spring框架中,控制器类可以写成以下方式。
@Controller
@ResponseBody
class BlogController {
@RequestMapping(name="BlogComments", path="blog/{blog}/comments/{page}")
public List<Comment> listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
...
}
}
现在,我如何基于映射的名称(在这种情况下为 "BlogComments")生成URL呢?在Spring框架中是否有任何可用的组件或服务,就像Symfony框架中一样?还有哪些其他可能的用例呢?
英文:
Apologies, if you find this question dumb... I am new to Spring framework. I spent hours looking up an answer...
According to the official Spring Framework documentation, you can assign a name to a RequestMapping
annotation using the name
property.
So the question is what's the point of giving a name to a route mapping?
In Symfony framework, we can use the mapping name to generate a URL.
class BlogController
{
/**
* @Route(name="BlogComments", path="blog/{blog}/comments/{page}")
*/
public function listBlogComments(Blog blog, page)
{
...
}
}
And then we can generate a URL based on the route name.
// This will generate a string "blog/27/comments/1".
$url = $this->generateUrl('BlogComments', [
'blog' => 27,
'page' => 1
]);
This is possible thanks to the Symfony\Component\Routing\Router
component.
The controller class can be written like the following in the Spring framework.
@Controller
@ResponseBody
class BlogController {
@RequestMapping(name="BlogComments", path="blog/{blog}/comments/{page}")
public List<Comment> listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
...
}
}
Now how do I generate a URL based on the name of the mapping which is "BlogComments"
in this case? Is there any Spring component or Service available as in the Symfony framework? What other possible use cases are available?
答案1
得分: 1
从文档中:
> public abstract String name
>
> 为此映射分配一个名称。在类型级别和方法级别都支持!当同时在这两个级别上使用时,将通过连接以“#”作为分隔符来派生组合名称。
关键时刻是“当同时在这两个级别上使用时”
因此,您应该为控制器分配一个名称,以及为方法分配一个名称,然后它应该开始工作。
@Controller
@ResponseBody
@RequestMapping(name = "AdminController")
class BlogController {
@RequestMapping(name="BlogComments", path="blog/{blog}/comments/{page}")
public List<Comment> listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
...
}
}
然后您可以使用 # 访问 URL
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<a href="${s:mvcUrl('AdminController#BlogComments').arg("1","123").build()}">获取人员</a>
> 应用程序可以通过名称使用静态方法 MvcUriComponentsBuilder#fromMappingName 或在 JSP 中通过 Spring 标签库注册的“mvcUrl”函数构建到控制器方法的 URL。
英文:
From documentation:
> public abstract String name
>
> Assign a name to this mapping. Supported at the type level as well as
> at the method level! When used on both levels, a combined name is
> derived by concatenation with "#" as separator.
The key moment is When used on both levels
So you should assign a name for the controller as well as well and it should start working.
@Controller
@ResponseBody
@RequestMapping(name = "AdminController")
class BlogController {
@RequestMapping(name="BlogComments", path="blog/{blog}/comments/{page}")
public List<Comment> listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
...
}
}
And then you can access URL using #
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<a href="${s:mvcUrl('AdminController#BlogComments').arg("1","123").build()}">Get Person</a>
> Applications can build a URL to a controller method by name with the
> help of the static method MvcUriComponentsBuilder#fromMappingName or
> in JSPs through the "mvcUrl" function registered by the Spring tag
> library.
答案2
得分: 1
@RequestMapping
注解的name
属性可用于为控制器类和方法的映射指定名称。
@Controller
@ResponseBody
@RequestMapping(name = "BlogController")
class BlogController {
@RequestMapping(name="BlogComments", path="blog/{name}")
public List<Comment> listBlogComments(@PathVariable String name) {
...
}
}
Spring JSP标签库提供了一个名为mvcUrl
的函数,可用于根据此机制准备链接到控制器方法。
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
...
<a href="${s:mvcUrl('BlogController#BlogComments').arg('test').buildAndExpand()}">获取评论</a>
英文:
@RequestMapping
annotation's name attribute can be used to assign a name to the mapping of controller class and method.
@Controller
@ResponseBody
@RequestMapping(name = "BlogController")
class BlogController {
@RequestMapping(name="BlogComments", path="blog/{name}")
public List<Comment> listBlogComments(@PathVariable String name) {
...
}
}
The Spring JSP tag library provides a function called mvcUrl that can be used to prepare links to controller methods based on this mechanism.
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
...
<a href="${s:mvcUrl('BlogController#BlogComments').arg("test").buildAndExpand()}">Get Coments</a>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论