【问题标题】:scala play how to pass variables to pagescala play如何将变量传递给页面
【发布时间】:2016-11-07 01:02:10
【问题描述】:

在 Scala + Play 中,如何将变量放入响应中?

在页面中

@lastName @firstName

在应用程序控制器中:

   def index = Action {
    implicit request =>{
        request.setAttribute("lastName", "john"); // not work
        Ok(views.html.index("xxx"))
    } 

}

如果在 java servlet 中,我们可以这样做:

request.setAttribute("name", "value");
request.getRequestDispatcher("page.jsp").forward(request, response);

如何在 Scala + Play 中做同样的事情?

【问题讨论】:

  • Rhys 有正确的答案,但您应该花一些时间来深入了解模板引擎的概念。模板是类型安全的和编译的,它为您提供编译时反馈(+ 编辑时反馈,具体取决于您的 IDE)。请参阅playframework.com/documentation/2.5.x/ScalaTemplates#Overview 了解更多信息。

标签: scala playframework


【解决方案1】:

您想使用视图/页面变量。

在你看来:

@(myStringVar: String)
<b>Hello @myStringVar</b>

你的控制器:

def index = Action {
    implicit request => {
        Ok(views.html.index(myStringVar = "Ooo!"))
    }
}

参考:https://playframework.com/documentation/2.5.x/ScalaTemplates#Overview

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    相关资源
    最近更新 更多