本文介绍了从 Constraints.ValidationPayload 获取 TypedMap 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将 playframework 2.8 与 java 一起使用,并使用 DI 和有效负载实现了表单验证,如官方播放文档中所述https://www.playframework.com/documentation/2.8.x/JavaForms#Custom-class-level-constraints-with-DI-支持

We're using playframework 2.8 with java and have implemented a form validation using DI and a payload as explained in the official play documentation https://www.playframework.com/documentation/2.8.x/JavaForms#Custom-class-level-constraints-with-DI-support

有效负载对象使用 getAttr() 方法提供一个包含来自请求的属性的 TypedMap.这由 本文档

The payload object provides a TypedMap containing attributes from the request, using the getAttr() method. This is explained by this documentation

由于 TypedKey 的实例用于在映射中存储值,我们无法访问由框架本身存储的任何请求属性.更多详细信息可以在 Github这篇 Stackoverflow 帖子

Since the instance of a TypedKey is used to store the value in the map, we're not able to access any request attributes, stored by the framework itself. More details can be found on Github and in this Stackoverflow post

看来,不可能从 TypedMap 中获取所有现有的键.
那么,问题是:当我们没有 TypedKey 的实例时,我们如何获取已经由 play 存储的 TypedMap 的值?

It seems, it's not possible to fetch all existing keys from a TypedMap.
So, the question is: How can we get values of the TypedMap, which were already stored by play, when we don't have the instance of the TypedKey?

推荐答案

Request.attrs 的 Keys for Request.attrs TypedMap 存储在 play.api.mvc.request.RequestAttrKey 对象中:

Keys for Request.attrs TypedMap are stored inside play.api.mvc.request.RequestAttrKey object:

package play.api.mvc.request

import ...

/**
 * Keys to request attributes.
 */
object RequestAttrKey {

  /**
   * The key for the request attribute storing a request id.
   */
  val Id = TypedKey[Long]("Id")

  /**
   * The key for the request attribute storing a [[Cell]] with
   * [[play.api.mvc.Cookies]] in it.
   */
  val Cookies = TypedKey[Cell[Cookies]]("Cookies")

  /**
   * The key for the request attribute storing a [[Cell]] with
   * the [[play.api.mvc.Session]] cookie in it.
   */
  val Session = TypedKey[Cell[Session]]("Session")

  /**
   * The key for the request attribute storing a [[Cell]] with
   * the [[play.api.mvc.Flash]] cookie in it.
   */
  val Flash = TypedKey[Cell[Flash]]("Flash")

  /**
   * The key for the request attribute storing the server name.
   */
  val Server = TypedKey[String]("Server-Name")

  /**
   * The CSP nonce key.
   */
  val CSPNonce: TypedKey[String] = TypedKey("CSP-Nonce")
}

那些是 Scala 键.这不是大问题,java TypedMap 只是 scala TypedMap 的包装器.

Those are scala keys. This is not big problem, java TypedMap is just wrapper for scala TypedMap.

来自 java 的示例用法,当我们有 Http.Request 时:

Example usage from java, when we have Http.Request:


import scala.compat.java8.OptionConverters;
import play.api.mvc.request.RequestAttrKey;

class SomeController extends Controller {

    public Result index(Http.Request request) {

        //get request attrs  
        play.libs.typedmap.TypedMap javaAttrs = request.attrs();

        //get underlying scala TypedMap
        play.api.libs.typedmap.TypedMap attrs = javaAttrs.asScala();

        //e.g. get Session from scala attrs
        Optional<Cell<Session>> session = 
            OptionConverters.toJava(attrs.get(RequestAttrKey.Session()));

        //process session data
        session.ifPresent(sessionCell -> {
            Map<String, String> sessionsData = sessionCell.value().asJava().data();

        //do something with session data
        });
    }
}

这篇关于从 Constraints.ValidationPayload 获取 TypedMap 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:48