在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中执行相同操作?
答案 0 :(得分:2)
您想使用视图/页面变量。
在您看来:
@(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