问题
How can I show the field label in a JSF error message when bean validation is used?
<h:messages/>
<h:inputText label="Username" value="#{myBean.username}" />
...
public class MyBean {
@NotNull
private String username;
...
}
If no user name is submitted only "Must not be null" is shown and there is no field reference.
回答1:
To display the concerned component's label in the error message define a new value for the property javax.faces.validator.BeanValidator.MESSAGE in a resource bundle of the application like this:
javax.faces.validator.BeanValidator.MESSAGE={1}: {0}
The placeholder {0}
refers to the error message as created by the Bean Validation runtime, {1}
refers to the component label. More information can be found in the JSF 2 spec. section 3.5.6.3.
来源:https://stackoverflow.com/questions/6803536/jsf-2-0-bean-validation-and-labels