h:messages gives no output
-
03-10-2019 - |
Pergunta
I'm learning about bean validation. I have created simple form with username and password input fields which are bound through backing bean to model (User) properties username and password. I have marked them with annotation:
@Id
@NotNull(message="Username cannot be empty")
private String username;
@NotNull(message="Password cannot be empty")
private String password;
Now on the page with that form I've put <h:messages/>
inside <h:form>...</h:form>
. I submit empty form, I am redirected to the same page (I have return null in action method in backing bean) but no messages are being displayed. What am I doing wrong?
Solução
Solution is instead of using @NotNull
, use @Size
because empty input field is actually empty string and not null
.
Outras dicas
If you'd like to get @NotNull
to work, you indeed need to let JSF set empty strings as null
. In JSF 2.0 you can do that by adding the following context param to the web.xml
:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
I can't tell from experience if that would trigger the @NotNull
, but in theory it should. Give it a try.