java - ReloadableResourceBundleMessageSource

标签 java spring-mvc

Spring mvc 新手,我尝试使用属性文件中的“ReloadableResourceBundleMessageSource”显示自定义错误消息,但仍然无法获取自定义消息,我得到的是 spring 默认消息。

我的 Controller 类:-

public class Controller1 {

    @InitBinder
    public void initMethod(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, "name", new Customproperty());
    }

    @RequestMapping(value = "/frontend/getdetails.htm", method = RequestMethod.GET)
    public ModelAndView getStudentsData(@Valid @ModelAttribute("student") Student student, BindingResult result) {

        if (result.hasErrors()) {
            ModelAndView model = new ModelAndView("input");
            return model;
        }

        else {
            ModelAndView model = new ModelAndView("output");
            return model;
        }

    }

我的模型类:- 其中大小变量经过验证。

public class Student {

    private String name;
    private int age;
    @Size(min = 5, max = 15)
    private String address;
    private String hobby;
    private String email;
    }

Spring Xml 文件:-

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

属性文件(/SpringLearning_9dec/WebContent/WEB-INF/errormessages.properties):-

Size.student.address = address should be with in 44 to 44 characters.

PFA 屏幕截图还有:- program

最佳答案

实际上,我不确定,但用于 ReloadableResourceBundleMessageSource 属性的 id 似乎存在问题。正如 ReloadableResourceBundleMessageSource 的文档中所解释的那样,它是 public interface MessageSource 的实现。因此,它的两个实现类 ReloadableResourceBundleMessageSourceResourceBundleMessageSource 应该具有与接口(interface) messageSource 相同的 id。

我认为在寻找接口(interface) MessageSource 的实现时,Spring 会查找 id messageSource 且实现类为 ResourceBundleMessageSourceReloadableResourceBundleMessageSource 的 bean。

关于java - ReloadableResourceBundleMessageSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48164637/

相关文章:

java - Spring MVC - Liferay - 验证(使用 @valid 注释)

java - JSP 和服务器端延迟

java - 检查 Java 是否安装了 Bash

java - 为Hadoop实现协调器服务

java - 当用户捕获图像时,如何检查 android 中当前打开的相机 ID(主要或次要)?

java - 在 oAuth2 资源服务器应用程序中使用@WithMockUser(与@SpringBootTest)

java - Maven Spring Boot 多模块

java - H2连接池

java - Android 上的 Apache Commons IO

Spring 3 MVC : is it possible to have a spring form without 'commandName' binding?