gpt4 book ai didi

java - 如何在 Spring MVC 中使用带有属性名称的 Spring @RequestMapping 注解

转载 作者:行者123 更新时间:2023-12-02 09:36:34 25 4
gpt4 key购买 nike

据我所知,Django 框架提供了一种命名 URL 的方法,因此可以轻松地在 View 方法和模板中引用它们。

例如:

# Definition in coffeehouse/urls.py
path('',TemplateView.as_view(template_name='homepage.html'),name="homepage")

# Definition in view method
from django.http import HttpResponsePermanentRedirect
from django.urls import reverse

def method(request):
....
return HttpResponsePermanentRedirect(reverse('homepage'))

# Definition in template
<a href="{% url 'homepage' %}">Back to home page</a>

Spring @RequestMapping 注解中的 name 属性是什么?

与Django框架中的名称URL相同吗?

如何在 Spring MVC 中使用带有属性名称的 @RequestMapping 注解?

最佳答案

要从处理程序方法的映射注释构建 URL,请使用:

  • 来自 JSP: 请参阅 Building URIs to Controllers and methods from views

    As of 4.1 every @RequestMapping is assigned a default name based on the capital letters of the class and the full method name. For example, the method getFoo in class FooController is assigned the name "FC#getFoo".

    [...]

    The Spring JSP tag library provides a function called mvcUrl that can be used to prepare links to controller methods based on this mechanism.

    For example given:

    @RequestMapping("/people/{id}/addresses")
    public class MyController {

    @RequestMapping("/{country}")
    public HttpEntity getAddress(@PathVariable String country) { ... }
    }

    The following JSP code can prepare a link:

    <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
    ...
    <a href="${s:mvcUrl('PC#getPerson').arg(0,'US').buildAndExpand('123')}">Get Person</a>
  • 来自 Thymeleaf: 请参阅 Building URIs to controllers .

    Since version 4.1, Spring allows the possibility to build links to annotated controllers directly from views, without the need to know the URIs these controllers are mapped to.

    In Thymeleaf, this can be achieved by means of the #mvc.url(...) expression object method, which allows the specification of controller methods by the capital letters of the controller class they are in, followed by the name of the method itself. This is equivalent to JSP’s spring:mvcUrl(...) custom function.

    For example, for:

    public class ExampleController {

    @RequestMapping("/data")
    public String getData(Model model) { ... return "template" }

    @RequestMapping("/data")
    public String getDataParam(@RequestParam String type) { ... return "template" }

    }

    The following code will create a link to it:

    <a th:href="${(#mvc.url('EC#getData')).build()}">Get Data Param</a>
    <a th:href="${(#mvc.url('EC#getDataParam').arg(0,'internal')).build()}">Get Data Param</a>
  • 从另一个处理程序重定向:请参阅MvcUriComponentsBuilder.fromMappingName(String mappingName) .

    Create a URL from the name of a Spring MVC controller method's request mapping.

    The configured HandlerMethodMappingNamingStrategy determines the names of controller method request mappings at startup. By default all mappings are assigned a name based on the capital letters of the class name, followed by "#" as separator, and then the method name. For example "PC#getPerson" for a class named PersonController with method getPerson. In case the naming convention does not produce unique results, an explicit name may be assigned through the name attribute of the @RequestMapping annotation.

    This is aimed primarily for use in view rendering technologies and EL expressions. The Spring URL tag library registers this method as a function called "mvcUrl".

    For example, given this controller:

    @RequestMapping("/people")
    class PersonController {

    @RequestMapping("/{id}")
    public HttpEntity<Void> getPerson(@PathVariable String id) { ... }

    }

    A JSP can prepare a URL to the controller method as follows:

    <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>

    <a href="${s:mvcUrl('PC#getPerson').arg(0,"123").build()}">Get Person</a>

    Note that it's not necessary to specify all arguments. Only the ones required to prepare the URL, mainly @RequestParam and @PathVariable).

    要使用它,您需要执行以下操作:

    return "redirect:" + MvcUriComponentsBuilder.fromMappingName("PC#getPerson")
    .arg(0,"123")
    .build();

关于java - 如何在 Spring MVC 中使用带有属性名称的 Spring @RequestMapping 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57465379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com