freemark 全局变量 定义 不生效

本文介绍了在Freemarker中定义全局变量不生效的问题及其解决方案。通过修改Spring配置文件和添加Java代码,以及调整Maven打包配置,确保全局变量`xpurl`能正确注入到模板中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


方法-:
<!--freemarker 注入全局变量-->
<bean id="freemarkerConfiguration"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:advtopic.properties" />
</bean>
<!-- freemarker 相关 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/"/>
    <property name="defaultEncoding" value="utf-8"/>
    <property name="freemarkerSettings">
        <props>
            <prop key="template_update_delay">10</prop>
            <prop key="locale">zh_CN</prop>
            <prop key="datetime_format">yyyy-MM-dd</prop>
            <prop key="date_format">yyyy-MM-dd</prop>
            <prop key="number_format">#.##</prop>
            <prop key="classic_compatible">true</prop>
        </props>
    </property>
    <property name="freemarkerVariables">
        <map>
            <entry key="xpurl" value="${xpurl}"></entry>
        </map>
    </property>
</bean>

这样是不生效的。要更改java代码

添加如下代码 

@Configuration
@PropertySource("classpath:xxx.properties")
public class ApiConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}


方法二
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>src/main/webapp/</directory>
                        <includes>
                            <include>*.*</include>
                            <include>**/*.*</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources/${run.environment}</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

</build>

<xpurl></xpurl>



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值