【问题标题】:Passing a session variable with form in Scala/Play framework在 Scala/Play 框架中使用表单传递会话变量
【发布时间】:2013-11-20 06:56:39
【问题描述】:

我正在从事的项目允许登录的用户通过表单添加图像来将图像添加到他们的个人资料中。我想知道是否可以自动传递用户名,以便用户在添加图像时不必输入他的用户名。我想通过让网站自动添加第一个输入文本框来摆脱它。我知道我可以使用 @{session.get("displayName")} 访问它,但不知道如何通过表单传递它。

@main("addimage")  {
@helper.form(action = routes.Images.processImageForm) {  <!--  <----------**************************----------------->
    <fieldset>
        <legend>Add My Picture</legend>


        @inputText(
            addImageForm("creatorUsername"),
            '_label -> "Add Name",
            '_help -> "Please enter your display name",
            '_error -> addImageForm.globalError
        )

        @inputText(
            addImageForm("url"),
            '_label -> "Add Image",
            '_help -> "Please add your image-url",
            '_error -> addImageForm.globalError
        )

        @inputText(
            addImageForm("description"),
            '_label -> "Add Description",
            '_help -> "Please enter your Description",
            '_error -> addImageForm.globalError
        )


    </fieldset>




    <div class="actions">
        <input type="submit" class="btn primary" value="Add Image">
        <a href="@routes.Application.index" class="btn">Cancel</a>
    </div> }

【问题讨论】:

    标签: scala playframework


    【解决方案1】:

    当您的用户登录时,您可以使用 session 来识别他们,play 使用 cookie 来存储 session 相关信息,您的 session 应该包含一些标识用户的标识符,在控制器中只需从 session 中获取此标识符并基于例如,您可以从 DB 中检索使用,无需以如下形式发送用户名:

    def formHandling =  Action { implicit request =>
            val userId = request.session.get(userSession)
            // handling form
    
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多