问题描述
我们有几个在Websphere门户环境中运行的JSF Portlet.开发人员想要创建一个可以保存在共享库中的实用程序jar,以便所有portlet都可以访问它.在这个罐子里,他想要一个可以使用静态方法的faces实用程序类.此方法将访问当前的FacesContext实例并返回该上下文的会话映射.
We have several JSF portlets running in a websphere portal environment. A developer wants to create a Utility jar which would live in the shared library, so all portlets could access it. In this jar he wants a faces utility class which would have a static method; this method would access the current FacesContext instance and return that context's session map.
我们一直在讨论这是否是一种明智的方法,以及如何确定如何将正确的上下文实例返回给每个Portlet实例.有这样做的经验,是好是坏?
We have been having some discussion over whether this is a wise approach, and how we know for sure that the correct context instance will be returned to each portlet instance. Any experience doing this, for better or for worse?
推荐答案
FacesContext.getCurrentInstance()封装了与请求关联的人工制品.上下文是线程局部的-它是在请求开始时设置的,并由充当控制器的Portlet放置在末尾.因此,只要您可以保证将代码作为请求的一部分来调用,就应该可以.如果调用转义了请求线程,则会出现问题.
FacesContext.getCurrentInstance() encapsulates the artefacts associated with a request. The context is thread-local - it is set up at the start of a request and disposed at the end by the portlet acting as the controller. So, so long as you can guarantee that the code is invoked as part of the request, it should be OK; problems will occur if the invocation escapes the request thread.
这篇关于从多个Portlet访问Util jar中的FacesContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!