SSJS中getComponent()的Java等价物

Java equivalent of getComponent() in SSJS(SSJS中getComponent()的Java等价物)

本文介绍了SSJS中getComponent()的Java等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以在Java中像这样访问XPages全局对象

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
...
...

但我找不到任何使用getComponent()is Java的等价物。Java中是否有类似于SSJS中的getComponent()的类或方法?

推荐答案

在Java语言中评估ssss可能是最简单的。来自Sven的代码:

String valueExpr = "#{javascript: getComponent('xxx').getValue();}";
FacesContext fc = FacesContext.getCurrentInstance();
ExpressionEvaluatorImpl evaluator = new ExpressionEvaluatorImpl( fc );
ValueBinding vb = evaluator.createValueBinding( fc.getViewRoot(), valueExpr, null, null);
vreslt = (String) vb.getValue(fc);

How to call ad hoc SSJS from a Java Bean

这里是Karsten Lehmann的纯Java解决方案:

/** 
 * Finds an UIComponent by its component identifier in the current 
 * component tree. 
 * 
 * @param compId the component identifier to search for 
 * @return found UIComponent or null 
 * 
 * @throws NullPointerException if <code>compId</code> is null 
 */ 
public static UIComponent findComponent(String compId) { 
    return findComponent(FacesContext.getCurrentInstance().getViewRoot(), compId); 
} 

/** 
 * Finds an UIComponent by its component identifier in the component tree 
 * below the specified <code>topComponent</code> top component. 
 * 
 * @param topComponent first component to be checked 
 * @param compId the component identifier to search for 
 * @return found UIComponent or null 
 * 
 * @throws NullPointerException if <code>compId</code> is null 
 */ 
public static UIComponent findComponent(UIComponent topComponent, String compId) { 
    if (compId==null) 
        throw new NullPointerException("Component identifier cannot be null"); 

    if (compId.equals(topComponent.getId())) 
        return topComponent; 

    if (topComponent.getChildCount()>0) { 
        List childComponents=topComponent.getChildren(); 

        for (UIComponent currChildComponent : childComponents) { 
            UIComponent foundComponent=findComponent(currChildComponent, compId); 
            if (foundComponent!=null) 
                return foundComponent; 
        } 
    } 
    return null; 
} 

http://www.mindoo.com/web/blog.nsf/dx/18.07.2009191738KLENAL.htm

这篇关于SSJS中getComponent()的Java等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:SSJS中getComponent()的Java等价物

基础教程推荐