Velocity 读取字符串模板生成代码
2023-09-11 14:21:14 时间
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/49428277 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
1,遇到问题
之前使用 freeMarker 开发 cms系统,生成html。
后来页面不用jsp,开发了,换成velocity展示了
SpringBoot 项目,如何优雅的把接口参数中的空白值替换为 null 值? 查询的方法是Get方法: 前端是通过url加参数传过来的,如果有一个参数值为空的时候,由于setEntity() 并不过滤空白,执行sql的时候 会把 作为参数去当做查询条件,查询就出现了问题:
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/49428277 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
之前使用 freeMarker 开发 cms系统,生成html。
后来页面不用jsp,开发了,换成velocity展示了。
想着生成页面也使用velocity。
但是发现读取文件的类库加载不进来。
参考官方网站例子:
http://velocity.apache.org/engine/devel/developer-guide.html
import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import java.io.StringWriter; import java.util.Date; public class CreateHtml { public static void main(String[] args) { Velocity.init(); /* lets make a Context and put data into it */ VelocityContext context = new VelocityContext(); context.put("name", "Velocity"); context.put("project", "Jakarta"); context.put("now", new Date()); /* lets make our own string to render */ String str = "We are using $project $name to render this. $now"; StringWriter stringWriter = new StringWriter(); Velocity.evaluate(context, stringWriter, "mystring", str); System.out.println(" string : " + stringWriter); }
读取文件从一个字符串读取模板,生成文件写到一个字符串里面。
读取文件的也不麻烦
Velocity.mergeTemplate(“testtemplate.vm”, context, w );
因为日期是Date,需要对时间进行格式化。
在web里面可以使用toolbox引入,但是在main函数里面不知道咋加载进去。
找了半天,其实非常简单,直接new一个对象就行。
import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import java.io.StringWriter; import java.util.Date; public class CreateHtml2 { public static void main(String[] args) {
context.put("now", new Date()); context.put("dateFormatUtils", new org.apache.commons.lang.time.DateFormatUtils()); /* lets make our own string to render */ String str = "We are using $project $name to render this. 中文测试 $!dateFormatUtils.format($!now,yyyy-MM-dd)"; StringWriter stringWriter = new StringWriter(); Velocity.evaluate(context, stringWriter, "mystring", str); System.out.println(" string : " + stringWriter); }
就一行:
context.put(“dateFormatUtils”, new org.apache.commons.lang.time.DateFormatUtils());
直接把新对象放入进去就可以使用格式化函数了。
$!dateFormatUtils.format($!now,yyyy-MM-dd)
SpringBoot 项目,如何优雅的把接口参数中的空白值替换为 null 值? 查询的方法是Get方法: 前端是通过url加参数传过来的,如果有一个参数值为空的时候,由于setEntity() 并不过滤空白,执行sql的时候 会把 作为参数去当做查询条件,查询就出现了问题:
相关文章
- Solidworks如何导入和使用模板文件
- pycharm中python模板代码自动生成
- AS 代码模板 文件模板 Templates MD
- 浅谈smarty模板的mvc框架
- eclipse常用功能:代码重构(变量批量重命名)、自定义注释、代码模板、打开断言
- Atitit velocity 模板引擎使用法 目录 1.1. 1.4 Context1 1.1.1. 1.4.1 Context 基本概念1 1.2. .3不同模式下使用velocity1
- Java注释模板设置详解
- 腾讯云微搭低代码-汽车体验小程序官方模板解析
- 【项目实战】SpringBoot整合Freemarker模板引擎实现Email邮件发送功能
- 数学建模学习(49):灰色预测案例二(案例+代码模板)
- Java通过ftl模板导出word最详细教程
- FreeMarker模板开发指南知识点梳理
- 在Eclipse中自定义类似syso的快捷代码模板
- Django(11)-模板层的过滤器和继承
- 【项目实战】IDEA中的代码模板,IDEA如何导入签名,设置类头注释和方法注释
- 【设计模式】模板方法模式--让你的代码更具灵活性与可扩展性
- Latex学习笔记(八)代码附录模板
- 【区块链技术开发】 Solidity使用Truffle Box工具实现预构建模板、自动化部署、创建智能合约示例代码