我有一個簡單的閃存實施與澤西使用看起來像這樣:損失類型信息的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編譯,但是謝謝 –
行。謝謝告訴我,對不起,沒有測試。 –