velocity模板引擎,读取不到模板请看如下格式即可!!

public static void main(String[] args) throws IOException {
		
		VelocityEngine velocity = new VelocityEngine();
		
		Properties properties = new Properties();
 
		// 指定字符集
		properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
		properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		
		String basePath = "templates";//路径为.vm文件所在的文件夹名称!!!此处文件名为templates
		// 设置模板的路径
		properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
		// 初始化velocity 让设置的路径生效
		velocity.init(properties);
		// 获取模版
		Template template = velocity.getTemplate("first.vm");
		// 创建context
		VelocityContext context = new VelocityContext();
		// 添加数据
		context.put("name", new String("Velocity"));
		// Merge 模版和context
		StringWriter writer = new StringWriter();
		template.merge(context, writer);
		//渲染到控制台
		System.out.println(writer.toString());
		writer.close();
 
	}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要引入 Apache Velocity 的相关依赖。在 Maven 项目中,可以在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3.1</version> </dependency> ``` 然后,可以编写一个 Velocity 模板文件,用来生成 `pom.xml` 文件。例如: ```xml <?xml version="1.0" encoding="UTF-8"?> <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>$groupId</groupId> <artifactId>$artifactId</artifactId> <version>$version</version> <dependencies> #foreach($dependency in $dependencies) <dependency> <groupId>${dependency.groupId}</groupId> <artifactId>${dependency.artifactId}</artifactId> <version>${dependency.version}</version> </dependency> #end </dependencies> </project> ``` 在模板文件中,可以使用 Velocity 的语法,通过变量和循环生成 `pom.xml` 文件的内容。例如,可以定义 `$groupId`、`$artifactId`、`$version` 和 `$dependencies` 等变量,分别表示项目的组ID、项目ID、版本号和依赖列表。在 `$dependencies` 变量中,使用 `#foreach` 循环遍历依赖列表,生成多个 `<dependency>` 标签。 最后,可以编写一个 Java 类,使用 Velocity 引擎加载模板文件,并传入变量值,生成 `pom.xml` 文件。例如: ```java import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class PomGenerator { public static void main(String[] args) throws Exception { // 初始化 Velocity 引擎 VelocityEngine ve = new VelocityEngine(); ve.init(); // 加载模板文件 Template t = ve.getTemplate("pom_template.vm"); // 设置变量值 VelocityContext ctx = new VelocityContext(); ctx.put("groupId", "com.example"); ctx.put("artifactId", "example-project"); ctx.put("version", "1.0.0"); List<Dependency> dependencies = new ArrayList<>(); dependencies.add(new Dependency("org.springframework", "spring-core", "5.2.0.RELEASE")); dependencies.add(new Dependency("org.springframework", "spring-web", "5.2.0.RELEASE")); ctx.put("dependencies", dependencies); // 生成输出文件 FileWriter writer = new FileWriter(new File("pom.xml")); t.merge(ctx, writer); writer.close(); } static class Dependency { String groupId; String artifactId; String version; Dependency(String groupId, String artifactId, String version) { this.groupId = groupId; this.artifactId = artifactId; this.version = version; } } } ``` 在代码中,首先初始化 Velocity 引擎,然后加载模板文件 `pom_template.vm`。接着,设置变量值,包括 `$groupId`、`$artifactId`、`$version` 和 `$dependencies` 等。最后,使用 Velocity 引擎的 `merge` 方法,将模板和变量合并,并将输出写入文件。这样就可以生成 `pom.xml` 文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值