Spring:如何从属性文件中设置@DateTimeFormat的模式?

时间:2013-07-31 19:23:55

标签: spring model controller annotations datetime-format

我正在使用Spring 3.1.1.RELEASE。我有一个模型,我提交给我的一个控制器。在其中,是以下字段

@DateTimeFormat(pattern = "#{appProps['class.date.format']}")
private java.util.Date startDate;

但是,上述方法不起作用(EL没有被解释),就每次提交表单而言,我都会收到错误。如果我使用以下

@DateTimeFormat(pattern="yyyy-MM-dd")
private java.util.Date startDate;
一切正常。但理想情况下,我想从属性文件中驱动模式。这是可能的,如果是这样,那么正确的语法是什么?

  • 戴夫

2 个答案:

答案 0 :(得分:2)

现在似乎只与Property Place持有人合作。

看看这个: https://jira.springsource.org/browse/SPR-8654

答案 1 :(得分:1)

我会使用PropertySourcesPlaceholderConfigurer来读取我的系统属性。然后,您可以使用此语法来解析占位符:${prop.name}

您的注释文件应该像这样工作:

@DateTimeFormat(pattern = "${class.date.format}")
private java.util.Date startDate;

要在xml中为您的应用程序配置PropertySourcesPlaceholderConfigurer,请尝试以下操作:

<bean class="org.springframework.beans.factory.config.PropertySourcesPlaceholderConfigurer">
  <property name="location">
    <list>
      <value>classpath:myProps.properties</value>
    </list>
  </property>
  <property name="ignoreUnresolveablePlaceholders" value="true"/>
</bean>

或者,使用JavaConfig:

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    //note the static method! important!!
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[] { new ClassPathResource("myProps.properties")};
    configurer.setLocations(resources);
    configurer.setIgnoreUnresolvablePlaceholders(true);
    return configurer;
}