0

让我们考虑这个简单的代码:

<h:form id="myForm">
    <h:inputText id="myInput">
        <a4j:support event="onchange" actionListener="#{myBean.doSomething}"/>
    </h:inputText>
</h:form>

这将生成以下 HTML 代码:

<input id="myForm:myInput" type="text" name="myForm:myInput" onchange="A4J.AJAX.Submit(...)" />

现在,我只是在我的情况下添加一些onchange东西<h:inputText>

<h:form id="myForm">
    <h:inputText id="myInput" onchange="alert('foobar');">
        <a4j:support event="onchange" actionListener="#{myBean.doSomething}"/>
    </h:inputText>
</h:form>

这将生成以下 HTML 代码:

<input id="myForm:myInput" type="text" name="myForm:myInput" onchange="alert('foobar');" />

如您所见,不再添加Ajax 代码。就我而言,这是一种非常奇怪的行为。如果事件已在输入字段中定义,为什么<a4j:support>不附加 Ajax 调用?

所以我的问题是如何进行已经定义的<a4j:support>工作?当然,解决方案必须同时运行中定义的 Javascript 代码Ajax 调用。eventinputonchange

换句话说,我想要以下 HTML:

<input id="myForm:myInput" type="text" name="myForm:myInput" onchange="alert('foobar'); A4J.AJAX.Submit(...)" />

我正在使用 Richfaces 3.3.2 和 JSF 1.2


编辑

当然,我可以在 的属性中移动onchangeJavascript 代码,执行以下操作:onsubmit<a4j:support>

<h:inputText id="myInput">
    <a4j:support onsubmit="alert('foobar');" event="onchange" actionListener="#{myBean.doSomething}"/>
</h:inputText>

但这是唯一的方法吗??

4

3 回答 3

1

如果没有明确记录这种行为,我会认为这是 Ajax4jsf 中的一个错误。向 JBoss.org 那边的 Ajax4jsf/RichFaces 人员报告。我以前见过这样的问题<a4j:commandButton onclick="someFunction()">

于 2010-10-06T15:18:37.327 回答
1

我之前已经遇到过这个问题,我发现 RichFaces 3.3.x 只会执行使用 a4j:support 定义的 onchange 事件的代码,而会忽略使用 JSF 组件定义的 onchange 代码。

在我的情况下,解决方法很简单,对于我的情况,使用其他事件而不是“onchange”是有效的(不确定它是 onclick 还是 onselect),所以我将我的代码附加到这个其他事件并且我的代码有效,但我'不确定这是否适合你。如果你真的需要两个元素的 onchange 事件,你必须按照 BalusC 所说的去做,并将它报告给 RichFaces 人。

于 2010-10-06T17:49:34.967 回答
0

更多关于上下文

事实上,我不能真正修改 的event<a4j:support>,就像Abel Morelos建议的那样,因为我想做的是添加一个自定义组件来执行一些客户端验证。这个验证调用了一个 Javascript 函数,所以我的自定义组件修改了onchange他的父级的值。

我的 JSF 代码将如下所示:

<h:inputText id="myInput">
    <my:validation .../>
    <a4j:support event="onchange" actionListener="#{myBean.doSomething}"/>
</h:inputText>

这段代码与下面的代码相当,除了onchange<h:inputText>由我的组件自动添加的:

<h:inputText id="myInput" onchange="if (!checkSomeValidation()) { return false; }">
    <a4j:support event="onchange" actionListener="#{myBean.doSomething}"/>
</h:inputText>

所以大家可以理解,我的自定义组件直接修改了 的onchange事件<h:inputText>,并且由于 的​​问题<a4j:support>,最终Ajax调用没有绑定到输入组件。


解决方案

当链接到一个 JSF 组件时,它<a4j:support>会将自己“转换”为一个 facet,其名称为,名称org.ajax4jsf.ajax.SUPPORTxxx在哪里。所以在我的情况下,将有一个名为.xxxevent<h:inputText>org.ajax4jsf.ajax.SUPPORTonchange

因此,我在自定义组件的 Java 代码中所做的是检查父级(<h:inputText>此处)是否具有这样的方面。

如果no,则表示父级没有<a4j:support event="onchange"/>链接到它。所以在这种情况下,我修改了我的onchange属性<h:inputText/>

如果,则意味着有一个<a4j:support event="onchange"/>链接到父级。因此,我使用以下代码修改了方面本身:

HtmlAjaxSupport facet = (HtmlAjaxSupport) getParent().getFacet("org.ajax4jsf.ajax.SUPPORTonchange");
if (facet != null) {
    facet.setOnsubmit("my Javascript code goes here");
} else {
    getParent().setOnchange("my Javascript goes here");
}
于 2010-10-07T07:37:00.117 回答