Java等效于SSJS中的getComponent()

时间:2013-01-25 17:13:44

标签: java xpages

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

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

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

2 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分:2)

在扩展库的Group扩展中,有一个包含XspQuery类和一些过滤器的查询包。这个类的目的是像dojo.query一样运行,为您提供许多不同的方法来查找组件,不仅仅是id,还包括组件类,客户端ID,服务器ID等。这是一个使用服务器ID的示例找到一个组件:

XspQuery query = new XspQuery();
query.byId("someId");
List<UIComponent> componentList = query.locate();

您可以在此处找到它:https://svn-166.openntf.org/svn/xpages/extlib/eclipse/plugins/com.ibm.xsp.extlib.group/src/com/ibm/xsp/extlib/query/

组扩展从未与扩展库一起分发,但是在svn存储库中,为了获得它,您必须通过OpenNTF svn服务器。