Java equivalent of getComponent() in SSJS

2019-09-05 01:45发布

I know that we can access the XPages global objects like this in Java

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

But I am unable to find any equivalent for using getComponent() is Java. Is there any class or method in Java which is similar to getComponent() in SSJS?

标签: java xpages
2条回答
放我归山
2楼-- · 2019-09-05 01:57

In the Group extension of the Extension Library there is a query package that contains the XspQuery class and some filters. This class was intended to function like dojo.query to give you many different ways to find a component, not just by the id but also things like Component Class, Client ID, Server ID, etc. Here's an example of using the server ID to locate a component:

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

You can find it here: https://svn-166.openntf.org/svn/xpages/extlib/eclipse/plugins/com.ibm.xsp.extlib.group/src/com/ibm/xsp/extlib/query/

The Group extension was never distributed with the extension library, but was in the svn repository and to get it you had to go through the OpenNTF svn server.

查看更多
叼着烟拽天下
3楼-- · 2019-09-05 01:58

It might be easiest by evaluating SSJS in Java. Code from 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

Here is pure Java solution by Karsten Lehmann:

/** 
 * 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

查看更多
登录 后发表回答