Mariusz.v7
Mariusz.v7

Reputation: 2442

Spring validation didn't display error messages saved in property file (freemarker template)

I have a problem with display error message defined in .properties file.

Here is my servlet-context.xml fragment (it should define resource bundle and inject it into validator; the resource messages are working well everywhere else):

  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8"/>
  </bean>

  <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="pl"/>
  </bean>

  <bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource" ref="messageSource" />
  </bean>

And here is a fragment of my property file:

messages.forms.validation.size.between=bla bla bla
messages.forms.validation.size.between.and=and

Here is a field definition.

@NotEmpty
@NotNull
@Size(min = 4, max = 25, message = "{messages.forms.validation.size.between} {min} {messages.forms.validation.size.between.and} {max}")
private String login;
public void setLogin(String v) { login = v; }
public String getLogin() { return login; }

What I expect is (according to: message = "{messages.forms.validation.size.between} {min} {messages.forms.validation.size.between.and} {max}") to get an error: bla bla bla 4 and 25, but instead of it, I'm getting: not converted message

I have spent couple of hours trying to find a solution with no success, so please help!

Upvotes: 0

Views: 715

Answers (1)

smoggers
smoggers

Reputation: 3192

Just write your message within the properties file, there is no need to set the message within the Bean. Below is how I display validation error messages via a properties file.

servlet.xml:

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="com.sga.app.messages"></property>
</bean>

UserBean.java:

@NotBlank
@Size(min = 6, max = 16)
@Pattern(regexp = "^\\w{6,}$")
@Id
private String username;

messages.properties:

Size.userBean.username = Username must be between 6 and 16 characters in length

register.jsp:

<li>
    <label id="outputUsername">
    <c:out value="Enter username:"></c:out>
    </label>
    <sf:input class="usernameInput" name="username" type="text"
      path="username" tabindex="0" id="usernameInput" />
    <sf:errors path="username" cssStyle="color:red;
      font-size: small;" />
</li>

Upvotes: 1

Related Questions