How to reverse generate a URL from a route on Play

2019-03-11 12:55发布

Under Play 1.4.x if I wanted to reverse generate a URL I might use something like:

Map<String, Object> map = new TreeMap();
map.put("myParam", myParam);
ActionDefinition ad = Router.reverse("MyAction.query", map);
String url = ad.url;

According to the unfailingly terse Play 2.x documentation, under Play 2 with Scala I'm supposed to use a Redirect:

val action = Action {Redirect(routes.MyAction.query(myParam))}

but what do I do with the action to acquire the actual URL?

1条回答
小情绪 Triste *
2楼-- · 2019-03-11 13:34
val relative = routes.MyAction.query(myParam).url

or absolute

val url = routes.MyAction.query(myParam).absoluteURL()

or absolute https

val securedUrl = routes.MyAction.query(myParam).absoluteURL(true)
查看更多
登录 后发表回答