本文介紹了SSJS中g(shù)etComponent()的Java等價物的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我知道我們可以在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
這篇關(guān)于SSJS中g(shù)etComponent()的Java等價物的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,