栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java模板引擎Velocity在eclipse中的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java模板引擎Velocity在eclipse中的使用

一、eclipse编译器安装velocity

1、eclipse中安装插件
在help -> Eclipse Marketplace  搜索-> velocity  安装veloeclipse 3.0.1

2、maven项目pom中配置velocity-jar


		
			org.apache.velocity
			velocity
			1.7
		

二、源码案例

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");
		properties.setProperty(Velocity.OUTPUT_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();

	}

此处为了演示路径问题

三、源码案例2,将vm文件渲染到HTML页面

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");
		properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");

		String basePath = "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"));
		
		//将list集合添加到context
		List list = new ArrayList();
		list.add("方便面");
		list.add("娃哈哈");
		list.add("大大泡泡糖");
		list.add("AD钙中钙");
		context.put("list", list);
		
        //输出到001.html文件所在路径
		FileOutputStream fos = new FileOutputStream("html/001.html");
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));

		// Merge 模版和context
		template.merge(context, writer);
		writer.close();
		System.out.println("渲染完成!");

	}

first.vm模板内容

##单行注释    #*多行注释*#

	获取后端的值:$name
	
	## #set用于变量赋值
	set赋值
	#set($a = 1)
	#set($b = 2)
	$a,$b
	#set( $list1 = ["a","b","c"] )
	$list1
	
	## #stop停止指令,即停止模板引擎的执行,可用于程序调试,注意不用能在#if和#foreach中使用
	此处停止
##	#stop
	
	## #if判断
	if判断
	#if($b<$a)
		$b
	#else
		$a
	#end
	
	
	## #foreach循环遍历
	foreach循环遍历
	#foreach($i in $list)
	$i
	#end
	
	## #include 指令导入本地文件,可包含多个,中间用逗号分隔
	本地文件info.txt
	#include("info.txt")
	
	## #macro 宏指令,可用于定义代码段,宏内还可以带参数
	##定义宏:#macro(t)
    #macro(t)
    yes
	#end
	使用宏
    #t()
	
	##定义带参数的宏
	#macro(y $p)
	$p
	#end
	使用带参数的宏
	#y("yes")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号