如何在弹簧 MVC 应用中显示 JSP 中属性文件中的值

2022-09-02 02:20:44

我用这样的豆子设置我的属性:app-servlet.xml

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="/WEB-INF/my.properties"></property>
    </bean>

大多数时候,我访问控制器或其他类中的属性,如下所示:

@Value("${dbtype}")
public String dbType;

但是,如果我想在 JSP 文件中使用属性并绕过控制器,该怎么办?这意味着我不希望将值类型作为模型属性从控制器传递到JSP。

有没有办法直接在jsp中访问属性?


答案 1

弹簧配置

<util:properties id="propertyConfigurer" 
                  location="classpath:yourPropertyFileClasspathHere "/>
<context:property-placeholder properties-ref="propertyConfigurer" />

断续器

<spring:eval expression="@propertyConfigurer.getProperty('propertyNameHere')" />

答案 2

您还可以执行的操作不会将您束缚在单个属性占位符中查找属性,或者如果您使用的是java配置并且只是实例化了属性SourcesPlaceholderConfigurer,则使用环境对象:

<spring:eval expression="@environment.getProperty('application_builtBy')" />