aspose file tools*
The moose likes JSF and the fly likes What is the function of the listener com.sun.faces.config.configureListener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "What is the function of the listener com.sun.faces.config.configureListener? " Watch "What is the function of the listener com.sun.faces.config.configureListener? " New topic
Author

What is the function of the listener com.sun.faces.config.configureListener?

Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I had a problem where sometimes the FacesContext.getCurrentInstance() returns null.

In a thread I've read that adding the listener com.sun.faces.config.configureListener could solve this issue.

I tried to found the exact function of this listener but could not find a good answer.

Maybe someone could give me some information or a hint where to find.

Thanks in advance.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
When exactly did it return null? You need to realize that the FacesContext is only available if the FacesServlet is invoked by a matching URL.
Kumar Mettu
Greenhorn

Joined: Mar 01, 2008
Posts: 6
Remko,

I am assuming you are trying to access FacesContext in ServletFilter. This is where you would FacesContext as null since CurrentFacesContext is set in FacesServlet and Pre Filters are invoked before FacesServlet.
There are known ways of solving this. Best way is discussed here:
http://www.thoughtsabout.net/blog/archives/000033.html

Also you might want to check if you want to use ServletFilter or PhaseListener for your problem.

Kumar.
javaswany.blogspot.com


Remko Strating wrote:I had a problem where sometimes the FacesContext.getCurrentInstance() returns null.

In a thread I've read that adding the listener com.sun.faces.config.configureListener could solve this issue.

I tried to found the exact function of this listener but could not find a good answer.

Maybe someone could give me some information or a hint where to find.

Thanks in advance.


<A href="http://javaswamy.blogspot.com">My Blog</A>
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
I don't recommend this approach.

If he is indeed trying to get the FacesContext inside a Filter, then there's a flaw in his design and he need to elaborate why he needs the FacesContext. If it is just for accessing managed beans or some other scoped attributes or some other stuff made available by request, session and application, he could perfectly use the (Http)ServletRequest for this which is already available inside a Filter.

By the way, why are you spamming an irrelevant link to your blog in every post? If you like to share it with others and want to increase your traffic, just place it in your signature.
Kumar Mettu
Greenhorn

Joined: Mar 01, 2008
Posts: 6
Bauke,

Please see last line of my message about PhaseListner and Servlet Filter.
Also I am not trying to spam. I am returning to JavaRanch after 1999-2000 again and still haven't gotten used to JavaRanch.
Thanks for the Tip.

Kumar.

Bauke Scholtz wrote:I don't recommend this approach.

If he is indeed trying to get the FacesContext inside a Filter, then there's a flaw in his design and he need to elaborate why he needs the FacesContext. If it is just for accessing managed beans or some other scoped attributes or some other stuff made available by request, session and application, he could perfectly use the (Http)ServletRequest for this which is already available inside a Filter.

By the way, why are you spamming an irrelevant link to your blog in every post? If you like to share it with others and want to increase your traffic, just place it in your signature.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
A PhaseListener is only useful if he want to access the JSF components or want to hook on specific JSF phases. Things which I don't see to happen in a Filter which he initially wanted to use.

Well, instead of keep guessing we can also just wait for his reply on my question why actually he needs the FacesContext instance.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Thanks for your support

I use the facescontext for finding a relation between two managed beans. In one I store some user information which is used in a second one by the initialisation in which the name of the creator is gathered from the other managed bean. Mostly this works well, but sometimes the facescontext is null. For which I check and fills in the default user.

From your reaction I will check if the FacesServlet is invoked by a matching URL.

Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Remko Strating wrote:I use the facescontext for finding a relation between two managed beans.
Request scoped beans are just accessible by HttpServletRequest#getAttribute(), session scoped beans by HttpSession#getAttribute() and application scoped beans by ServletContext#getAttribute(). In the other way you can just set them by setAttribute().

With this information you must be able to solve this problem without the need for FacesContext.

Apart from that and continuing with the "flaw-in-design" story, are you familiar with managed properties? Are you aware that you can perfectly inject beans in each other as managed property using faces-config.xml? It really sounds like that you want to associate beans with each other. You don´t need a Filter for this.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Thanks voor your feedback I will try the HttpSession, because both beans are session scoped.

I was aware of managed properties, but not aware that I could inject complete beans with each other.

In the examples of managed properties I have seen only simple types are used. I will try it with complete beans.



Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
It´s fairly simple. You can find here an example: http://balusc.blogspot.com/2006/06/communication-in-jsf.html#InjectingManagedBeansInEachOther
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Thanks I have solved it with a managed property it was fairly simple.

I will test it and if it works good, I will replace my current code with this solution because it's much cleaner than managing the relationships within the code.

Your other remark by solving it with the help of the HttpSession I could not understand.

I need to use the FacesContext.getCurrentInstance() for getting an ExternalContext which I could use for getting the SessionScoped beans or is there a different way for getting the ExternalContext. The reason for this is if the FacesContext.getCurrentInstance returns null. I will have a null pointer exception when getting the ExternalContext.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
JSF API runs on top of Servlet API. JSF stores session scoped beans in the session scope. In Servlet terms that´s just to be done by getting/setting it as attribute of the HttpSession.

ExternalContext#getSessionMap() returns basically a modifable map of HttpSession attributes. Under the hood it just does HttpSession#get/setAttribute(). Exactly the thing which you can also do in a Filter.
 
wood burning stoves
 
subject: What is the function of the listener com.sun.faces.config.configureListener?