2

I have this class, where the field "nome" can't be empty

public class Utente {       
    ......

    @NotEmpty 
    @Size(min=2, max=30)
    private String nome;
    .......    
}

After the user fills and sends the form I call this method

    @RequestMapping("/formSent")
    public String formSentMethod(Model model, @Valid Utente utente, BindingResult result){
    .....
}   

I print the error in the Eclipse's console with this plain code, put inside the formSentMethod method.

    @RequestMapping("/formSent")
    public String formSentMethod(Model model, @Valid Utente utente, BindingResult result){
    .....
    for(ObjectError errore : result.getAllErrors()){            
        System.out.println(errore.getDefaultMessage());         
    }
    .....
}   

So far, I get the default error message "may not be empty", when the user doesn't fill the field "nome".

I tried to customize that message by using a properties file called messages.properties, that I put under WEB-INF/classes, as you can see in this pic enter image description here

Inside messages.properties I wrote

   NotEmpty.utente.nome = Il nome è obbligatorio

In my XXX-servlet.xml I "call" the properties file this way

<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">   

            <property name="basename" value="messages" />
    </bean> 

<mvc:annotation-driven />

Since it didn't work I even tried editing the basename value this way

<property name="basename" value="WEB-INF/Classes/messages" />

but it didn't work as well.

I keep on getting the default error message and not my customized message "Il nome è obbligatorio". Probably what I wrote in my properties file is not correct. What am I doing wrong?

MDP
  • 4,177
  • 21
  • 63
  • 119
  • Could you check your .war whether the messages.properties exists there? – StanislavL Sep 16 '14 at 13:49
  • Could you show the view/html part that shows the error? – dieend Sep 16 '14 at 13:55
  • @dieend I edited my post. So far i print the error message on the Eclipse's console. – MDP Sep 17 '14 at 06:36
  • I mean check whether the message.properties file exists in the war file. No need to change the value of messageSource. Looks like it can't load the messages.properties file. TGry to @Autowire the messageSource in your controller and check whether it contains the key/value pair. – StanislavL Sep 17 '14 at 07:07

2 Answers2

2

message.properties doesn't reference the class and field, but form name and the field. For example in this html:

<form:form method="POST" commandName="utente" action="customer/signup">
    <form:errors path="*" cssClass="errorblock" element="div" />
    <table>
        <tr>
            <td>Customer Name :</td>
            <td><form:input path="nome" /></td>
            <td><form:errors path="nome" cssClass="error" /></td>
        </tr>
        <tr>
            <td>Customer Age :</td>
            <td><form:input path="age" /></td>
            <td><form:errors path="age" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" /></td>
        </tr>
    </table>
</form:form>

NotNull.utente.nome will be correct, because the form utente contains field nome. The important thing is, it's not based on the class but the path in html.

To access the internationalization from controller, you need MessageSource bean, and use the bean to get the internationalization.

Here is example taken from here to access internationalization message in controller:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="ValidationMessages"/>
</bean>

Inject your MessageSource instance inside your Controller

@Autowired
private MessageSource messageSource;

And to get your message, do as follows

for (Object object : bindingResult.getAllErrors()) {
    if(object instanceof FieldError) {
        FieldError fieldError = (FieldError) object;

        /**
          * Use null as second parameter if you do not use i18n (internationalization)
          */

        String message = messageSource.getMessage(fieldError, null);
    }
}
Community
  • 1
  • 1
dieend
  • 2,231
  • 1
  • 24
  • 29
0

I believe that message's should be placed inside classes but one level up, according to your configuration.

You could also make a absolute path like:

/WEB-INF/classes/messages

or one level up

/WEB-INF/messages

Also, remember that with this configuration, you're not setting default encoding, which may lead to some problems, I suggest set property defaultEncoding to UTF-8

kamil
  • 3,482
  • 1
  • 40
  • 64