[英]JSF : How to update one session bean base on another session bean?
我目前正在修改一些jsf應用程序。 我有兩個豆。
當我第一次在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.