如何在Play framework 2.0模板中插入控制器操作的链接

ron*_*ron 13 templates hyperlink playframework-2.0

如果我有一个操作Application.show(tag: String),并且还有相应的路由条目,如何在不手动创建URL的情况下将此操作的链接插入模板?

我想做点什么magiclink(Application.show("tag")).

bie*_*ior 18

句法:

<a href='@routes.Application.show("some")'>My link with some string</a>
Run Code Online (Sandbox Code Playgroud)

通过类比,您还可以在控制器中生成URL.即.一些动作后重定向:

public static Result justRedirect(){

    // use as String
    String urlOfShow = routes.Application.index().toString().

    // or pass as a redirect() arg
    return redirect(routes.Application.show("some"));
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*tti 5

routes文件中的 URL 放入html 的格式如下:

@routes.NameOfYourClass.nameOfyourMethod()
Run Code Online (Sandbox Code Playgroud)

因此,如果您的routes文件中有:

GET     /products                   controllers.Products.index()
Run Code Online (Sandbox Code Playgroud)

你的Products班级看起来像这样:

@routes.NameOfYourClass.nameOfyourMethod()
Run Code Online (Sandbox Code Playgroud)

<a>应该是这样的:

<a href="@routes.Products.index()">Products</a>
Run Code Online (Sandbox Code Playgroud)

另外:如果你的方法可以接受参数,那么你当然可以把它们放入你的方法是这样的圆括号之间: index("Hi")

我希望这个答案更容易理解。

  • 如果我的控制器在子包中怎么办?假设它在`com.mycompany.myproduct.controllers.LoginController.index()`中 (2认同)