@session.get 无法在 scala html 模板中工作

@session.get not working in scala html template

我的@session.get("id") 在下面无法正常工作。登录系统后,我在控制器中使用 .withSession("id" -> id) 设置了 session 值 id。有没有办法让这个 session.get 工作?

main.scala.html

@(标题:字符串)(内容:Html)

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" cantent="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimun-scale=1.0, user-scalable=no" />
    <title>@title</title>

</head>

<body>
  <div class="main container">
    <div class="navigation container">
      <div class="logo container">
        <div class="user">@session.get("id")</div>
        <div class="separator"></div>
    </div>
    <div class="separator"></div>
    <ul>
      <li class="user"><a href="@routes.AdmnTask.user()">USER</a></li>
      <li class="code"><a href="@routes.AdmnTask.code_list()">CODE</a></li>
           </ul>
  </div>
  <div class="header container">
    <div class="left"></div>
    <a class="right" href="#all-menu"></a>
  </div>
  <div id="mainnav" class="mainnav">
        @content
  </div>
</div>
</body>

找不到此原因:值 session 错误。即使在添加

之后也没有发现同样的问题
@()(implicit session: play.api.mvc.Session)

这是我的 index.scala.html,它成为我模板的基本结构。

@*
 * This template takes a single argument, a String containing a
 * message to display.
 *@
@(message: String)

@*
 * Call the `main` template with two arguments. The first
 * argument is a `String` with the title of the page, the second
 * argument is an `Html` object containing the body of the page.
 *@
@main("Welcome") {

    @*
     * Get an `Html` object by calling the built-in Play welcome
     * template and passing a `String` message.
     *@
    @message

}

请帮忙!

您需要明确地将会话传递给您的模板:

vies.htlm.index(message,session) 来自您的操作。然后进一步将此会话传递给您的 main.scala.html.

并且在 index.scala.html 中,从会话参数中删除隐式。

或者,在您的 index.scala.html 和 main.scala.html 中传递(隐式请求:Request[T])。

@(message: String)(implicit request: Request[T]) -> index
@(title: String)(content: Html)(implicit request: Request[T]) -> main

您可以像这样访问会话:

request.session.get("key")

这就够了。