我在JBoss EAP 6.0上有一个JSF 2.1 + RichFaces 4.0 webapp。我无法使用FacesMessage
显示从bean到用户的自定义错误消息。我正在使用addMessage()
添加消息。但是,该消息未显示在<rich:messages>
和我的控制台中:
INFO [javax.enterprise.resource.webcontainer.jsf.renderkit](http-localhost / 127.0.0.1:8888-3)警告:FacesMessage已入队,但可能尚未显示。 sourceId = null [severity =(ERROR 2),summary =(mobile =应大于10 degits),detail =()]。
查看:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<ui:composition>
<h:head/>
<link href="../../css/styles.css" rel="stylesheet" type="text/css" />
<h:form id="personform12" >
<h:panelGrid id="test2343" width="80%">
<rich:messages id="messages" globalOnly="true" errorClass="globalError" showSummary="true" showDetail="false"></rich:messages>
</h:panelGrid>
<h:panelGrid border="0" columns="3" width="90%" cellpadding="0" cellspacing="1" >
<h:panelGroup styleClass="alignmentRight">
<h:outputText value="#{bundle['id']}" />
</h:panelGroup>
<h:inputText id="id" value="#{personBean.pdto.id}" required="true">
</h:inputText>
<rich:message styleClass="error" for="id" />
<h:panelGroup>
<h:outputText value="#{bundle['name']}" />
</h:panelGroup>
<h:inputText id="name" value="#{personBean.pdto.name}" required="true"/>
<rich:message styleClass="error" for="name" />
<h:panelGroup styleClass="alignmentRight">
<h:outputText value="city" />
</h:panelGroup>
<h:inputText id="city" value="#{personBean.pdto.city}" required="true"/>
<rich:message styleClass="error" for="city" />
<h:panelGroup styleClass="alignmentRight">
<h:outputText value="state" />
</h:panelGroup>
<h:inputText id="state" value="#{personBean.pdto.state}" required="true"/>
<rich:message styleClass="error" for="state" />
<h:panelGroup styleClass="alignmentRight">
<h:outputText value="mobile" />
</h:panelGroup>
<h:inputText id="mobile" value="#{personBean.pdto.mobile}" required="true">
</h:inputText>
<rich:message styleClass="error" for="mobile" />
<h:panelGroup styleClass="alignmentRight">
<h:outputText value="DOB" />
</h:panelGroup>
<rich:calendar style="height:25px" inputStyle="width:203px" required="true"
showWeeksBar="false"
boundaryDatesMode="scroll"
id="dob" value="#{personBean.pdto.dob}"
datePattern="dd/MM/yyyy" cellWidth="24px" cellHeight="22px" />
<rich:message styleClass="error" for="dob" />
</h:panelGrid>
<br/><br/>
<h:commandButton id="commandSubmit" value="Submit" action="#{personBean.save}"></h:commandButton>
</h:form>
</ui:composition>
</html>
型号:
public class PersonJSFBean {
private static final Logger log = Logger.getLogger(PersonJSFBean.class);
private PersonDTO pdto;
private PersonManager personManager;
public String save(){
String str = null;
try {
if(pdto.getMobile().length()<10) {
System.out.println("inside the mobile no>>>>>>>>>>>"+pdto.getMobile());
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "mobile no should be greater than 10 degits ", null);
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}
} catch (Exception e) {
System.out.println("Error occured>>>>>>>>>>>");
e.printStackTrace();
}
return str;
}
// ...
}
答案 0 :(得分:0)
由于ui:composition中没有模板属性,我假设页面是顶级的。然后缺少 h:body ,这是一个例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:head/>
<h:body>
<h:form .....
</h:body>
</html>