Jsf 在呈现自己的数据表时创建的HtmlCommandButton不起作用ActionListener或Action
我创建了一个dataTable组件,并希望在标题中添加一个隐藏列的按钮,但该按钮不会触发事件 这是报头的编码方法。我在函数呈现标题中,当标题必须为隐藏自己编写按钮时,我调用这个标题Jsf 在呈现自己的数据表时创建的HtmlCommandButton不起作用ActionListener或Action,jsf,jsf-2.2,Jsf,Jsf 2.2,我创建了一个dataTable组件,并希望在标题中添加一个隐藏列的按钮,但该按钮不会触发事件 这是报头的编码方法。我在函数呈现标题中,当标题必须为隐藏自己编写按钮时,我调用这个标题 private void encodeHideColumnOption(FacesContext context, UIComponent table, ResponseWriter writer) throws IOException { HtmlCommandBut
private void encodeHideColumnOption(FacesContext context,
UIComponent table, ResponseWriter writer) throws IOException {
HtmlCommandButton hide = (HtmlCommandButton) context
.getApplication()
.createComponent(HtmlCommandButton.COMPONENT_TYPE);
hide.setId("hb_" + model.getColumnIndex());
hide.setValue("X");
AjaxBehavior ajax = new AjaxBehavior();
ajax.setRender(Arrays.asList(":" + table.getParent().getClientId(context),
":messagePanel"));
ajax.setExecute(Arrays.asList(":form"));
hide.addClientBehavior(hide.getDefaultEventName(), ajax);
hide.addActionListener(new ActionListenerHideOption());
table.getChildren().add(hide);
hide.encodeAll(context);
}
这是ActionListener类
@SessionScoped
public class ActionListenerHideOption implements ActionListener, Serializable {
public ActionListenerHideOption(){
}
@Override
public void processAction(ActionEvent event)
throws AbortProcessingException {
System.out.println("Action Listener Fired :D");
}
}
但是当我点击按钮时,什么也没发生,我用这个问题来验证:但是不起作用。你能帮我吗
--编辑--
我正在尝试不同的方法,但我真的不明白为什么我用这种方法,而且有效
<h:commandButton id="hb_x" value="∅">
<f:actionListener type="package.ActionListenerHideOption" />
<f:ajax render=":form:dataTable" onevent="tableReset" />
</h:commandButton>
但另一种方式不。。。有人能帮我吗?这是解决办法。在呈现dataTable组件my dataTable组件时,我必须添加此函数来解码组件dataTable
@Override
public void decode(FacesContext context, UIComponent component) {
for (UIComponent child : component.getChildren()) {
for (UIComponent grandChild : child.getChildren()) {
if (grandChild instanceof HtmlCommandButton) {
grandChild.decode(context);
}
}
}
}
这允许在我的数据表中的所有HtmlCommand按钮中启动解码进程。。。我认为它可以与HtmlCommandLink一起使用,但还没有经过测试。如果任何人都能在性能和其他方面做得更好,则可能是更好的代码,欢迎:
--编辑--
我做了一个更好的解决方案,它解码我的数据表上的所有内容
@Override
public void decode(FacesContext context, UIComponent component) {
decodeChildren(context, component);
}
public void decodeChildren(FacesContext context, UIComponent component) {
for (UIComponent child : component.getChildren()) {
if (!child.getChildren().isEmpty()) {
decodeChildren(context, child);
}else{
child.decode(context);
}
}
}
就这样=