spring - Playframework2就像 Spring 的反向路由

标签 spring routes reverse thymeleaf

任何人都可以建议我 Spring 的路由机制吗?

我使用 thymeleaf 作为我的 View ,并且我想在 View 中使用类名和方法名作为我的 url - 就像在 playframework 中一样。

但我喜欢在 spring 中在 Controller 方法声明之前定义 url。

等待您的建议。谢谢。

最佳答案

从 4.1 版本开始,Spring Framework 提供了一种从模板生成到资源的路由的方法(即 View 中的反向路由)。

您可以查看the reference documentation on the subject ,但它基本上使用自动生成的命名路由。

我不知道 Thymeleaf 是否支持其标准方言,但是 you could quite easily extend it ;如果没有,这可能是一个可以为 Thymeleaf 项目贡献的功能。

假设您有一个像这样的 MyUserController:

@Controller
public class MyResourceController {

  @RequestMapping("/user/{name}")
  public String showUser(String name, Model model) {

    ...
    return "show";
  }
}

使用这样的方言,您可以引用这样的操作:

<a th:uri="mvcUrl('MRC#ShowUser').buildAndExpand('bob')">Show user Bob</a>
<!-- will generate "/user/bob" -->

关于spring - Playframework2就像 Spring 的反向路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080454/

相关文章:

xml - Eclipse Spring STS "Config Set"有什么用?

ruby-on-rails - Rails 中的漂亮路径

python - 快速开发路由协议(protocol)原型(prototype)的环境

php - 在 php 中反转 MySQL 结果

java - 无法写入 JSON : No serializer found for class org. json.JSONObject 并且未发现创建 BeanSerializer 的属性

java - Spring MVC 项目上的“缺少 Artifact ”和 'Failed to read artifact descriptor' 错误

java - Java 中 spring-security 的替代方案(spring)

php - Laravel 5.1 ACL 路由资源不工作

python - 需要备用 Python 列表反向解决方案

c - 使用 C 中的 POW 函数反转五位数字