按下 p:commandButton 时,h:messages 不显示消息 [英] h:messages does not display messages when p:commandButton is pressed
问题描述
我对 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>
另见:
- 了解 PrimeFaces 流程/更新和 JSF f:ajax 执行/渲染属性
这篇关于按下 p:commandButton 时,h:messages 不显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!