2013-04-01 14 views
0

下面是如何以編程方式創建一個<h:commandButton>編程創建H:的commandButton沒有的onclick = 「processInput(ID)」 援引

private HtmlCommandButton generateButton(String id,String label){ 
    HtmlCommandButton button = (HtmlCommandButton)app.createComponent 
     (HtmlCommandButton.COMPONENT_TYPE); 
    button.setId(id); 
    button.setValue(label); 
    button.setOnclick("processInput(id)"); 
    return button; 
} 

當單擊該按鈕時,我需要執行processInput(id)功能。但是,當我點擊該按鈕時,什麼也沒有發生。

+0

所以後來.....? – Freak

+0

@freak問題是當我點擊按鈕什麼都沒有發生 – Bucks

+0

所以請在你的問題中寫下這個陳述,以便其他人可以清楚地理解這個問題。 – Freak

回答

1

Onclick函數會將processInput添加爲JavaScript函數。你需要通過ID在你的Java方法一樣,

setOnClick("processInput(" + id + ")"); 

要添加您需要使用setActionExpression操作方法,

HtmlCommandButton button = new HtmlCommandButton(); 
FacesContext fc = FacesContext.getCurrentInstance(); 
ELContext ctx = fc.getELContext(); 
String expression = "#{processInput('id')}"; 
Class[] parameterTypes = new Class[1]; 
parameterTypes[0]=String.class; 
MethodExpression me = fc.getApplication().getExpressionFactory(). 
         createMethodExpression(ctx, 
         expression, returnType, parameterTypes); 
button.setActionExpression(me); 
button.setOnclick("alert('');");