许多Play API会出现运行时异常“此处没有HTTP上下文可用”。例如,在playtemplate中使用以下代码可能会导致此运行时异常。
flash().remove("message")
目前为止,为了解决这个问题,我使用 try/catch 块并在 http 上下文不可用的情况下吃掉异常,但我宁愿检查 http 上下文是否可用,而不是吃掉异常,那么是否有任何 API 允许用户检查 http 上下文是否存在呢? 我尝试了以下内容,但最终也以运行时异常结束。
@if(Http.Context.current() != null) {
@* Do something here which needs http context *@
}