1

First of all, All is done in JSF 1.2. In short, I have this code:

public class MobileConfigurationToolSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent event) { ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); externalContext.getSessionMap().put("myBean", new MyBean()); } ... } 

The bean is accessed from the JSF context as a regular JSF session bean.

<h:outputText value="#{myBean.textMsg}"/> 

The result is an almost infinite loop which finally ends due to:

011-04-06 15:47:42,435 WARN [http-80-1] org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase PanelGrid GeneralConfigForm:j_id_jsp_1651416019_7:0:GeneralDetailPanel has not enough children. Child count should be a multiple of the columns attribute. 2011-04-06 15:47:42,825 IDT] E 0000000c (org.apache.catalina.connector.CoyoteAdapter#service) An exception or error occurred in the container during the request processing ava.lang.ArrayIndexOutOfBoundsException 

What I'm trying to achieve is to create a bean object by my self (According to URL parameters or cookies) in the session listener and "inject" the new bean to the JSF session.

What Am I doing wrong?

Thanks!

EDIT- I thought that another solution could be to make the myBean instantiate somehow by JSF and then run methods on it to achieve the same idea. I remember a @BalusC article on how to make JSF instantiate a bean from code but I couldn't find it. (I'm Guessing @BalusC will answer me on this so thanks in advance :-) )

1 Answer 1

2

I have this code:

As to the listener, you shouldn't access FacesContext in there. It is not guaranteed to be present there since the HttpSessionListener is not part of JSF. If the session is been created upon a request which does not involve FacesServlet, the FacesContext#getCurrentInstance() will return null.

The correct way would be

event.getSession().setAttribute("myBean", new MyBean()); 

The result is an almost infinite loop which finally ends due to:

As to the infinite loop, the repeating part of the stacktrace is more interesting than the ending part. I suspect that this is caused by another bug.


What I'm trying to achieve is to create a bean object by my self (According to URL parameters or cookies) in the session listener and "inject" the new bean to the JSF session.

As to the functional requirement, just declare the bean session scoped and do the desired job in its constructor or @PostConstruct. Whenever you reference it in the view, its constructor and @PostConstruct will run. You have access to the request parameters and cookies just there.

public MyBean() { // Constructor. Map<String, String> params = externalContext.getRequestParameterMap(); Map<String, Object> cookies = externalContext.getRequestCookieMap(); // ... } @PostConstruct public void init() { // Or here, if you're dependent on managed properties. } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I guessed that was the answer but wanted the advice from the pro :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.