Why is the form identifier prefix in h: message output?

I am currently trying to perform a simple check using required = "true"

<h:form> <h:messages globalOnly="true"/> <h:panelGrid> <h:panelGrid columns="3"> <f:facet name="header"> Login Application Sample </f:facet> <h:outputLabel for="UserId" value="User Id" /> <h:inputText id="UserId" value="#{userBean.userId}" required="true" /> <h:message for="UserId"/> <h:outputLabel for="Password" value="Password" /> <h:inputSecret id="Password" value="#{userBean.password}" required="true" /> <h:message for="Password" /> <f:facet name="footer"> <h:commandButton value="Login" action="#{userBean.login}"/> <h:commandButton type="reset" value="Reset"/> </f:facet> </h:panelGrid> </h:panelGrid> </h:form> 

Leaving the fields empty, and then click the login button, these error messages will be displayed on the right side of each field:

j_idt7: UserId: Verification error: value required.
j_idt7: Password: Verification error: value required.

This is what I expected, but I do not want to display the id form prefix 'j_idt7:'. I read examples of books, they do not output the form identifier prefix. I want to:

UserId: Verification error: value required.
Password: Verification error: value required.

What should I do to skip displaying the form identifier prefix in component-specific messages?

I am currently testing JSF 2 in glassfish v3.

+4
source share
5 answers

The default message icon corresponds to the component's client ID, exactly the same as you can see in the generated HTML output via rightclick, View Source. This j_id7 in this particular case is the client identifier of the parent <form> element. If you provide the JSF component with a fixed ID, for example, <h:form id="login"> , then the labels will become login:UserId and login:Password respectively.

However, you can use the attribute of the input component label to completely redefine it so that the message label appears exactly as you intended.

 <h:inputText ... label="User ID" /> <h:inputSecret ... label="Password" /> 

If an attribute of the input component label present, it will be used instead of the client identifier. Using prependId="false" , as suggested by other answers, has disadvantages . Do not do that.

Alternatively, you can use the requiredMessage attribute (either converterMessage or validatorMessage ), but this does not allow you to parameterize messages and, therefore, you will have to hardcode the labels, etc.

 <h:inputText ... label="User ID is required." /> <h:inputSecret ... label="Password is required." /> 

See also:


It is noted that it is really inconvenient for the labels to be duplicated as follows:

 <h:outputLabel for="userId" value="User ID" ... /> <h:inputText id="userId" ... label="User ID" /> <h:outputLabel for="password" value="Password" ... /> <h:inputSecret id="password" ... label="Password" /> 

If you use the JSF OmniFaces utility library , you can use <o:outputLabel> so that JSF transparently sets the label attribute of the associated component:

 <o:outputLabel for="userId" value="User ID" ... /> <h:inputText id="userId" ... /> <o:outputLabel for="password" value="Password" ... /> <h:inputSecret id="password" ... /> 
+13
source

If you use MyFaces and the bean framework (JSR303), try defining a MessageBundle with the key javax.faces.validator.BeanValidator.MESSAGE

faces-config.xml

 <application> <message-bundle>ApplicationMessages</message-bundle> </application> 

ApplicationMessages.properties

 #javax.faces.validator.BeanValidator.MESSAGE={1}: {0} javax.faces.validator.BeanValidator.MESSAGE={0} 

Details

+5
source

You need to override these messages from JSF.

You may have a messages.properties file in your classpath

Messages.properties

 javax.faces.component.UIInput.REQUIRED=Field must be entered. 

Faces-config.xml

 <application> <message-bundle>Messages</message-bundle> <locale-config> <default-locale>en</default-locale> </locale-config> </application> 

Take a look at this article.

+2
source

If you want to see the HTML source from your browser, you will find that the id your field is input <form-id>+":"+<input-field-id> , in your case j_idt7:UserId: Try giving <h:form> some meaningful id to make some sense out of it. Here you can read the JSF ID here . If you don’t like it, you can disable it by changing the form tag to something like this,

 <h:form prependId = false> // its true by default. 

But this can be problematic, as indicated here by BalusC .

Also, it looks like you never set up any validation messages. This, in turn, ends with this message. Therefore, a message.properties file is needed in order to have control over the message and show something more appropriate. Even then, the field name should not be part of the message in order to make this validation message common in order to avoid duplication. See BalusC's answer regarding the use of the label attribute inside <h:inputText> .

+1
source

Where you write required = true , you can define requiredMessage = "My Message" for any type of input.

eg.

 <h:inputText autocomplete="false" id = "CellNumber" label="Cell Number" required="true" requiredMessage="Cell Number Required" maxlength="10" value="#{userManagedBean.cellNumber}" > 
+1
source

All Articles