【问题标题】:Displaying .properties Value in Spring JSP在 Spring JSP 中显示 .properties 值
【发布时间】:2015-12-11 20:35:40
【问题描述】:

我试图在每个 jsp 页面的页脚中显示我在 Spring 4 中构建的应用程序的版本。我想从 .properties 文件中读取值并将其输出到 footer.jsp,该文件将在每个页面上继承。

它没有按我的意愿工作,我很难调试它。

问题:这是正确的方法吗?如果是这样,有什么建议我做错了吗?

我试过了:

上下文配置:

@Bean(name = "applicationVersion")
public String applicationVersion() {
    return environment.getRequiredProperty("application.version"));
}

@javaConfig

@Bean
public ViewResolver jspViewResolver() {
  InternalResourceViewResolver viewResolver = new  InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);

  viewResolver.setExposeContextBeansAsAttributes(true);
  viewResolver.setExposedContextBeanNames("applicationVersion");
  return viewResolver;
}

JSP:

<span><c:out value="${applicationVersion}"/></span>

【问题讨论】:

  • 我对@9​​87654324@ 不熟悉,但是您是否尝试在将某些内容发送到您的JSP 之前将其打印到控制台?以防万一……

标签: java spring jsp javabeans spring-java-config


【解决方案1】:

我不确定你在看什么,只是一些我可以帮助你的指南:

在 Java Config 中设置属性文件;

@Bean(name = "PropertiesFile")
public static PropertyPlaceholderConfigurer cmsProperties() {
    PropertyPlaceholderConfigurer placeholder = new PropertyPlaceholderConfigurer();
    ClassPathResource[] value = new ClassPathResource[] { new ClassPathResource("Put your properties location here") };
    placeholder.setLocations(value);
    return placeholder;
}

如果你想使用属性文件中的数据,

只需使用@Value

例如在属性文件中你有这个:

version=1.3

然后调用它只需在控制器或 bean 中使用它:

@Value("${version}") String version

希望对你有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-21
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多