How to insert link to controller action in Play framework 2.0 template
Asked Answered
R

4

14

If I have an action Application.show(tag: String), and also have a corresponding routing entry, how can I insert a link to this action to a template without crafting the url manually?

I would like to do something like magiclink(Application.show("tag")).

Razorbill answered 16/9, 2012 at 14:12 Comment(0)
W
19

syntax:

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

By analogy you can also generate urls in your controllers. ie. for redirecting after some action:

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"));
}
Woodring answered 16/9, 2012 at 14:43 Comment(0)
L
5

The format for putting a URL from your routes file in your html is as follow:

@routes.NameOfYourClass.nameOfyourMethod()

So, if in your routes file you have:

GET     /products                   controllers.Products.index()

And your Products class looks like this:

public class Products extends Controller {

    public Result index() {
        return ok(views.html.index.render());
    }
}

Your <a> should look like this:

<a href="@routes.Products.index()">Products</a>

In addition: If your method can accept parameters, then you can of course pass them in between the parenthesize of your method like this: index("Hi").

I hope this answer is more clear to understand.

Liba answered 19/8, 2015 at 13:31 Comment(1)
what if my Controller is in sub packages? let's say it is in com.mycompany.myproduct.controllers.LoginController.index()Reisfield
M
4

The accepted answer is right, but it doesn't cover the case where the controller is in a sub-package, i.e.: controllers.applications.MyFavouriteApplication.show()

Since I had a hard time finding the answer, I'll post it here.

To put a non-scoped link into a template, the proper pattern is @controllers.{sub-packages if any}.routes.{your class}.{your method}()

So in this case it would be @controllers.applications.routes.MyFavouriteApplication.show()

IF you were using the recommended Play pattern of using @Inject to create singleton controller objects, and IF you thought the correct answer was @controllers.applications.MyFavouriteApplication.show(), you would get an error like this:

Object MyFavouriteApplication is not a member of controllers.applications. Note: class MyFavouriteApplication exists, but it has no companion object.

Given that you had already supplied the @Inject() @Singleton annotation, this would seem like a very strange error indeed. It might make you question if you were building the project correctly. Determining the true cause could cost you considerably in blood and treasure.

Magnetron answered 27/11, 2017 at 23:47 Comment(0)
R
3

Ah, as simple as @{routes.Application.show("tag")}.

Razorbill answered 16/9, 2012 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.