简体   繁体   English

JSF:如何在另一个会话bean的基础上更新一个会话bean?

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

I am currently modifying some jsf application. 我目前正在修改一些jsf应用程序。 I have two beans. 我有两个豆。

  • connectionBean connectionBean
  • UIBean 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吗?

Sounds to me like it's some kind of problem with UIBean referencing an out-of-date version of ConnectionBean. 在我看来,UIBean引用了过时的ConnectionBean版本是一种问题。 This is one problem with JSF - if you re-create a bean, JSF will not update the references in all your other beans. 这是JSF的一个问题-如果您重新创建一个bean,JSF将不会更新所有其他bean中的引用。

You could try getting a 'fresh' copy of the ConnectionBean each time. 您可以尝试每次获取ConnectionBean的“新”副本。 The following method will retrieve a backing bean by name: 以下方法将按名称检索支持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的情况下,很难变得更加具体!

@Phill Sacre getApplication().createValueBinding is now deprecated. @Phill Sacre getApplication()。createValueBinding现在已弃用。 Use this function instead for JSF 1.2. 对于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());
}

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM