首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在spring MVC应用程序中显示JSP中属性文件中的值

如何在spring MVC应用程序中显示JSP中属性文件中的值
EN

Stack Overflow用户
提问于 2013-02-27 19:43:53
回答 3查看 70.7K关注 0票数 21

我在app-servlet.xml中使用这样的bean设置属性:

代码语言:javascript
复制
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="/WEB-INF/my.properties"></property>
    </bean>

大多数情况下,我会访问控制器或其他类中的属性:

代码语言:javascript
复制
@Value("${dbtype}")
public String dbType;

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

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

EN

回答 3

Stack Overflow用户

发布于 2015-01-20 22:46:26

你还可以做的并不局限于在单个属性占位符中查找属性,或者如果你正在使用java配置并且仅仅实例化一个环境就是使用PropertySourcesPlaceholderConfigurer对象:

代码语言:javascript
复制
<spring:eval expression="@environment.getProperty('application_builtBy')" />
票数 23
EN

Stack Overflow用户

发布于 2013-09-12 09:22:55

在上下文中,只需执行以下操作:

代码语言:javascript
复制
<util:properties 
    id="propertyConfigurer"
    location="classpath:yourPropertyFileClasspathHere"
/>
<context:property-placeholder properties-ref="propertyConfigurer" />

用于创建Properties bean(与answer中的@nkjava.blogspot.com相同)。但这并不是所有需要做的工作。

现在,您需要将该bean公开给JSP。有几种方法可以做到这一点,这取决于视图解析器的类型。InternalResourceViewResolver有一个解决方案--您需要将"exposeContextBeansAsAttributes“设置为true,并使用所需的beans列表填充"exposedContextBeanNames”。

对于tiles也是解决方案。

这样您就可以在JSP中简单地使用这个bean了。例如,通过EL:

代码语言:javascript
复制
${propertyConfigurer['my.string.from.prop.file']}
票数 0
EN

Stack Overflow用户

发布于 2019-02-21 09:10:17

在Spring版本4中,您可以找到以下属性文件:

1) xml模式

代码语言:javascript
复制
                <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
                   <property name="ignoreUnresolvablePlaceholders" value="true"/>
                      <property name="locations">
                          <list>
                            <!-- default resources folder (default package maven project) -->
                             <value>classpath:mongodb.remote.properties</value>  
                                <!-- Or in /WEB-INF/ folder -->
                              <value>/WEB-INF/mongodb.remote.properties</value>  
                          </list>
                      </property>
                  </bean>
----------------------------------------------------------------------------------------

2)编程模式:

代码语言:javascript
复制
    If you have for example this package : com.profile.config, com.profile.controller, ecc.. 
    it's not problem if you put only com.profile, it's ok !!! Now


    @Configuration
    @ComponentScan(basePackages = "com.profile")
    /** resources folder & default package maven project*/
    @PropertySource(value = { "classpath:mongodb.remote.properties" }) 
    public class MyPropertySourcesPlaceholderConfigurer {


        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }


    ---------------------------------------------------------------------------------
    Your property file

    label.test.val=this is the property file value!!!!!
    ---------------------------------------------------------------------------------

    @Controller
    public class LabelsAndValuesController {


         @Value("${label.test.val}")
         String test;

    }

输出:

代码语言:javascript
复制
    ---------------------------------------------------------------------------------
    this is the property file value!!!!!
    ---------------------------------------------------------------------------------
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15111260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档