当前位置: 代码迷 >> 综合 >> jsf中如何在一个后台bean中访问另一个后台bean
  详细解决方案

jsf中如何在一个后台bean中访问另一个后台bean

热度:96   发布时间:2024-01-17 09:43:05.0
/**
 * 根据 faces-confis.xml 中的 Managed Bean
的名称,来获取当前 FacesContext 中的相应 Managed Bean
 *
* @param beanName El表达式 ,例如: #{backingBean}
* @return the managed bean associated with the bean name
 */
public static Object getManagedBean(String beanName) {
                return getValueBinding(beanName).
getValue(FacesContext.getCurrentInstance());

 

}

private static ValueBinding getValueBinding(String el) {
        return getApplication().createValueBinding(el);

 

}

private static Application getApplication() {
        ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
                        .getFactory(FactoryFinder.APPLICATION_FACTORY);
        return appFactory.getApplication();

 

}

通过Application创建一个ValueBinding对象,这个对象能够和资源文件联系起来,通过它的getValue()方法反转?得到一个Java Object实例。我们在引用时再转换成我们需要的类型即可。


FacesContext context = FacesContext.getCurrentInstance();
     ValueBinding vb = context.getApplication().createValueBinding("#{SoCreator.conumber}");
     vb.setValue(context,co);
  相关解决方案