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

13

我在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个回答

22

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

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

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

另一种方法是使用PrimeFaces的<p:messages>替换它,该组件具有autoUpdate属性,可用于在Ajax响应上进行自动更新。

<p:messages ... autoUpdate="true" />
<h:form>         
    <p:commandButton ... />
</h:form>
完全不同的选择是通过在按钮上添加 ajax="false" 属性来关闭 ajax,这样将执行同步 postback,从而实现完整页面更新,就像标准的 JSF <h:commandButton> 在没有 <f:ajax> 的情况下的行为一样。
<h:messages ... />
<h:form>         
    <p:commandButton ... ajax="false" />
</h:form>

另请参阅:


我浪费了一些时间,因为p:inputMask忽略了所有规则。 - Gilberto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接