来自复合组件的自定义客户端事件

时间:2019-05-16 15:27:40

标签: jsf-2.2

我正在尝试一种通用逻辑,所有自定义组件,或者更佳的是,自定义组件用户可以用来在组件完成其ajax操作后执行。此操作可能会在服务器上失败,因此必须在所有操作成功完成后执行用户的执行。

因此,我朝着客户行为的方向发展。但是我被困住了,因为提供的代码仅返回提供的响应,并且不执行javascript。

<?xml version='1.0' encoding='UTF-8'?>
<partial-response id="j_id1"><changes><update id="j_id1:javax.faces.ViewState:0"><![CDATA[142571433633527695:1304613666692082786]]></update></changes></partial-response>

这是我现在可以做的:

  1. addClientBehavior被执行
  2. 行动被鞭打
  3. super.queueEvent使用预期的javascript onevent执行
  4. 响应不执行javascript

ComponentBeh.java

@Slf4j
@FacesComponent("ComponentBeh")
public class ComponentBeh extends UINamingContainer implements ClientBehaviorHolder {

    @Override
    public void addClientBehavior(final String eventName, final ClientBehavior behavior) {
        super.addClientBehavior(eventName, behavior);
        log.info("Adding eventName {}", eventName);
    }

    @Override
    public Collection<String> getEventNames() {
        return Collections.singletonList("myEvent");
    }

    public void action() {

        log.info("Action triggered.");

        final List<ClientBehavior> clientBehaviors = getClientBehaviors().get("myEvent");
        if (CollectionUtils.isNotEmpty(clientBehaviors)) {
            final ComponentBehEvent componentBehEvent = new ComponentBehEvent(this, clientBehaviors.get(0));
            super.queueEvent(componentBehEvent);
        }
    }
}

ComponentBehEvent.java

public class ComponentBehEvent extends AbstractAjaxBehaviorEvent {

        public ComponentBehEvent(final UIComponent component,
                                 final Behavior behavior) {
            super(component, behavior);
        }
    }

component-beh.xhtml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--
  ~ Copyright © 2019 by Parsek d.o.o.
  ~ All Rights Reserved.
  -->
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:cc="http://xmlns.jcp.org/jsf/composite"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
>
    <cc:interface componentType="ComponentBeh">
        <cc:clientBehavior name="myEvent" default="true"/>
    </cc:interface>
    <cc:implementation>
        <h:commandButton value="doit">
            <f:ajax listener="#{cc.action}"/>
        </h:commandButton>
    </cc:implementation>
</ui:composition>

component-beh-test.xhtml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!--
  ~ Copyright © 2019 by Parsek d.o.o.
  ~ All Rights Reserved.
  -->
<ui:composition template="/layout/sdk-layout.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:v="http://vitaly.parsek.com/showcase"
                xmlns:f="http://xmlns.jcp.org/jsf/core">

    <ui:define name="content">
        <section class="surface">
            <div class="group">
                <h:form>
                    <h1>Component</h1>
                    <v:component-beh>
                        <f:ajax event="myEvent" onevent="alert('yo')"/>
                    </v:component-beh>
                </h:form>
            </div>
        </section>
    </ui:define>
</ui:composition>

0 个答案:

没有答案