Java中Velocity快速對變量中的引號特殊字符進行轉義
簡介
Velocity是一個基于Java的模板引擎,與Freemarker類似。相較于Freemarker更輕量,但帶來的問題就是功能不如Freemarker強大,所以實際項目中可能會更傾向于用Freemarker,這里不作過多介紹了,本文主要記錄一下在使用中碰到的要對引號特殊字符進行轉義的問題。
問題背景
項目應用中使用了Velocity,但是其中的一個模板在執(zhí)行時會報錯,模板如下:
["${content}",${scene_id}]
當content的值中含有特殊字符時,由于本身是List格式,在將變量替換后,會因為變量中的特殊字符導致轉換JSON報錯。比如
String content = "etsl\"hesaid.\"iathisis";
執(zhí)行后會報錯:
Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, pos 8, json : ["etsl"hesaid."iathisis",59]
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1436)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1322)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:1206)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:1111)
at com.alibaba.fastjson.JSON.parseArray(JSON.java:508)
由于Velocity功能不夠強大,不能像Freemarker那樣用自帶的函數對特殊字符進行處理。
解決方法
后來經過查找資料,了解到Velocity有拓展工具類(org.apache.velocity.tools.generic.EscapeTool),查看EscapeTool類的源碼,可以發(fā)現該類中包含了很多工具方法,比如針對html,js等語言,也有對應的轉義方法。當前問題可以使用其中的org.apache.velocity.tools.generic.EscapeTool#java方法來解決。
引入依賴:
<!-- https://mvnrepository.com/artifact/org.apache.velocity.tools/velocity-tools-generic --> <dependency> <groupId>org.apache.velocity.tools</groupId> <artifactId>velocity-tools-generic</artifactId> <version>3.1</version> </dependency>
將template修改為:
// 創(chuàng)建VelocityContext對象
VelocityContext context = new VelocityContext();
// 向VelocityContext中添加變量
context.put("content", content);
context.put("scene_id", 59);
// 添加自定義工具類
context.put("esc", new EscapeTool());并且在創(chuàng)建VelocityContext對象時,將工具類加載進去:
// 創(chuàng)建VelocityContext對象
VelocityContext context = new VelocityContext();
// 向VelocityContext中添加變量
context.put("content", content);
context.put("scene_id", 59);
// 添加自定義工具類
context.put("esc", new EscapeTool());問題得到解決。
完整代碼如下:
public static void main(String[] args) throws Exception {
String template = "[\"$esc.java(${content})\",${scene_id}]";
String content = "etsl\"hesaid.\"iathisis";
System.out.println(content);
// 初始化Velocity引擎
Velocity.init();
// 創(chuàng)建VelocityContext對象
VelocityContext context = new VelocityContext();
// 向VelocityContext中添加變量
context.put("content", content);
context.put("scene_id", 59);
// 添加自定義工具類
context.put("esc", new EscapeTool());
// 合并模板和VelocityContext
StringWriter writer = new StringWriter();
Velocity.evaluate(context, writer, "Velocity Example", template);
// 輸出結果
String params = writer.toString();
System.out.println(params);
JSONArray jsonArray = JSONObject.parseArray(params);
System.out.println(jsonArray);
}到此這篇關于Java中Velocity如何對變量中的引號特殊字符進行轉義的文章就介紹到這了,更多相關Velocity特殊字符轉義內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot整合mybatis-plus 實現分頁查詢功能
這篇文章主要介紹了springboot整合mybatis-plus 實現分頁查詢功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Spring?IOC容器基于XML外部屬性文件的Bean管理
這篇文章主要為大家介紹了Spring?IOC容器Bean管理XML外部屬性文件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
springboot?log4j2.xml如何讀取application.yml中屬性值
這篇文章主要介紹了springboot?log4j2.xml如何讀取application.yml中屬性值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Mybatis-plus批量去重插入ON DUPLICATE key update使用方式
這篇文章主要介紹了Mybatis-plus批量去重插入ON DUPLICATE key update使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

