`RequestMapping`注解中的`name`属性在Spring Framework中有哪些用途?

huangapple go评论75阅读模式
英文:

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.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#name--

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=&quot;BlogComments&quot;, path=&quot;blog/{blog}/comments/{page}&quot;)
     */
    public function listBlogComments(Blog blog, page)
    {
        ...
    }
}

And then we can generate a URL based on the route name.

// This will generate a string &quot;blog/27/comments/1&quot;.
$url = $this-&gt;generateUrl(&#39;BlogComments&#39;, [
    &#39;blog&#39; =&gt; 27,
    &#39;page&#39; =&gt; 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=&quot;BlogComments&quot;, path=&quot;blog/{blog}/comments/{page}&quot;)
    public List&lt;Comment&gt; listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
        ...
    }
}

Now how do I generate a URL based on the name of the mapping which is &quot;BlogComments&quot; 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=&quot;BlogComments&quot;, path=&quot;blog/{blog}/comments/{page}&quot;)
public List&lt;Comment&gt; 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 = &quot;AdminController&quot;)
class BlogController {

    @RequestMapping(name=&quot;BlogComments&quot;, path=&quot;blog/{blog}/comments/{page}&quot;)
    public List&lt;Comment&gt; listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
        ...
    }
}

And then you can access URL using #

&lt;%@ taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;s&quot; %&gt;

&lt;a href=&quot;${s:mvcUrl(&#39;AdminController#BlogComments&#39;).arg(&quot;1&quot;,&quot;123&quot;).build()}&quot;&gt;Get Person&lt;/a&gt;

> 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 = &quot;BlogController&quot;)
class BlogController {

    @RequestMapping(name=&quot;BlogComments&quot;, path=&quot;blog/{name}&quot;)
    public List&lt;Comment&gt; 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.

&lt;%@ taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;s&quot; %&gt;
...
&lt;a href=&quot;${s:mvcUrl(&#39;BlogController#BlogComments&#39;).arg(&quot;test&quot;).buildAndExpand()}&quot;&gt;Get Coments&lt;/a&gt;

huangapple
  • 本文由 发表于 2020年4月11日 12:53:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61152283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定