如何在 Spring 中使用 Apache Velocity

问题描述

标题几乎描述了我的问题,我不想手动渲染每个模板,但不知道如何让它们自动渲染。我使用的是 Spring Boot 版本,其中速度支持已经不存在了,而且我对来自 apache 的 spring-veLocity-support 包一无所知。

我试过了:

    @GetMapping("/test")
    fun test(context: VeLocityContext) : Template {
        context.put("test","Hello World!")
        
        return VeLocity.getTemplate("views/templates/index.vm")
    }

这是一个bean:

@Bean("veLocityEngine")
open fun getVeLocityBean() : VeLocityEnginefactorybean {
    val properties = Properties()

    if (System.getenv("IS_LOCAL").toBoolean()) {
        // load templates from file for instant-reload when developing
        properties.setProperty(RuntimeConstants.RESOURCE_LOADERS,"file")
        properties.setProperty(
            RuntimeConstants.FILE_RESOURCE_LOADER_PATH,"${System.getProperty("user.dir")}/api/src/main/resources/"
        )
    } else {
        // load templates from jar
        properties.setProperty(RuntimeConstants.RESOURCE_LOADERS,"class")
        properties.setProperty(
            "resource.loader.class.class","org.apache.veLocity.runtime.resource.loader.ClassPathResourceLoader"
        )
    }

    VeLocity.init(properties) // For manual rendering

    val veLocity = VeLocityEnginefactorybean()
    veLocity.setVeLocityProperties(properties)

    return veLocity
}

但是由于无限递归,这只是返回了一个(字面意思)无尽的 json 并抛出了一个 JsonMappingException

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)