【问题标题】:p:commandButton refreshes entire page instead of partially whereas f:ajax works finep:commandButton 刷新整个页面而不是部分刷新,而 f:ajax 工作正常
【发布时间】:2016-07-29 16:13:59
【问题描述】:

我尝试在单击按钮时更新页面的一部分。 现在,我有以下内容:

template.xhtml

    <h:form prependId="false">

        <h:commandButton value="NEWS" action="news">
            <f:ajax render="newsContent"></f:ajax>
        </h:commandButton>

        <h:panelGroup layout="block" id="newsContent">
            <ui:insert name="newsContent">
                <ui:include src="/WEB-INF/partials/news/news.xhtml"/>
            </ui:insert>
        </h:panelGroup>

    </h:form>

/WEB-INF/partials/news/news.xhtml

<h:commandLink action="newsdetails">
    <f:ajax render="newsContent" />
</h:commandLlink>

newsdetails.xhtml

 <h:commandButton value="INDEX" action="index">
     <f:ajax render="newsContent" />
 </h:commandButton>

现在它工作正常,但如果我用类似的东西替换 &lt;h:commandbutton&gt;

<p:commandButton value="INDEX" action="index" update="newsContent"/>

然后内容被更新但页面被刷新。有什么想法我在这里做错了吗?

【问题讨论】:

  • 页面刷新是什么意思?重新加载?还是你放宽了范围?
  • 停止使用prependId="false",永远不要使用它,它是 JSF 1.2 的一个不经思考的遗留物。

标签: jsf jsf-2 primefaces commandbutton ajax-upload


【解决方案1】:

我终于解决了。

我在 Button 上使用了 actionListener。

<p:commandLink actionListener="#{news.setCurrent(n)}" update="newsContent" />

现在从 Bean 中读取包含的源代码:

<h:panelGroup layout="block" id="newsContent">
    <ui:insert name="newsContent">
        <ui:include src="#{news.page}"/>
    </ui:insert>
</h:panelGroup>

页面的 getter 类似于:

public String getPage() {
    if(current == null){
        return "/WEB-INF/partials/news/news.xhtml";
    }
    return "/WEB-INF/partials/news/details.xhtml";
}

在页面加载时 currentObject 仍然为空,因此会显示 news.xhtml。单击后,当前设置为对象,页面更新为详细信息页面。无需重新加载页面。

【讨论】:

    【解决方案2】:

    您没有调用任何有效的 ajax 操作:

    删除action="index"以避免重新加载索引页面。

    【讨论】:

    • 如果这是答案,那还是很奇怪...一个有效而另一个无效...
    • 删除按钮上的 action="index" 只会导致按钮什么都不做。 index 是包含内容的 xhtml 文件,应该更新。
    • 默认情况下,使用 p:commandButton 您正在执行 ajax 请求。无需为此采取任何措施。您已选择更新新闻内容。如果没有任何反应,您应该检查是否需要使用 actionListener 调用一些代码来准备您想要查看的更改。
    • Ajax 请求不需要任何页面参数,因为它不会加载新页面,它只会刷新您在更新参数“newsContent”中选择的指定部分内容。
    • @Kukeltje 是的,你是对的。但是如果没有该操作,则不应该刷新页面,这对于 ajax 请求毫无意义。如果这没有帮助,我会检查是否已发送所需的数据并尝试将 h:panelGroup 更改为 p:outputPanel,因为这样可以保证更新时没有问题。
    猜你喜欢
    • 2014-03-23
    • 1970-01-01
    • 2014-06-24
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多