当字符串在同一个文件中定义时,我如何在spring xml文件中的map:util元素中引用字符串变量?



下面是一个xml文件的上下文,我在其中定义了一个映射及其元素。映射中的一些元素最终将由xpath组成,其中xpath的某些部分将是相同的。我想创建变量来表示那些在元素之间冗余的xpath部分。

我定义了一个带有id、属性和值的bean。无论我怎么尝试,我都无法引用bean的值。当我请求Map.get("devo").get(" error404categorescontainer ")的值时,我得到的都是"${errorContainer}"。我想让它给我"//div[@id='noResultsFeatures']//div[contains(@class,'noResultsFeaturesContainer')]".

我试图创建的bean作为一个变量开始于

我到处都找过了,但我没能找到解决办法。我尝试用#符号代替$符号。我尝试了不同的方法来定义bean,并试图"拒绝"bean,但都无济于事。有人能给我一个解决办法吗?

下面是xml文件

的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="errorContainer" class="java.lang.String">
    <property name="error404CategoryContainer" value="//div[@id='noResultsFeatures']//div[contains(@class,'noResultsFeaturesContainer')]"/>
</bean>
<util:map id="devo">
    <entry key="FourOFour">
        <util:map>                
            <entry key="error404FeaturedCategoriesHeader" value="//h2[@id='noResultsFeaturesHeader' and contains(text(),'Featured Categories')]" />

   None of these works – I only have one in the file at a time.
            <entry key="error404CategoriesContainer" value="${errorContainer}" />
            <entry key="error404CategoriesContainer" value="${errorContainer. error404CategoryContainer}" />
            <entry key="error404CategoriesContainer" value="${error404CategoryContainer}" />

            <entry key="error404CategoriesContainerLinks" value="//div[@id='noResultsFeatures']//div[contains(@class,'noResultsFeaturesContainer')][${1}]//ul//li" />
            <entry key="error404CategoriesContainerLinkByCategoryAndLinkNumber" value="//div[@id='noResultsFeatures']//div[contains(@class,'noResultsFeaturesContainer')][${1}]//ul//li[${2}]//a" />
        </util:map>
    </entry>
</util:map>

你需要为你的应用上下文定义一个PropertySourcesPlaceholderConfigurer <context:property-placeholder…>。在中,您可以将属性定义为键值对,并使用${key}语法引用值。

请看这里:http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html

最新更新