根据this的评论,Spring WebFlux支持spring.freemarker.request-context-attribute
,实际上它工作正常。但是现在,我必须提供我的DelegatingWebFluxConfiguration
扩展。根据official guide和post,我只按如下方式扩展了DelegatingWebFluxConfiguration
类:
@Configuration
public class MyDelegatingWebFluxConfiguration extends DelegatingWebFluxConfiguration {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.freeMarker();
}
}
然后,除了request-context-attribute
之外,一切看起来都很好,因为我得到了一个例外:
freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> request [in template "index.ftl" at line 8, column 20]
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
FTL stack trace ("~" means nesting-related):
- Failed at: ${request.contextPath} [in template "index.ftl" at line 8, column 18]
其中request
的引用就是spring.freemarker.request-context-attribute
的值。
那么,有没有人可以帮忙处理这个案子呢?
发布于 2018-08-17 19:55:01
根据Spring Framework参考,你只需要有一个实现WebFluxConfigurer
的配置类(你不应该在它上面有@EnableWebFlux
,as it will disable Spring Boot auto-configuration)。
扩展DelegatingWebFluxConfiguration
与在配置类上扩展@EnableWebFlux
相同,它将告诉Spring Boot您希望完全控制WebFlux基础设施设置。
你不需要自己设置Freemarker,Spring Boot会帮你设置,你只需要在你的application.properties
中进行配置即可。
使用WebFluxConfigurer
应该可以解决这里的问题。
https://stackoverflow.com/questions/51833615
复制