8

I created validation messages(ValidationMessages.properties) file to make the i18n possible in my project.

It looks like:

pwtNumber.error=PWT Number error.. 

I defined it in faces-config.xml:

<message-bundle>com.mycompany.web.i18n.ValidationMessages</message-bundle>

In my code I used it in this way:

 @Null(message = "{pwtNumber.error}")
    public String getPwtNummer() {
        return pwtNummer;
    }

But the problem is i don't get the error message but the key from the properties file. It is the error message that i get:

myForm:pwtNummer: {pwtNumber.error}

How can i solve it?

Kayser
  • 6,544
  • 19
  • 53
  • 86

1 Answers1

8

You're confusing JSF builtin validation with JSR303 bean validation.

The <message-bundle> is to be used to override/specify JSF builtin validation messages, not JSR303 bean validation messages. Although you used the right filename for the JSR303 bean validation bundle file, ValidationMessages.properties, the JSR303 bean validation however also requires the bundle file to be placed in the root of the classpath (thus, without any package; in the default package). After you've fixed that, don't forget to remove the incorrect <message-bundle> entry.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Where should i replace this file in a maven project? src/main/webapp?? – Kayser Sep 05 '12 at 12:56
  • I have no idea. I'm no Maven guy. I'd say, just in root of Java source folder, there where you've placed your Java packages. Just in the same level as the `com` folder representing your `com.` root package. – BalusC Sep 05 '12 at 13:00
  • I put the file in root and I removed the `` entry from faces-config.xml. I wrote some keys and values in the property file like `javax.faces.component.UIInput.CONVERSION -- {0}: Conversion error occurred` I could not find the class for `@Null` annotation or `@Size` annotation. Therefore i could not override the error messages of these annotations . Where can i find them.. – Kayser Sep 05 '12 at 14:50
  • Uh, you seem to keep confusing them. `javax.faces.component.UIInput.CONVERSION` is JSF builtin converter message, not part of JSR303 bean validation. Put it in a separate properties file which you register by ``. – BalusC Sep 05 '12 at 14:51
  • Hmm I am really confused now. How can i override the bean validation messages(JSR303)? – Kayser Sep 05 '12 at 14:55
  • Specify the `message` attribute of the JSR303 annotation like as you already did for `@Null(message = "{pwtNumber.error}")`. The code in your question is fine, the only thing which you need to change is placing the properties file in package root and getting rid of ``. That's all, exactly as mentioned in my answer. – BalusC Sep 05 '12 at 14:56
  • And i should define it in validationmessages.properties. This file should be kept in root folder. How does the Projct know that the property file is there. Should i register it? – Kayser Sep 05 '12 at 14:58
  • 1
    You don't need to register it. It's just the predefinied filename `ValidationMessages` (case sensitive!) and the predefinied location (package root!) which makes it to be auto-registered. – BalusC Sep 05 '12 at 14:58
  • It does not work. I put the file in web Module(Includes managedbeans). But the annotated classes are in another Module (Data Module). I generate an ear using them. Can it be the reason. ? – Kayser Sep 05 '12 at 15:06
  • I'm no Maven guy, but that file has ultimately to end up in `/WEB-INF/classes` folder of the built WAR file. Usually (at least, without Maven), just placing it in root of Java source folder should do it. – BalusC Sep 05 '12 at 15:15
  • I checked.. It is in /WEB-INF/classes – Kayser Sep 05 '12 at 15:19
  • it must be in war(/WEB-INF/classes) not directly in ear, right?? – Kayser Sep 05 '12 at 15:19
  • Yes, in WAR. It should work fine now. I checked your code snippet again and `@Null` isn't recognizeable as a standard JSR303 bean validation annotation. It should be `@NotNull`. – BalusC Sep 05 '12 at 15:31
  • Thank you very much. I found it. The default language in my operating system is german. Therefore it needs ValidationProperties_de I added this file then it works. A silly question again. How can the user change bean validation language? – Kayser Sep 05 '12 at 15:34
  • 1
    The locale used is obtained from `UIViewRoot#getLocale()`. Thus, the enduser can change it by changing the JSF view locale (the ``). – BalusC Sep 05 '12 at 15:35
  • if the BeanValidation framework integrates with JSF for getting locale why can't integrate with JSF message bundles for localisation? why so many different, competing and redundant artefacts? Strange are the ways of Java/JEE Spec writers! – Saasira Dec 27 '13 at 09:15