2011-06-24 56 views
0

我們最近從JSF 1.2升級到2.1。我們在WebSphere 6.1上運行,其具有的Servlet 2.4使用Servlet 2.4訪問JSF2.1中的另一個託管Bean

我們使用以下庫: MyFaces的2.1.1 EL-API 2.2

現在我們唯一的問題是,我們不能訪問其他備份豆類就像我們之前做過的:

public static Object getBackingBean(String pName) { 
    ELContext elContext = FacesContext.getCurrentInstance().getELContext(); 
    Object ret = elContext.getELResolver().getValue(elContext, null, pName); 
    return ret; 
} 

這將始終返回null。 我們也試過:

Beanclass bean = (Beanclass) FacesContext.getCurrentInstance().getApplication() 
.getELResolver().getValue(elContext, null, "beanclass"); 

哪些返回null。

我們已經嘗試了@ManagedProperty註釋,但這顯然是一個Servlet 2.5功能。默認情況下,ELContext是否可能使用DI?有沒有辦法在JSF2.1和Servlet 2.4中獲得另一個支持Bean的實例?謝謝!

回答

1

正如你可以在閱讀the MyFaces webpage

JSF 2.1需要Java 1.5或更高版本,JSP 2.1,JSTL 1.2和的Java Servlet 2.5實施。

0

誠然,正式的JSF 2.1需要這樣做,但理論上可以使用JSP 2.0運行MyFaces Core 2.1。如果您可以使用MyFaces Core 1.2.x運行應用程序,那麼您可以使用2.1來執行該應用程序,因爲沒有技術上的原因導致無法運行。嘗試詢問MyFaces Users List。我做了一個檢查,並且有一些用戶在WebSphere 6.1上運行1.2.x應用程序,所以也許你可以在那裏有更好的運氣。

+0

謝謝 - 我已經交叉發佈它在MyFaces用戶列表 – toby

2

我只是想用正確的答案跟進我自己的問題 - 即使Servlet 2.5是必需的,我的任務也可以在沒有它的情況下實現。這裏是如何可靠地得到sessionBean的實例:

BeanClass beanInst = (BeanClass) JSF2Util.findBean("beanClass"); 

public static <T> T findBean(String beanName) { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    Application app = facesContext.getApplication(); 
    ExpressionFactory elFactory = app.getExpressionFactory(); 
    ELContext elContext = facesContext.getELContext(); 
    ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{" + beanName + "}",Object.class); 
    return (T) valueExp.getValue(elContext); 
} 
相關問題