我正在维护一个应用程序,其中在几个100个Jsps和tagx文件中,我需要替换一些硬编码的字符串-替换值将从已经读入的属性文件中驱动。
我的spring mvc应用程序中的属性文件是这样读入的:
<context:property-placeholder location="classpath*:someProps.properties, someOther.properties" />没有可以添加的id属性,并且我无法通过id获得值,所以这个选项是不存在的。
在internet上解决这个问题的唯一解决方案是声明一个PropertiesFactoryBean,然后使用spring eval读入一个jsp/tagx。如下所示:
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="properties">
<props>
<prop key="database.name">${database.name}</prop>
</props>
</property>
</bean>如果我需要读取很多值,这很快就会变得很麻烦(看起来这个应用程序很快就会出现这种情况)。有没有其他方法可以从jsp/tagx文件中的属性文件中读取属性?如果有人能告诉我PropertiesFactoryBean和context:property-placeholder之间的区别,也会对我有所帮助。
Spring版本3.2.2发布
发布于 2014-04-29 19:29:34
您可以在jsp:的上下文xml中通过property propertyplaceholder和spring标记获取jsp中的属性值:
<!-- PropertyPlaceHolder -->
<util:properties id="propertyConfigurer" location="WEB-INF/test/someProps.properties"/>
<context:property-placeholder properties-ref="propertyConfigurer"/>在您的jsp中:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
...
<spring:eval expression="@propertyConfigurer.getProperty('your.property1')" />发布于 2016-04-13 10:46:23
您可以尝试i18n,在spring-Servlet.xml中添加配置信息:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- properties path -->
<property name="basename" value="messages" />
<property name="useCodeAsDefaultMessage" value="true" />
在jsp文件中,您只需添加:<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>,然后就可以从.propertise文件中获取值,如下所示:
<spring:message code="hello" arguments="111,222" argumentSeparator=",">祝好运!
https://stackoverflow.com/questions/23359056
复制相似问题