Java 相当于 SSJS 中的 getComponent()

标签 java xpages

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

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

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

最佳答案

通过 Java 评估 SSJS 可能是最简单的。来自斯文的代码:

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

关于Java 相当于 SSJS 中的 getComponent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14526915/

相关文章:

xpages - 在本地计算机上运行 XPage 扩展库

xpages - 覆盖通过 xPages URL 打开的附件的内容处置类型

xpages - 如何使 Xpages 中的部分看起来像这样

java - 从循环线程返回多个值而不用Java杀死它

java - 为非默认模块选择本地开发服务器上的特定端口

java - Sco Unix Grep 特定号码(ID)

javascript - 如何使 xPages 名称选择器显示公司目录或扩展目录目录名称?

java - "AWT-EventQueue-0"中的异常

java - 启动 Minecraft 时出现 "Picked up _JAVA_OPTIONS: -Xmx512M"错误

http - 使 Domino HTTP session 无效/删除?