Tuesday, March 1, 2011

JSF : How to update one session bean base on another session bean?

I am currently modifying some jsf application. I have two beans.

  • connectionBean
  • UIBean

When I set my connection parameters in connectionBean the first time, the UIBean is able to read my connectionBean information and display the correct UI Tree.

However when I try to set the connection parameters in the same session. My UIBean will still use the previous connectionBean information.

It only will use after I invalidate the whole httpSession.

Is there anyway I can make one session bean update another session bean?

From stackoverflow
  • Sounds to me like it's some kind of problem with UIBean referencing an out-of-date version of ConnectionBean. This is one problem with JSF - if you re-create a bean, JSF will not update the references in all your other beans.

    You could try getting a 'fresh' copy of the ConnectionBean each time. The following method will retrieve a backing bean by name:

    protected Object getBackingBean( String name )
    {
        FacesContext context = FacesContext.getCurrentInstance();
    
        return context
                .getApplication().createValueBinding( String.format( "#{%s}", name ) ).getValue( context );
    }
    

    Without knowing the specifics of your code and how you're using the beans it's difficult to be more specific!

  • @Phill Sacre getApplication().createValueBinding is now deprecated. Use this function instead for JSF 1.2. To get a fresh copy of the bean.

    protected Object getBackingBean( String name )
    {
     FacesContext context = FacesContext.getCurrentInstance();
    
     Application app = context.getApplication();
    
     ValueExpression expression = app.getExpressionFactory().createValueExpression(context.getELContext(),
       String.format("#{%s}", name), Object.class);
    
     return expression.getValue(context.getELContext());
    }
    

0 comments:

Post a Comment