h:按下p:commandButton时,消息不显示消息

hFo*_*nti 10 jsf messages primefaces

我在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>
Run Code Online (Sandbox Code Playgroud)

使用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 "";
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 20

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

一种方法是指定update属性on <p:commandButton>以指向<h:messages>组件的客户端ID .

<h:messages id="messages" ... />
<h:form>         
    <p:commandButton ... update=":messages" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

另一种方法是用PrimeFaces替换它,<p:messages>它具有一个autoUpdate属性,用于自动更新ajax响应.

<p:messages ... autoUpdate="true" />
<h:form>         
    <p:commandButton ... />
</h:form>
Run Code Online (Sandbox Code Playgroud)

一个完全不同的选择是通过向ajax="false"按钮添加属性来关闭ajax ,这样就可以执行同步回发,这有效地导致整页更新,就像标准JSF <h:commandButton>在没有使用时的行为一样<f:ajax>.

<h:messages ... />
<h:form>         
    <p:commandButton ... ajax="false" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

也可以看看: