微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Spring 中使用 Apache Velocity

如何解决如何在 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 举报,一经查实,本站将立刻删除。