簡體   English   中英

JSF:如何在另一個會話bean的基礎上更新一個會話bean?

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

我目前正在修改一些jsf應用程序。 我有兩個豆。

  • connectionBean
  • UIBean

當我第一次在connectionBean中設置連接參數時,UIBean能夠讀取我的connectionBean信息並顯示正確的UI樹。

但是,當我嘗試在同一會話中設置連接參數時。 我的UIBean仍將使用以前的connectionBean信息。

它只會在我使整個httpSession無效后使用。

無論如何,我可以使一個會話Bean更新另一個會話Bean嗎?

在我看來,UIBean引用了過時的ConnectionBean版本是一種問題。 這是JSF的一個問題-如果您重新創建一個bean,JSF將不會更新所有其他bean中的引用。

您可以嘗試每次獲取ConnectionBean的“新”副本。 以下方法將按名稱檢索支持bean:

protected Object getBackingBean( String name )
{
    FacesContext context = FacesContext.getCurrentInstance();

    return context
            .getApplication().createValueBinding( String.format( "#{%s}", name ) ).getValue( context );
}

在不了解代碼細節以及如何使用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());
}

在第一個會話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);
}  
...

而不是像這樣在第二個會話Bean中使用:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM