FacesContext和“Servlet”上下文 [英] FacesContext and "Servlet" Context
问题描述
是否有任何与FacesContext等效的东西,但是在servlet环境中?
is there any equivalent to FacesContext, but in servlet environment?
我有一些DAOSessionManager来处理我的数据库的事务。当使用JSF编写当前页面时,我可以使用FacesContext来识别当前的http请求,但是关于servlet的那些呢?
I have some DAOSessionManager that handles transaction to my database. I can use the FacesContext to identify the current http request when the current page is written using JSF, but what about servlet ones ?
我找不到任何方法来获取当前的Servlet上下文,或httpRequest ...
I can't find any way to get the current Servlet context, or httpRequest...
谢谢。
PS:是的,有一个参考来自我的DAO层的FacesContext是一个耻辱,但这是一个开始。
PS : yes, having a reference to FacesContext from my DAO layer is a shame, but that's a start.
推荐答案
这是 ServletContext
。它在servlet类中由继承的提供 getServletContext()
方法。
It's the ServletContext
. It's available inside servlet classes by the inherited getServletContext()
method.
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
ServletContext context = getServletContext();
// ...
}
与<$ c的主要区别$ c> FacesContext 是 ServletContext
不是 ThreadLocal
,因此您无法从当前线程静态地获取它例如 FacesContext#getCurrentInstance()
。你真的需要传递 ServletContext
参考DAO方法,无论你需要它:
The major difference with FacesContext
is that the ServletContext
isn't ThreadLocal
, so you cannot obtain it "statically" from the current thread like FacesContext#getCurrentInstance()
does. You really need to pass the ServletContext
reference around into the DAO methods wherever you need it:
someDAO.doSomething(getServletContext());
或者更好的是,为避免紧密耦合,只需从中提取所需信息并传递给它: / p>
Or better yet, to avoid tight coupling, just extract the desired information from it and pass it:
Object interestingData = getServletContext().getAttribute("interestingData");
someDAO.doSomething(interestingData);
这篇关于FacesContext和“Servlet”上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!