Java 如何使用 Spring Boot 正确配置 Velocity?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29503617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:07:24  来源:igfitidea点击:

How to properly configure Velocity with Spring Boot?

javaspringspring-bootvelocity

提问by wMattDodd

I'm new to Spring Boot, and I'm having major trouble with this. There's a short list of Velocity-related Spring Boot properties I can set in application.properties, and those are working fine. But there's a huge number of Velocity properties that I can't configure that way.

我是 Spring Boot 的新手,我遇到了大麻烦。我可以在 application.properties 中设置与 Velocity 相关的 Spring Boot 属性的简短列表,这些属性工作正常。但是我无法以这种方式配置大量的 Velocity 属性。

I found thisquestion, that seems to address what I need, but it isn't working for me. When I use breakpoints within Spring Boot during program start-up, I can see the "spring.velocity.properties.*" key/value pairs being read and loaded by Spring Boot correctly--they just don't seem to affect anything. No matter what value I set them to, the Velocity run-time behavior uses the default anyway.

我发现了这个问题,这似乎解决了我的需要,但对我不起作用。当我在程序启动期间在 Spring Boot 中使用断点时,我可以看到 Spring Boot 正确读取和加载了“spring.velocity.properties.*”键/值对——它们似乎没有影响任何东西。无论我将它们设置为什么值,Velocity 运行时行为都使用默认值。

What am I missing?

我错过了什么?

EDIT:

编辑:

pom.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>docuvore</groupId>
<artifactId>docuvore-server</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.BUILD-SNAPSHOT</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <!-- Core Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- Tomcat and Spring Web MVC -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Security 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    -->

    <!-- Spring Data and MongoDB -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <!-- Apache Velocity --> 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-velocity</artifactId>
    </dependency>

    <!-- Project Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.2</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<!-- Additional lines to be added here... -->

<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>
</project>

application.properties

应用程序属性

logging.path=/logs
#spring.velocity.resourceLoaderPath = /templates/
#spring.velocity.checkTemplateLocation=false
spring.velocity.properties.template.provide.scope.control = true
spring.velocity.properties.directive.parse.max.depth = 9
spring.velocity.properties.runtime.log = C:/logs/velocity.log

Application.java

应用程序.java

package com.github.docuvore.prototype;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration 
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

ExampleVelocityController.java

示例VelocityController.java

package com.github.docuvore.prototype.examples;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleVelocityController {

    @RequestMapping("/exampleVelocity")
    String home() {
        String result = null;

        VelocityEngine velocity = new VelocityEngine();
        velocity.init();
        Template template = velocity.getTemplate("src/main/resources/templates/general/htmlElement.vm");

        VelocityContext context = new VelocityContext();
        context.put("title", "Apache Velocity");

        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        result = writer.toString();

        return result;
    }
}

htmlElement.vm

htmlElement.vm

<html>
#parse ("src/main/resources/templates/general/bodyElement.vm")
</html>

bodyElement.vm

bodyElement.vm

<body>
#parse ("src/main/resources/templates/general/bodyElement.vm")
</body>

回答by Eddú Meléndez

You should put your breakpoint at VelocityAutoConfigurationto ensure that Velocity is used.

您应该将断点放在VelocityAutoConfiguration以确保使用 Velocity。

Take a look at spring-boot-sample-velocity. Check the dependencies.

看看spring-boot-sample-velocity。检查依赖项。