任何人都可以建议我 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/