

模板引擎Velocity学习系列 - #set指令
source link: https://www.tuicool.com/articles/ymayUnm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#set指令
#set指令用于向一个变量或者对象赋值。
格式: #set($var = value)
LHS是一个变量,不要使用特殊字符例如英文句号等,不能用大括号括起来。测试发现#set($user.name = 'zhangsan'),#set(${age} = 18)均赋值失败。
RHS可以是变量、字符串字面值、数字字面值、方法、ArrayList、Map、表达式。
测试案例
User对象类
public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
测试类TestVelocity
public class TestVelocity { public static void main(String[] args) { VelocityEngine engine = new VelocityEngine(); // 初始化VelocityEngine engine.setProperty("resource.loader", "file"); engine.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader"); engine.setProperty("input.encoding", "utf8"); engine.setProperty("output.encoding", "utf8"); engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "D:\\conf"); engine.init(); Template template = engine.getTemplate("hellovelocity.vm"); VelocityContext ctx = new VelocityContext(); ctx.put("user", new User("zhangsan", 18)); StringWriter stringWriter = new StringWriter(); template.merge(ctx, stringWriter); System.out.println(stringWriter.toString()); } }
测试模板文件 hellovelocity.vm
#set($name = 'john') #set($age = 18) #set($array = ['a', 'b', 'c']) #set($map = {'a' : 'a', 'b' : 'b', 'c' : 'c'}) #set($userName = "$!{user.getName()}") #set($userAge = "$!{user.getAge()}") #set($userTest1 = $user.getAge_()) #set($userTest2 = "$!{user.getAge_()}") $name $age $array.get(0) $array.get(1) $array.get(2) $map.get('a') $map.get('b') $map.get('c') $userName $userAge $userTest1 $userTest2
输出结果
john 18 a b c a b c zhangsan 18 $userTest1
说明:
#set($userTest1 = $user.getAge_())
#set($userTest2 = "$!{user.getAge_()}")
右边$!{user.getAge_()}表达式计算失败情况下,放在双引号里面,赋值空串,放在外面将不会赋值。
在使用#set时,字符串的字面值如果放在双引号里,将会被解析,放在单引号里面,将会被当做字面量。
#set($test1 = "$userName") #set($test2 = '$userName') $test1=字符串zhangsan,$test2=字符串$userName。
Recommend
-
41
接上文For循环和其它编程语言一样,for用来编辑列表中的项。下面以一个例子来说明for在flask的jinjia2模板中的使用。创建一个模板list.html代码如下:{% extends "base.html" %}{% block content %} {% for user in users %} <li>{{ user }}</li&...
-
93
接上文宏可以理解为函数,即把一些常用的模板片段做好封装,以便于重用,减少工作量和维护难度。宏的定义很简单:{%macro xxx()%}##这里写内容{%endmacro%}下面引用官方的一个例子进行说明:{% macro input(name, value='', type='text', size=20) -%}<input ty...
-
47
README.md Poi-tl(Poi-template-language)
-
78
无论是前端的 MVVM 框架, 还是像我们常用的如 ejs 这样的模板引擎, 都是为了组件化工作的, 是为了减少重复编写某些代码而出现的.实际上模板引擎是为了实现模板文件与业务数据的结合, 实现界面与数据的分离. 为了能够深刻理解模板引擎的工作原理, 这...
-
40
-
69
-
26
Flask 和 Django,以及其它很多 Python 框架如 Ansible,都默认使用 Jinja2 来作为模版引擎。我们用 Jinja2 在服务器上直接生成配置和其他文件。 Jinja 是一个基于 Python 设计语言的“全功能模板引擎”,个人认为 Jinja 语法本身并不复杂,...
-
6
从 Velocity 到 Thymeleaf:浅谈模板迁移 欧雷 发表于 4 年之前 2 条评论 标签:
-
6
真有人试了?开发者将ChatGPT与Unity引擎融合,输指令做游戏 2023-03-22 •
-
6
凯迪拉克预告“Opulent Velocity”电动V系列概念车2024/03/14 16:14|By
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK