I'm using a template to generate an email in Play. That email includes an URL which is generated through reverse routing :
<a href="@routes.MyController.downloadFile(user).absoluteURL()">
The problem is, the downloadFile
function takes an implicit request: Request
parameter (as any Action
) and I don't always have a Request
when I'm sending an email (it can be triggered out of a request/response workflow).
Is there some way to obtain the reverse route anyway? Or maybe should I pass it a dummy request object (and then, how to generate one?)
Try putting FakeRequest
on the compile classpath
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0"
and then provide it as a default request
parameter to the template
@(foo: String)(implicit request: RequestHeader = play.api.test.FakeRequest())
When implicit request is not available, you should use method described below defined in play.mvc.Call
( play.api.mvc.Call
returned by reverse router extends java play.mvc.Call
class, so there is no additional work needed):
/**
* Transform this call to an absolute URL.
*
* @param secure true if the absolute URL should use HTTPS protocol instead of HTTP
* @param host the absolute URL's domain
* @return the absolute URL string
*/
public String absoluteURL(boolean secure, String host)
Example view code:
@(secure: Boolean, host: String)
<a href="@routes.MyController.downloadFile(user).absoluteURL(secure, host)">
secure
and host
values could be obtained from request or defined somewhere in application configuration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.