Not able to print message from properties file using size annotation using spring-mvc

早过忘川 提交于 2021-01-29 06:40:47

问题


I am completely new to Spring MVC. I have a Student class with below annotation

@Size(min=2, max=10 )
public String studentHobby;

and a StudentController class:

@RequestMapping("admissionSuccess.html")
public ModelAndView admissionSuccess(@Valid @ModelAttribute("student") Student student,BindingResult result)
{

    if(result.hasErrors())
    {
        ModelAndView model=new ModelAndView("admissionForm");
        return model;
    }
    ModelAndView model=new ModelAndView("admissionSuccess");
    model.addObject("student",student);
    return model;
}

and studentmessages.properties:

Size.student.studentHobby=please enter a value for studenthobby  between 2 and 10;

and spring-servlet.xml file :

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/studentmessages/"></property>
</bean>

I am not able to print the message from properties file, I am getting the default message. Please tell me whether there is something wrong with the code.


回答1:


How is your directory structure? It looks like you set the basename of yout ReloadableResourceBundle to match your property filename. Instead of that it must match the folder where you put your message files. And rename studentmessages.properties to ValidationMessages.properties (the default resource bundle for validation messages).

Like this:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/"></property>
</bean>

And add this to your configuration xml:

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

Directory structure:

...
   |_ WEB-INF
         |_ messages
               |_ ValidationMessages.properties

And you should specify in the property of your model which message should be used on validation (like Igor Patsyan said). Like this:

@Size(min=2, max=10, message="Size.student.studentHobby")
public String studentHobby;

Reference: http://www.silverbaytech.com/2013/04/16/custom-messages-in-spring-validation/




回答2:


You need to add an attribute message to the annotation @Size:

@Size(min=2, max=10, message = 'Size.student.studentHobby' )

If you don't define this attribute, a message will be genereted automatically:

"size must be between 2 and 10"

And also you need:

  1. to define correct path WEB-INF/i18n/messages to your property file in messageSource bean;

  2. to configure LocalValidatorFactoryBean in the config file;

  3. to inject the declared validator in your controller and use it.

As shown here.



来源:https://stackoverflow.com/questions/31223859/not-able-to-print-message-from-properties-file-using-size-annotation-using-sprin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!