I have the following code for some user input validation
<h:form>
<h:inputText value="#{user.input}">
<f:validator validatorId="validator.InputValidator" />
</h:inputText>
<h:commandButton action="someaction" value="submit">
</h:commandButton>
<h:messages layout="table"></h:messages>
</h:form>
It works fine, but I need to show some UTF-8 message if user input is not valid,
how can I do this?
I assume that your current problem is that characters outside the ISO-8859-1 range are been shown as mojibake. Is this true? I couldn’t think of another reason for asking this trivial question. Yes? Then read ahead:
First, if you’re still using old JSP instead of its successor Facelets, then you need to set the page encoding to UTF-8. Put this in top of every JSP:
or configure it globally in
web.xml:If you’re using Facelets, you don’t need to do anything. It uses by default already UTF-8 as response encoding.
Second, if you’re obtaining the messages from
.propertiesfiles, then you need to understand that those files are by default been read using ISO-8859-1. See alsojava.util.Propertiesjavadoc:So, you need to write down the the characters outside the ISO-8859-1 range by Unicode escape sequences. E.g. instead of
you need to write
This can be automated by using
native2asciitool.As a completely different alternative, you can supply JSF a custom
ResourceBundleimplementation with a customControlwhich reads the files using UTF-8. This is in more detail expanded in this answer and this blog. This works only whenever you’re providing validation messages by your own as for examplerequiredMessageinstead of overriding JSF default validation messages. Other than that (i.e. you need it for<message-bundle>files), then you really need to use thenative2asciitool.See also: