JSF 的組件和組件樹(shù)的概念非常好,可以完全通過(guò)JAVA代碼實(shí)現(xiàn)的組件的生成與設(shè)置方法等操作.尤其是JSF1.2提供了更多的組件的Binding方法.下面就實(shí)際做一個(gè)例子.
首先在managedBean中創(chuàng)建組件
1、創(chuàng)建按鈕
private HtmlCommandButton button = new HtmlCommandButton();
public HtmlCommandButton getButton() {
button.setValue("button"); //設(shè)置按鈕上顯示的值
FacesContext context = FacesContext.getCurrentInstance();
ELContext elContext = context.getELContext();
ExpressionFactory ef = context.getApplication().getExpressionFactory();
Class[] c = new Class[0];
// c[0] = ActionEvent.class; //此處想調(diào)用一個(gè)帶參數(shù)的方法,沒(méi)有成功
MethodExpression me = ef.createMethodExpression(elContext,
"#{testBean.testEl}", Void.TYPE, c);
button.setActionExpression(me); //為鈕加入一個(gè)響應(yīng)的方法,這種方法是JSF1.2方式的
return button;
}
//在運(yùn)行時(shí)被調(diào)用的方式,同前面的方法綁定中的EL表達(dá)是一致的.
public void testEl() {
System.out.println("testEl method is run no param");
}
在JSP頁(yè)面中加入按鈕.(testBean是在FacesConfig.xml中聲明的上的bean)
<h:commandButton binding="#{testBean.button }"/>
2、創(chuàng)建表格
private HtmlPanelGrid grid = new HtmlPanelGrid();
public HtmlPanelGrid getGrid() {
List list = grid.getChildren();//取得表格的列表
list.add(getHtmlInputText());//向表格列表加入組件
System.out.println("grid column " + grid.getColumns());
return grid;
}
public HtmlInputText getHtmlInputText() {
htmlInputText.setValue("abcc");//這里直接設(shè)置值了,可以通過(guò)expressionFactory.createValueExpression(elContext, "#{testBean.userid}", String.class);來(lái)設(shè)置值表達(dá)式
htmlInputText.setAlt("alt htmlInputText ");
htmlInputText.setConverter(new TestConve());//為輸入框設(shè)置轉(zhuǎn)換器
return htmlInputText;
}
在JSP中加入下面代碼:
<h:panelGrid binding="#{testBean.grid }"/>
就可以在表格內(nèi)顯示一個(gè)輸入框了。
Technorati : jsf, 動(dòng)態(tài)組件
posted on 2008-07-01 21:39
Libo 閱讀(1442)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
JavaServer Faces