我有一个简单的闪存实施与泽西使用看起来像这样:损失类型信息的servlet代码
@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.now
读取当前的闪存值。我的now
值总是“什么”。如果我做同样的事情在我的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 }
我失去了一些东西明显?
编辑
我添加了一个小例子,web应用在https://github.com/kardeiz/sc-issue-20160229复制这个问题。
'case Some(x):Option [Wrapper] => x.wrapped'工作吗? (没有测试它)。你的REPL会话与你的代码不一样(它只是测试'x'是'Wrapper',而不是'Some(x)'中的'x''是一个'Wrapper') –
@TheArchetypalPaul那不会' t编译,但是谢谢 –
行。谢谢告诉我,对不起,没有测试。 –