如何使用action作为play 2.0中其他操作的参数

时间:2016-03-31 14:01:28

标签: playframework playframework-2.4 play-templates

我在模板中有以下功能:

@activeLink(currentPath:String) = @{
    if(request.path.equals(currentPath)) {
        "active"
    }

}

(来自:Play Framework: How do I change active class on route change

现在我想把它称为:

@activeLink(@routes.Application.index().url)

但是我收到以下错误:

play.sbt.PlayExceptions$CompilationException: Compilation error[illegal start of simple expression]
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
    at scala.Option.map(Option.scala:145) ~[scala-library-2.10.5.jar:na]
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49) ~[na:na]
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44) ~[na:na]
    at scala.Option.map(Option.scala:145) ~[scala-library-2.10.5.jar:na]
    at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44) ~[na:na]
    at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40) ~[na:na]
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
    at scala.util.Either$LeftProjection.map(Either.scala:377) ~[scala-library-2.10.5.jar:na]
    at play.sbt.run.PlayReload$.compile(PlayReload.scala:17) ~[na:na]
    at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3$$anonfun$2.apply(PlayRun.scala:61) ~[na:na]
    at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3$$anonfun$2.apply(PlayRun.scala:61) ~[na:na]
    at play.runsupport.Reloader$$anonfun$reload$1.apply(Reloader.scala:323) ~[na:na]
    at play.runsupport.Reloader$$anon$3.run(Reloader.scala:43) ~[na:na]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_60]
    at play.runsupport.Reloader$.play$runsupport$Reloader$$withReloaderContextClassLoader(Reloader.scala:39) ~[na:na]
    at play.runsupport.Reloader.reload(Reloader.scala:321) ~[na:na]
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:113) ~[play-server_2.10-2.4.6.jar:2.4.6]
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:111) ~[play-server_2.10-2.4.6.jar:2.4.6]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) ~[scala-library-2.10.5.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) ~[scala-library-2.10.5.jar:na]
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) ~[akka-actor_2.10-2.3.13.jar:na]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) ~[akka-actor_2.10-2.3.13.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library-2.10.5.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library-2.10.5.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library-2.10.5.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library-2.10.5.jar:na]

将url传递给模板中的函数的正确方法是什么?

使用Play 2.4.6和Java 8测试。

1 个答案:

答案 0 :(得分:1)

参数必须通过而不是' @'试试@activeLink(routes.Application.index().url)