我对 JSF 中的 h:messages 标记有疑问,它根本不显示任何消息。当我单击按钮时,在 Glassfish 日志中没有错误。设置如下:
测试.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 "";
}