当按下p:commandButton时,h:messages不显示消息 [英] h:messages does not display messages when p:commandButton is pressed

查看:122
本文介绍了当按下p:commandButton时,h:messages不显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with the h:messages tag in JSF that simply does not show any messages. In the Glassfish log are no errors when I click the button. The setup is as follows:

test.xhtml:

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:

With The 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 "";
}

推荐答案

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

You're sending an ajax request with PrimeFaces <p:commandButton>. Ajax requests have by default no form of feedback (unless PrimeFaces' autoUpdate="true" is been used somewhere). You should be explicitly specifying parts of the view which you'd like to update on ajax response.

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

One way is specifying the update attribute on <p:commandButton> to point to the client ID of the <h:messages> component.

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

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

Another way is to replace it by PrimeFaces <p:messages> which has an autoUpdate attribute for the purpose of automatic update on ajax response.

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

另一种完全不同的选择是通过向按钮添加ajax="false"属性来关闭ajax,这样将执行同步回发,从而有效地进行整页更新,就像标准JSF <h:commandButton>的行为一样在不使用<f:ajax>的情况下使用.

A completely different alternative is to turn off ajax by adding ajax="false" attribute to the button, this way a synchronous postback will be performed which effectively results in a full page update, exactly like as how the standard JSF <h:commandButton> behaves when used without <f:ajax>.

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

另请参见:

  • 了解PrimeFaces流程/更新和JSF f:ajax执行/渲染属性
  • See also:

    • Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
    • 这篇关于当按下p:commandButton时,h:messages不显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆