[英]JSF custom message for validation error
I'm using GlassFish 3.1.2.2 and I want to define a generic validation message template to not include field identifier/label. 我正在使用GlassFish 3.1.2.2,并且想定义一个通用验证消息模板,以不包含字段标识符/标签。
I've found couple of related topics on StackOverflow but none of these solutions (all involving javax.faces.validator.BeanValidator.MESSAGE ) is working. 我在StackOverflow上找到了几个相关的主题,但是这些解决方案(都涉及javax.faces.validator.BeanValidator.MESSAGE )都没有起作用。
1) placing ValidationMessages.properties in root of classpath (in my case it was placed in WEB-INF/classes of my WAR file) - no effect 1)将ValidationMessages.properties放在类路径的根目录中(在我的情况下,它放在WEB-INF / WAR文件的类中)-无效
2) defining the ValidationMessages.properties file in <message-bundle> in faces-config.xml - no effect 2)在faces-config.xml的<message-bundle>中定义ValidationMessages.properties文件-无效
3) defining the ValidationMessages.properties file in <resource-bundle> in faces-config.xml - no effect 3)在Faces-config.xml的<resource-bundle>中定义ValidationMessages.properties文件-无效
I tested that resource bundle is correctly working as I can use it in EL, example: #{text['javax.faces.validator.BeanValidator.MESSAGE']} where text is my var from resource-bundle definition. 我测试了资源束是否可以正常工作,因为我可以在EL中使用它,例如:#{text ['javax.faces.validator.BeanValidator.MESSAGE']}其中text是我来自资源束定义的变量。
NOTE: I don't want to give validatorMessage attribute on each of input fields in my application. 注意:我不想在我的应用程序的每个输入字段中都提供validateatorMessage属性。 I just want to setup my custom message once for whole application.
我只想为整个应用程序设置一次自定义消息。
IMPORTANT: the solution presented here Mkyong.com is not working as well. 重要提示:此处显示的解决方案Mkyong.com效果不佳。
NOTE: I'm declaring to use JSF 2.0 and Glassfish 3.1.2.2 certainly supports that. 注意:我声明使用JSF 2.0,而Glassfish 3.1.2.2当然支持该功能。 NOTE: I don't want to implement validation in managed bean instead of using JSF validation/Bean Validation.
注意:我不想在托管bean中实现验证,而不是使用JSF验证/ Bean验证。
I'm answering myself: to set custom message for required-field validation you need to set following properties in messages bundle file (message bundle is defined in faces-config.xml by <message-bundle> tag): 我在回答自己:要为必填字段验证设置自定义消息,您需要在消息捆绑文件中设置以下属性(消息捆绑是在faces-config.xml中通过<message-bundle>标记定义的):
1) javax.faces.component.UIInput.REQUIRED_detail - a message that is by default presented in h:message or h:messages tags - or when showDetail=true (it is by default). 1) javax.faces.component.UIInput.REQUIRED_detail-默认情况下,在h:message或h:messages标签中显示的消息-或showDetail = true时(默认情况下)。 GENERALLY IT'S ENOUGH.
一般而言,足够了。
2) javax.faces.component.UIInput.REQUIRED - a message that is presented when showSummary=true in h:message and h:messages tags. 2) javax.faces.component.UIInput.REQUIRED-当h:message和h:messages标签中的showSummary = true时显示的消息。 Set this additionally if you display summary in your h:message/h:messages.
如果在h:message / h:messages中显示摘要,请另外设置此设置。
NOTE: this is not true that javax.faces.validator.BeanValidator.MESSAGE is responsible for required-field-error messages. 注意:这不是javax.faces.validator.BeanValidator.MESSAGE负责必填字段错误消息的说法。
Add <h:messages>
in your jsp/xhtml and to display error message from Managed Bean depending upon condition Use 在您的jsp / xhtml中添加
<h:messages>
并根据条件显示来自Managed Bean的错误消息使用
FacesContext ctx = FacesContext.getCurrentInstance();
FacesMessage msg =new FacesMessage(FacesMessage.SEVERITY_INFO, errorMessage,detailMessage);
ctx.addMessage(null, msg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.