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

查看:27
本文介绍了按下 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 发送 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.

一种方法是在 上指定 update 属性以指向 的客户端 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 替换它,它有一个 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 在没有 的情况下使用时的行为.

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>

另见:

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