My xhtml page code:
<h:outputLabel value="Email address: " />
<h:inputText id="email" value="#{userBean.emailAddressBeingEdited}" required="true" requiredMessage="*" />
<h:message for="email" id="emailM"/>
My java code:
FacesContext.getCurrentInstance().addMessage("email", new FacesMessage("Email already in use"));
I tried nesting the h:message in the h:inputText, but that did not work. If I need to provide more information, please ask.
The client ID which you specify in
addMessage()has to be the real client ID, not the component ID. The client ID of an input component is usually prefixed with the ID of the parent form component. For starters who haven’t memorized which JSF components implementNamingContainerand how client IDs are generated, the easiest way to find out the real client ID is to open the page in browser, view its source and locate the generated HTML<input type="text">element and finally take itsid. If you have given the<h:form>an ID offormId, it’ll beformId:email. That’s the real client ID which you need to specify inaddMessage().If you don’t specify the right client ID, then the message remains unhandled. If the JSF project stage is set to
Development, then JSF will automatically append it in a list of “unhandled messages” in the end of the body. This is what you’re seeing. View the generated HTML source to see it yourself, it’s the autogenerated HTML list<ul id="javax_faces_developmentstage_messages">.Unrelated to the concrete problem, you seem to be doing the validation in the JSF bean action method. This is not the right way. You should be using a
Validatorfor this. This way you don’t need to worry about the client IDs. You just need to throw aValidatorExceptionwith aFacesMessage. E.g.which you then register as
<f:validator validatorId="uniqueEmailValidator" />inside the input component.