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

时间:2013-04-23 17:06:44

标签: jsf primefaces messages

我在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 "";
}

1 个答案:

答案 0 :(得分:19)

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

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

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

另一种方法是使用具有<p:messages>属性的PrimeFaces autoUpdate替换它,以便自动更新ajax响应。

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

完全不同的替代方法是通过向按钮添加ajax="false"属性来关闭ajax,这样就可以执行同步回发,这有效地导致整页更新,就像标准JSF {{ 1}}在没有<h:commandButton>的情况下使用。

<f:ajax>

另见: