我有一个用于 Jersey 的简单 Flash 实现,如下所示:
@PostConstruct def before { flash.rotateIn }
@PreDestroy def after { flash.rotateOut }
object flash {
val KeyNow = "local.flash.now"
val KeyNext = "local.flash.next"
// case class Wrapper(wrapped: Map[String, Seq[String]])
case class Wrapper(wrapped: String)
def rotateIn {
for {
session <- Option(request.getSession(false))
obj <- Option(session.getAttribute(KeyNext))
} {
request.setAttribute(KeyNow, obj)
session.removeAttribute(KeyNext)
}
}
def rotateOut {
for (obj <- Option(request.getAttribute(KeyNext))) {
request.getSession.setAttribute(KeyNext, obj)
}
}
def now = Option(request.getAttribute(KeyNow)) match {
case Some(x: Wrapper) => x.wrapped
case Some(x) if x.isInstanceOf[Wrapper] => "WHAT"
case _ => "NOPE"
}
def next(value: String) {
request.setAttribute(KeyNext, Wrapper(value))
}
}
我在这里稍微简化了它,但它让我可以使用 flash.next
为 flash 设置一个值,并使用 flash.now
读取当前的 flash 值。
奇怪的是我的 now
值总是“WHAT”。如果我在我的 REPL 中做类似的事情,我不会遇到同样的问题:
val req = new org.springframework.mock.web.MockHttpServletRequest
val res = req.getSession
res.setAttribute("foo", Wrapper("foo"))
req.setAttribute("foo", res.getAttribute("foo"))
// Is not None
Option(req.getAttribute("foo")).collect { case x: Wrapper => x }
我是否漏掉了一些明显的东西?
编辑
我在 https://github.com/kardeiz/sc-issue-20160229 添加了一个最小的示例 webapp 来复制这个问题.
请您参考如下方法:
我试过你的例子。检查my answer对于您的其他问题,请详细了解模式匹配在这种情况下的工作原理。
简而言之,由于您的Wrapper
是一个内部类,模式匹配还会检查“外部类”引用。似乎根据应用程序服务器的实现,Router.flash
可以是每个请求的不同实例,因此模式匹配失败。
解决这个问题的简单方法是使 Wrapper
成为顶级类,这样它就不会引用任何其他类。