JSF 动态创建组件

JSF 的组件和组件树的概念非常好,可以完全通过JAVA代码实现的组件的生成与设置方法等操作.尤其是JSF1.2提供了更多的组件的Binding方法.下面就实际做一个例子.

首先在managedBean中创建组件

1、创建按钮

private HtmlCommandButton button = new HtmlCommandButton();

public HtmlCommandButton getButton() {
button.setValue("button"); //设置按钮上显示的值
FacesContext context = FacesContext.getCurrentInstance();
ELContext elContext = context.getELContext();
ExpressionFactory ef = context.getApplication().getExpressionFactory();
Class[] c = new Class[0];
// c[0] = ActionEvent.class; //此处想调用一个带参数的方法,没有成功
MethodExpression me = ef.createMethodExpression(elContext,
"#{testBean.testEl}", Void.TYPE, c);
button.setActionExpression(me); //为钮加入一个响应的方法,这种方法是JSF1.2方式的

return button;
}

//在运行时被调用的方式,同前面的方法绑定中的EL表达是一致的.

public void testEl() {
System.out.println("testEl method is run no param");
}

在JSP页面中加入按钮.(testBean是在FacesConfig.xml中声明的上的bean)

<h:commandButton binding="#{testBean.button }"/>

2、创建表格
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");//这里直接设置值了,可以通过expressionFactory.createValueExpression(elContext, "#{testBean.userid}", String.class);来设置值表达式

htmlInputText.setAlt("alt htmlInputText ");
htmlInputText.setConverter(new TestConve());//为输入框设置转换器
return htmlInputText;
}

在JSP中加入下面代码:

<h:panelGrid binding="#{testBean.grid }"/>

就可以在表格内显示一个输入框了。


Technorati : jsf, 动态组件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值