JSF2.0 与只能工作一次

发布于 2024-10-17 13:04:58 字数 759 浏览 1 评论 0原文

我在 JSF2.0 中的标签有问题,我希望有人能指出我做错了什么。这是我在 UI 中得到的内容:

<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>

这非常有效 - 我的对话范围支持 bean 有一个方法 setSelectedItem,当我第一次从其中选择不同的项目时,它会被调用并执行其操作菜单;输出文本在前端更新,我很高兴。但是,对菜单选择的进一步更改不会触发通过 ajax 对设置器的调用。我还在 f:ajax 标记上尝试过使用侦听器 - 侦听器方法也仅在第一次被调用(代码中的断点来解决这个问题)。

我做错了什么吗?

I'm having a problem with the tag in JSF2.0 and I hope someone can point out what I'm doing wrong. Here's what I've got in the UI:

<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>

This is working great - my conversation-scoped backing bean has a method setSelectedItem, and it's called and it does its thing the first time I select a different item from the menu; the output text is updated in the frontend, and I'm happy. However, further changes to the menu selection do not trigger a call to the setter via ajax. I've also tried this with a listener on the f:ajax tag - the listener method is only called that first time as well (breakpoints in the code to figure this out).

Am I doing something incorrectly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谜兔 2024-10-24 13:04:59

我有同样的问题。
对于下面的代码,ajax 仅运行一次。

<h:inputText id="element_id" value="#{viewBean.someValue}"></h:inputText>   
<h:commandLink action="#{viewBean.someAction}" value="click me">
     <f:ajax render=":my_form:another_element" execute="element_id> </f:ajax>
</h:commandLink>

当我将正在执行的元素添加到渲染属性时,每次都会触发ajax。

<h:inputText id="element_id" value="#{viewBean.someValue}"></h:inputText>   
<h:commandLink action="#{viewBean.someAction}" value="click me">
     <f:ajax render=":my_form:another_element element_id" execute="element_id> </f:ajax>
</h:commandLink>

I had the same issue.
For the code below ajax was run only once.

<h:inputText id="element_id" value="#{viewBean.someValue}"></h:inputText>   
<h:commandLink action="#{viewBean.someAction}" value="click me">
     <f:ajax render=":my_form:another_element" execute="element_id> </f:ajax>
</h:commandLink>

When I add to render attribute the element which I'm executing then the ajax is triggered every time.

<h:inputText id="element_id" value="#{viewBean.someValue}"></h:inputText>   
<h:commandLink action="#{viewBean.someAction}" value="click me">
     <f:ajax render=":my_form:another_element element_id" execute="element_id> </f:ajax>
</h:commandLink>
携君以终年 2024-10-24 13:04:59

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

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

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

I had a similar problem, in my case everithing worked fine in all browsers except that in IE9 the ajax was fired only once.

I was using render="@form" and when I changed it to render="@all", it worked fine. I dunno why, since I only have one Form in that page, and all my components are in that form.

So I would advise you to check the render and execute tags, at least in my case it was the solution

北渚 2024-10-24 13:04:59

我有同样的错误,使用 Primeface's p:ajax 而不是 f:ajax 修复了它。

I had the same bug, fixed it using Primeface's p:ajax instead of f:ajax.

我的鱼塘能养鲲 2024-10-24 13:04:58

我有类似的问题。

下面的第二个 commandButton 仅在下面具有视图参数的 JSF 视图中工作一次。将 添加到我的第一个 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>

我的 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;

}

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

I had a similar problem.

My second commandButton below only works once in the JSF view below that has a view param. Adding <f:param> to my first commandButton solved the problem. This is a situation not covered by BalusC's very helpful discussion.

<!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>

And my FooManager and BarManager look the same:

@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;

}

When it is not working, my Weblogic/Mojarra library does not give any helpful hint. There is no error anywhere. It was only after numerous tries that I came up with a working button like the first one above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文