gpt4 book ai didi

spring - 无法在电子邮件模板中快速使用宏?

转载 作者:行者123 更新时间:2023-12-01 06:12:09 33 4
gpt4 key购买 nike

大家好我在发送电子邮件时使用速度模板我想根据用户区域设置从属性文件中动态读取文本

xml配置:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

<property name="basenames">
<list>
<value>classpath:messages</value>
<value>classpath:messages_ar</value>
</list>
</property>

<property name="defaultEncoding" value="UTF-8"/>
</bean>

<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
<prop key="velocimacro.library">org/springframework/web/servlet/view/velocity/spring.vm</prop>
</props>
</property>
</bean>
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/classes/com/spacerdv/mailTemplates"/>
</bean>


<!--

View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.

-->

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
<!-- if you want to use the Spring Velocity macros, set this property to true -->
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>

当尝试从属性文件中读取文本时,例如:

<span>#springMessage("hi.message")</span>

它不读取任何东西,或打印默认值,只打印:

$springMacroRequestContext.getMessage($code)

不知道为什么? ,我错过了什么吗?有什么帮助吗?

最佳答案

当使用速度引擎发送电子邮件时,您可能必须配置引擎以使用 spring 中提供的 velocimacro 库。

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
<prop key="velocimacro.library">org/springframework/web/servlet/view/velocity/spring.vm</prop>
</props>
</property>
</bean>

可以查看the example in spring documentation .

如果 Spring 没有自动将 $springMacroRequestContext 变量注入(inject)到您的模型中,您应该自己放置它:

model.put("springMacroRequestContext", new RequestContext(request, response, getServletContext(), model));

这基本上就是他们在 AbstractTemplateView 中所做的类(class)。我猜你做不到,因为你在这里处理电子邮件,而不是网络请求。但这绝对暗示了您可以做些什么来让它发挥作用。

关于spring - 无法在电子邮件模板中快速使用宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4324739/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com