JSF:将AjaxBehaviour添加到动态生成的HTMLInputText中

时间:2015-03-27 12:08:40

标签: ajax jsf listener

我想动态创建带有AjaxBehaviour的UIComponents并将其添加到HTMLPanelGrid。将调用ajax方法,但之前没有值绑定,之后没有渲染。请帮我!非常感谢!这是一些代码:

Ajax功能:

public void ajax(AjaxBehaviorEvent event) {
    HtmlPanelGrid grid = (HtmlPanelGrid) event.getComponent().getParent();
    HashMap<String, Object> params;

    try {
        ajaxSet(grid);
        params = collect();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    SearchComponent search = (SearchComponent) ComponentRegistry.instance()
            .getComponent(IComponentConstants.SEARCH);
    this.score = Integer.toString(search.getSystemsCount(params));
}

创建HTMLInputText的功能:

public HtmlInputText inputText(String id, String expression,
        UIComponent parent, AjaxBehaviorListener listener) {
    HtmlInputText text = new HtmlInputText();
    text.setId(id);
    if (!expression.isEmpty()) {
        text.setValueExpression("value", createValueExpression(expression));
    }

    AjaxBehavior ajax = (AjaxBehavior) FacesContext.getCurrentInstance()
            .getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
    ajax.setExecute(Arrays.asList(new String[] { "@form" }));
    ajax.setRender(Arrays.asList(new String[] { "@all" }));
    ajax.addAjaxBehaviorListener(listener);
    text.addClientBehavior("valueChange", ajax);

    add(text, parent);

    return text;
}

根据具有所需字段信息和侦听器的列表创建字段的功能:

public void createSearchFields(UIComponent parent) {
    String beanname = Utilities.lookupManagedBeanName(this);
    GUIHelper helper = GUIHelper.instance();

    for (Searchfield searchfield : shown) {
        helper.label(searchfield.id, searchfield.displayname, parent);
        for (Field f : searchdto.getClass().getDeclaredFields()) {
            GUISearchField gsf = f.getAnnotation(GUISearchField.class);
            if (gsf == null)
                continue;
            if (gsf.id().equals(searchfield.id)) {
                String expression = "#{" + beanname + ".searchdto."
                        + f.getName() + "}";
                helper.inputText(searchfield.id, expression, parent, this);
            }
        }
    }
}

@Override
public void processAjaxBehavior(AjaxBehaviorEvent event)
        throws AbortProcessingException {
    ajax(event);
}

public abstract void ajax(AjaxBehaviorEvent event);

xhtml部分:

<h:form id="searchform" styleClass="ibm-column-form ibm-styled-form">
                <h:panelGrid columns="2" binding="#{systemsearchable.searchfields}">
                </h:panelGrid>
                <h:commandButton value="Search" action="#{systemsearchable.search}">
                    <f:ajax execute="@form" render="results"></f:ajax>
                </h:commandButton>
            </h:form>

如果您需要更多信息,请告诉我们:)

0 个答案:

没有答案