Spring 4资源属性文件

时间:2016-05-27 09:37:48

标签: spring-mvc properties-file

MyClass的

@Entity
@Table(name = "PIL_P_APPLCTN")
public class PIL_P_APPLCTN extends META_INF {

    @NotBlank
    @Column(unique = true, nullable = false, name = "APPLCTN_CD")
    private String APPLCTN_CD;

    @NotBlank
    @Column(nullable = false, name = "APPLCTN_ID")
    private String APPLCTN_ID;

    @NotBlank
    @Column(nullable = false, name = "APPLCTN_DS")
    private String APPLCTN_DS;

    @NotNull
    @Digits(integer = 20, fraction = 0)
    @Column(nullable = false, name = "DECLG_IDNTFNT_NU")
    private BigDecimal DECLG_IDNTFNT_NU;

}
src / main / resources

中的

messages.properties

NotBlank.pil_p_applctn.APPLCTN_CD=Champ obligatoire !
NotBlank.pil_p_applctn.APPLCTN_ID=Champ obligatoire !
NotNull.pil_p_applctn.APPLCTN_DS=Champ obligatoire !
NotBlank.pil_p_applctn.DECLG_IDNTFNT_NU=Champ obligatoire !
Digits.pil_p_applctn.DECLG_IDNTFNT_NU=Seules les données numériques sont acceptées avec max 20 chiffres !
non.unique.APPLCTN_CD={0} existe déjà !
typeMismatch=Format invalide !

AppConfig.class

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.fussa.fyby")
public class AppConfig extends WebMvcConfigurerAdapter {



    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
@Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

}

当我尝试

messageSource.setBasename("classpath:/messages");

messageSource.setBasename("classpath:messages");

这个消息是在控制台中显示:

  

警告:org.springframework.context.support.ResourceBundleMessageSource    - 找不到MessageSource的ResourceBundle [classpath:/ messages]:找不到基本名称类路径的包:/ messages,locale fr_FR

所以我认为messageSource.setBasename("messages");可以访问 messages.properties ..但为什么在我的jsp文件中会显示默认消息,而不是 messages.properties

感谢您的任何建议..

2 个答案:

答案 0 :(得分:0)

由于您的语言区域为fr_FR,因此请将您的文件重命名为messages_fr.properties,以便Spring可以根据所选的显示语言选择正确的文件。

答案 1 :(得分:0)

通过从 messages.properties 中删除一些自定义消息并将其添加到我的实体

来修复问题
@Entity
@Table(name = "PIL_P_APPLCTN")
public class PIL_P_APPLCTN extends META_INF {
    @NotBlank(message="Champ obligatoire !")
    @Column(unique = true, nullable = false, name = "APPLCTN_CD")
    private String APPLCTN_CD;

    @NotBlank(message="Champ obligatoire !")
    @Column(nullable = false, name = "APPLCTN_ID")
    private String APPLCTN_ID;

    @NotBlank(message="Champ obligatoire !")
    @Column(nullable = false, name = "APPLCTN_DS")
    private String APPLCTN_DS;

    @NotNull(message="Champ obligatoire !")
    @Digits(integer = 20, fraction = 0,message="Max 20 chiffres !")
    @Column(nullable = false, name = "DECLG_IDNTFNT_NU")
    private BigDecimal DECLG_IDNTFNT_NU;

    public PIL_P_APPLCTN() {
    }}

<强> messages.properties

non.unique.APPLCTN_CD=" {0} " existe déjà !
typeMismatch=Format invalide !