Java等效于SSJS中的getComponent()

Nav*_*een 0 java xpages

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

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
...
...
Run Code Online (Sandbox Code Playgroud)

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

Pan*_*amo 5

通过在Java中评估SSJS可能是最简单的.来自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);
Run Code Online (Sandbox Code Playgroud)

如何从Java Bean调用ad hoc SSJS

这是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; 
} 
Run Code Online (Sandbox Code Playgroud)

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

  • 在Karsten的代码和SSJS中的*getCompenent*方法之间存在不同的行为:虽然Karsten的代码从顶部组件搜索组件,但SSJS中的'getComponent*方法是否从当前组件向上执行搜索.这可能会导致不同的结果.我不确定如果在方面中使用该方法会发生什么. (2认同)