带有<f:ajax>的JSF2.0仅工作一次

ang*_*rds 5 jsf jsf-2

我在JSF2.0中的标记有问题,希望有人指出我做错了什么。这是用户界面中的内容:

<h:panelGroup>
  <h:form id="theForm">
    <h:selectOneMenu id="theMenu" value="#{viewBean.selectedItem}">
        <f:ajax event="change" render="selectedItemText"/>
    <f:selectItem itemLabel=""/>
    <f:selectItems value="#{viewBean.selectableItems}"/>
    </h:selectOneMenu>
    <h:outputText id="selectedItemText" value="#{viewBean.selectedItemText}" />
  </h:form>
</h:panelGroup>
Run Code Online (Sandbox Code Playgroud)

这很好用-我的对话范围内的支持bean有一个方法setSelectedItem,它被调用,并且在我第一次从菜单中选择另一个项目时就可以完成它;输出文本在前端更新,很高兴。但是,对菜单选择的进一步更改不会通过ajax触发对设置程序的调用。我也在f:ajax标签上的侦听器上进行了尝试-侦听器方法也仅在第一次调用(代码中的断点才能弄清楚)。

我做错了什么吗?

小智 5

我有一个类似的问题。

我下面的第二个commandButton在具有视图参数的JSF视图中仅可以使用一次。添加<f:param>到我的第一个commandButton可以解决此问题。BalusC的非常有用的讨论没有涵盖这种情况。

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

<f:view>
    <f:metadata>
        <f:viewParam name="id" value="#{fooManager.millis}" required="true"/>
    </f:metadata>

    <h:head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    </h:head>

    <h:body>
        <h:form id="fooForm">
            <h:commandButton
                    id="barBbutton"
                    value="foo:test"
                    action="#{fooManager.test}">
                <f:param name="id" value="1"/>
                <f:ajax render="fooMillis1"/>
            </h:commandButton>

            <p>
                <h:outputText id="fooMillis1" value="foo:display1: #{fooManager.millis}"/>
            </p>
            <p>
                <h:outputText id="fooMillis2" value="foo:display2: #{fooManager.millis}"/>
            </p>
        </h:form>
        <h:form id="barForm">
            <h:commandButton
                    id="barButton"
                    value="bar:test"
                    action="#{barManager.test}">
                <f:ajax render="barMillis1"/>
            </h:commandButton>

            <p>
                <h:outputText id="barMillis1" value="bar:display1: #{barManager.millis}"/>
            </p>
            <p>
                <h:outputText id="barMillis2" value="bar:display2: #{barManager.millis}"/>
            </p>
        </h:form>
    </h:body>
</f:view>
</html>
Run Code Online (Sandbox Code Playgroud)

而且我的FooManager和BarManager看起来相同:

@ManagedBean
@ViewScoped
public class FooManager {

    public long getMillis() {
        return millis;
    }

    public void setMillis(long millis) {
        this.millis = millis;
    }

    public void test() {
        setMillis(System.currentTimeMillis());
    }
    private long millis;

}
Run Code Online (Sandbox Code Playgroud)

当它不起作用时,我的Weblogic / Mojarra库没有给出任何有用的提示。任何地方都没有错误。只是经过无数次尝试,我才想到了一个工作按钮,就像上面的第一个按钮一样。

  • http://stackoverflow.com/a/2120183的第3点对此进行了介绍。您在&lt;f:viewParam&gt;上没有`&lt;h:message(s)&gt;`,因此从未收到有关'required =“ true”`错误的通知。但是,这应该已经在服务器日志中记录了警告行,或者如果将javax.faces.PROJECT_STAGE设置为Development,则至少已经呈现了开发阶段消息列表。顺便说一句,当您的bean处于视图作用域内时,在&lt;f:viewParam required =“ true”&gt;上的&lt;f:param&gt;在技术上没有多大意义。您可能想看看OmniFaces`&lt;o:viewParam&gt;`http://showcase.omnifaces.org/components/viewParam (3认同)
  • 遇到同样的问题,并且在我的&lt;f:viewParam&gt;上出现&lt;h:messages&gt;时,会通知我“ required”错误并为我节省了无数的时间...终于使用```required =“#{!facesContext.postback}”```/sf/ask/3076911141/Called-only-1st-time/46678427#46678427修复了它 (2认同)

小智 1

我有一个类似的问题,在我的例子中,除了 IE9 中 ajax 只被触发一次之外,所有浏览器都工作正常。

我正在使用render="@form",当我将其更改为 时render="@all",效果很好。我不知道为什么,因为我在该页面中只有一个表单,而我的所有组件都是该表单。

所以我建议你检查renderexecute标签,至少在我的情况下这是解决方案