JSF:如何基于另一个会话bean更新一个会话bean?

时间:2022-05-14 19:11:21

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

我目前正在修改一些jsf应用程序。我有两个豆子。

  • 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.

当我第一次在connectionBean中设置连接参数时,UIBean能够读取我的connectionBean信息并显示正确的UI树。

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

但是,当我尝试在同一会话中设置连接参数时。我的UIBean仍将使用先前的connectionBean信息。

It only will use after I invalidate the whole httpSession.

它只会在我使整个httpSession无效后使用。

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

无论如何我可以让一个会话bean更新另一个会话bean吗?

3 个解决方案

#1


1  

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.

对我来说听起来像是UIBean引用过时版本的ConnectionBean的某种问题。这是JSF的一个问题 - 如果重新创建bean,JSF将不会更新所有其他bean中的引用。

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

您可以尝试每次都获取ConnectionBean的“新”副本。以下方法将按名称检索辅助bean:

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!

在不知道代码细节以及如何使用bean的情况下,很难更具体!

#2


1  

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

@Phill Sacre getApplication()。现在不推荐使用createValueBinding。使用此函数代替JSF 1.2。获得豆子的新副本。

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());
}

#3


0  

Define constant and static method in first session bean:

在第一个会话bean中定义常量和静态方法:

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 use in second session bean like this:

比在第二个会话bean中使用这样:

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

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

更好的方法是使用一些依赖注入框架,如JSF 2或Spring。

#1


1  

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.

对我来说听起来像是UIBean引用过时版本的ConnectionBean的某种问题。这是JSF的一个问题 - 如果重新创建bean,JSF将不会更新所有其他bean中的引用。

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

您可以尝试每次都获取ConnectionBean的“新”副本。以下方法将按名称检索辅助bean:

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!

在不知道代码细节以及如何使用bean的情况下,很难更具体!

#2


1  

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

@Phill Sacre getApplication()。现在不推荐使用createValueBinding。使用此函数代替JSF 1.2。获得豆子的新副本。

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());
}

#3


0  

Define constant and static method in first session bean:

在第一个会话bean中定义常量和静态方法:

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 use in second session bean like this:

比在第二个会话bean中使用这样:

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

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

更好的方法是使用一些依赖注入框架,如JSF 2或Spring。