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

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

  • connectionBean
  • UIBean

When I set the connection parameters on the connectionBean for the first time, the UIBean can read my connectionBean information and display the correct UI tree.

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

It will only be used after I invalidate the entire httpSession.

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

0


source to share


3 answers


Sounds like a problem to me with a UIBean referencing an outdated version of the ConnectionBean. This is one of the problems with JSF - if you re-create the bean, JSF will not update references in all other beans.

You can try to get a "new" copy of the ConnectionBean every time. The following method will get support for a 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 use beans, it's hard to be more specific!

+1


source


@Phill Sacre getApplication (). createValueBinding is now deprecated. Use this function instead of 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());
}

      

+1


source


Define constant and static method in first bean session:

public class FirstBean {

public static final String MANAGED_BEAN_NAME="firstBean";

/**
 * @return current managed bean instance
 */
public static FirstBean getCurrentInstance()
{
  FacesContext context = FacesContext.getCurrentInstance();
  return (FirstBean) context.getApplication().evaluateExpressionGet(context, "#{" + FirstBean.MANAGED_BEAN_NAME + "}", TreeBean.class);
}  
...

      

than using in the second session bean like this:

...  
FirstBean firstBean = FirstBean.getCurrentInstance();  
...

      

Better approach would be to use some kind of Injection Injection framework like JSF 2 or Spring.

0


source







All Articles