抓住JSF 2的主要code在AjaxBehaviorEventJSF、code、AjaxBehaviorEvent

2023-09-10 13:51:36 作者:尐熊ル

我有一个支持bean链接到事件听者一个JSF AJAX keyup事件。

I have a JSF ajax keyup event linked to an event listner in a backing bean.

在code在JSF文件是像下面。

The code in the JSF file is like below.

<h:inputText id="txtDescription" value="#{institutionController.current.description}" disabled="#{institutionController.modifyControlDisable}" >
    <f:ajax event="keyup" listener="#{institutionController.changeDetailsEvent}" />
</h:inputText>

在code。在支持Bean是像下面。

The code in the backing bean is like below.

public void changeDetailsEvent(AjaxBehaviorEvent event) {
}

我想实现不同的逻辑取决于关键presses,如所示低于伪code。

I want to achieve different logic depending on the key presses, like shown is pseudocode below.

public void changeDetailsEvent(AjaxBehaviorEvent event) {
    If (event.key = Key.enter) {
        do something;
    } else if (event.key = Key.Escape) {
        so something else;
    } else {
        do nothing;
    }

}

有人能告诉我这是怎么了支持Bean做了什么?

Can someone please tell me how this is done in the backing bean?

推荐答案

AjaxBehaviorEvent 不包含有关JavaScript的的任何信息事件对象。你需要自己一起传递希望的信息。这可以通过其值是通过JavaScript的pfilled $ P $一个隐藏的输入字段来实现。例如,

The AjaxBehaviorEvent doesn't contain any information about the JavaScript event object. You need to pass the desired information along yourself. This can be achieved by a hidden input field whose value is to be prefilled by JavaScript. For example,

<h:inputText value="#{bean.input}" onkeyup="document.getElementById('#{keyCode.clientId}').value=event.keyCode">
    <f:ajax event="keyup" execute="@this keyCode" listener="#{bean.listener}" />
</h:inputText>
<h:inputHidden id="keyCode" binding="#{keyCode}" value="#{bean.keyCode}" />

(请注意, ID 隐藏字段都包含在执行,使其得到沿着Ajax请求提交,也请注意,结合用于能够动态地获取生成的客户端ID在的document.getElementById() ,以设置键code值,你可以c中的客户端ID交替也很难$ C $,如果它是固定的)的

(please note that the id of the hidden field is included in execute so that it get submitted along on the ajax request, please also note that the binding is used to be able to dynamically obtain the generated client ID in document.getElementById() in order to set the key code value, you could alternatively also hardcode the client ID if it's fixed)

private String input;
private int keyCode;

public void listener() {
    switch (keyCode) {
        case 13:
            // Enter key was pressed.
            break;
        case 27:
            // Escape key was pressed.
            break;
        default:
            // Other key was pressed.
            break;
    }
}

您可以找到所有有效的概述关键code 中的值的 Mozilla的DOM引用。

You can find an overview of all valid keyCode values in the Mozilla DOM reference.