我在app-servlet.xml
中使用这样的bean设置属性:
<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中的属性?
发布于 2015-01-20 22:46:26
你还可以做的并不局限于在单个属性占位符中查找属性,或者如果你正在使用java配置并且仅仅实例化一个环境就是使用PropertySourcesPlaceholderConfigurer对象:
<spring:eval expression="@environment.getProperty('application_builtBy')" />
发布于 2013-09-12 09:22:55
在上下文中,只需执行以下操作:
<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:
${propertyConfigurer['my.string.from.prop.file']}
发布于 2019-02-21 09:10:17
在Spring版本4中,您可以找到以下属性文件:
1) xml模式
<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)编程模式:
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;
}
输出:
---------------------------------------------------------------------------------
this is the property file value!!!!!
---------------------------------------------------------------------------------
https://stackoverflow.com/questions/15111260
复制相似问题