[英]Access another managed Bean in JSF2.1 with Servlet 2.4
我们最近已从JSF 1.2升级到2.1。 我们正在运行具有Servlet 2.4的WebSphere 6.1。
我们正在使用以下库:myfaces 2.1.1 el-api-2.2
现在,我们唯一的问题是我们无法像以前那样访问其他Backing Bean:
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的实例? 谢谢!
我只是想用正确的答案来回答自己的问题-即使需要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);
}
您可以在MyFaces网页上阅读:
JSF 2.1需要Java 1.5或更高版本,JSP 2.1,JSTL 1.2和Java Servlet 2.5实现。
确实,JSF 2.1正式要求这样做,但是从理论上讲,可以在JSP 2.0中运行MyFaces Core 2.1。 如果可以使用MyFaces Core 1.2.x运行应用程序,则很有可能可以使用2.1来运行它,因为没有技术原因无法运行该应用程序。 尝试在MyFaces用户列表中询问。 我进行了检查,发现有一些在WebSphere 6.1上运行1.2.x应用程序的用户,因此也许您可以在其中获得更好的运气。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.