【问题标题】:h:messages does not display messages when p:commandButton is pressed按下 p:commandButton 时 h:messages 不显示消息
【发布时间】:2013-04-17 00:16:56
【问题描述】:

JSF 中的 h:messages 标记存在问题,它根本不显示任何消息。当我单击按钮时,在 Glassfish 日志中没有错误。 设置如下:

test.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" 
    xmlns:j="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
    <h:messages globalOnly="true"/>
    <h:form id="loginform">         
        <p:commandButton id="testButton" value="Test"
          action="#{loginSessionBean.test()}" />
    </h:form>
</h:body>
</html>

使用 SessionScopedBean:

@ManagedBean
@SessionScoped
public class LoginSessionBean implements Serializable {

private static final long serialVersionUID = 1L;
...
public String test(){
     FacesContext fc = FacesContext.getCurrentInstance();
     fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Test!", null)); 
    return "";
}

【问题讨论】:

    标签: jsf primefaces messages


    【解决方案1】:

    您正在使用 PrimeFaces &lt;p:commandButton&gt; 发送 ajax 请求。默认情况下,Ajax 请求没有任何形式的反馈(除非在某处使用了 PrimeFaces 的autoUpdate="true")。您应该明确指定要在 ajax 响应上更新的视图部分。

    一种方法是在&lt;p:commandButton&gt; 上指定update 属性以指向&lt;h:messages&gt; 组件的客户端ID。

    <h:messages id="messages" ... />
    <h:form>         
        <p:commandButton ... update=":messages" />
    </h:form>
    

    另一种方法是将其替换为 PrimeFaces &lt;p:messages&gt;,它具有 autoUpdate 属性,用于自动更新 ajax 响应。

    <p:messages ... autoUpdate="true" />
    <h:form>         
        <p:commandButton ... />
    </h:form>
    

    另一种完全不同的选择是通过向按钮添加ajax="false" 属性来关闭ajax,这样将执行同步回发,从而有效地导致整页更新,就像标准JSF &lt;h:commandButton&gt; 的行为方式一样不使用&lt;f:ajax&gt;时。

    <h:messages ... />
    <h:form>         
        <p:commandButton ... ajax="false" />
    </h:form>
    

    另见:

    【讨论】:

    • 我损失了几个小时,因为 p:inputMask 忽略了所有规则
    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2015-05-09
    相关资源
    最近更新 更多