1

I have a CDI bean in a JSF application with this property:

@NotNull(message = "Senha não informada.")
public String getSenha()
{
    return _senha;
}

The problem is that when validation fails the message displayed on the page is this:

Senha não informada.

The problem is not with JSF, but with Bean Validation, because if I place the message in a standard JSF validator and use it instead of the bean validation annotation, the message is displayed correctly with the right accented characters.

I tried changing the request and response encoding to UTF-8 in a filter to see if the problem was fixed, but the problem remains.

Of course, if I change the annotation message to this Senha n\u00e3o informada. I get the right message on the page, but I don't want this. I want a way to instruct the bean validation API (or another thing) to use the accented characters in my own language.

I'm using Wildfly 10 with all default libraries.

P.S.: I don't want to use a message bundle for my messages.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Marcos
  • 1,237
  • 1
  • 15
  • 31
  • 1
    This can happen if the Java source file itself is not saved using UTF-8 by the editor and/or not being read using UTF-8 by the compiler. Answer depends on tooling used. Javac CLI? Eclipse? Maven? Etc. – BalusC Jul 18 '16 at 13:39
  • I'm using Eclipse and the _Text file encoding_ of the Workspace is configured to use _UTF-8_. I'm also using the Gradle plugin Buildship in Eclipse with this project. – Marcos Jul 18 '16 at 13:42
  • And what's the build tool? Eclipse itself or Maven (via e.g. m2e)? – BalusC Jul 18 '16 at 13:44
  • The build tool is Gradle (with Buildship plugin) – Marcos Jul 18 '16 at 13:45
  • Ah okay, sorry I don't do Gradle, but in Maven you'd need to set `UTF-8` property in `pom.xml`. Perhaps Gradle has something similar. – BalusC Jul 18 '16 at 13:45
  • I will check this and report my findings later. – Marcos Jul 18 '16 at 13:46
  • Nice catch, @BalusC. The configuration in Gradle is `tasks.withType(JavaCompile) {options.encoding = 'UTF-8'}` and it solved the problem. Maybe you would like to give it as an answer so I can mark it as answered. Thank you anyway. – Marcos Jul 18 '16 at 14:00

1 Answers1

2

This can happen if the Java source file itself is not saved using UTF-8 by the editor and/or not being read using UTF-8 by the compiler. Answer depends on the tooling used. It boils down to reconfiguring the tooling to use UTF-8.

In case of Eclipse, you need to make sure that workspace text file encoding is set to UTF-8 as shown in this screenshot (source). This will make Eclipse to save Java source files using UTF-8, build the project using UTF-8 and also to use UTF-8 in output console.

In case you're using Maven to build the project, then you need to add the below entries to pom.xml. The reporting output encoding isn't required to solve your specific problem, but it's handy.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

In case you're using Gradle to build the project, it can be solved with below configuration, as confirmed in your comment on the question.

tasks.withType(JavaCompile) {options.encoding = 'UTF-8'}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you again for the answer. – Marcos Jul 18 '16 at 14:41
  • I was thinking about this to, but due to this remark by the OP: _"The problem is not with JSF, but with Bean Validation, because if I place the message in a standard JSF validator and use it instead of the bean validation annotation, the message is displayed correctly with the right accented characters."_ I thought it might be something else. – Kukeltje Jul 18 '16 at 15:02
  • @Kukeltje and this is true indeed. – Marcos Jul 18 '16 at 15:05
  • @Marcos? So was the above thing the answer or not? Now you got me confused even more... – Kukeltje Jul 18 '16 at 15:15
  • @Kukeltje sorry for this. In my last comment I meant that what I said was really true, as cited by you. When I use standard _JSF_ validators the message is displayed correctly. But _Bean Validation_ is really taking a different approach than _JSF_ to encode the validation messages, that's why they are not displayed with the correct accents. – Marcos Jul 18 '16 at 15:22
  • So you meant a jsf validator in xhtml, not a 'custom' jsf validator in java? The latter is what I was assuming... Hence I thought that it could not be dependent on the java tooling saving it in a wrong way. – Kukeltje Jul 18 '16 at 15:25
  • If, for example, I use this in a _JSF_ _xhtml_ page `` the message is displayed correctly. If I use a _JSF_ validator on the xhtml page too. But this is not the case with the _Bean Validation_ annotation. I didn't test with a custom _JSF_ validator, but I suppose that the message would be displayed correctly with it too, as it comes from _JSF_. – Marcos Jul 18 '16 at 15:34
  • No it would have been wrong to since it would have been in java and fail the same as the bean-validator. So the problem was not that JSF was working and bean-validation was not, the problem was that your java code was not UTF-8 – Kukeltje Jul 18 '16 at 15:35
  • @Kukeltje Makes sense. – Marcos Jul 18 '16 at 15:36