Java中Velocity快速對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義
簡介
Velocity是一個(gè)基于Java的模板引擎,與Freemarker類似。相較于Freemarker更輕量,但帶來的問題就是功能不如Freemarker強(qiáng)大,所以實(shí)際項(xiàng)目中可能會(huì)更傾向于用Freemarker,這里不作過多介紹了,本文主要記錄一下在使用中碰到的要對(duì)引號(hào)特殊字符進(jìn)行轉(zhuǎn)義的問題。
問題背景
項(xiàng)目應(yīng)用中使用了Velocity,但是其中的一個(gè)模板在執(zhí)行時(shí)會(huì)報(bào)錯(cuò),模板如下:
["${content}",${scene_id}]
當(dāng)content的值中含有特殊字符時(shí),由于本身是List格式,在將變量替換后,會(huì)因?yàn)樽兞恐械奶厥庾址麑?dǎo)致轉(zhuǎn)換JSON報(bào)錯(cuò)。比如
String content = "etsl\"hesaid.\"iathisis";
執(zhí)行后會(huì)報(bào)錯(cuò):
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功能不夠強(qiáng)大,不能像Freemarker那樣用自帶的函數(shù)對(duì)特殊字符進(jìn)行處理。
解決方法
后來經(jīng)過查找資料,了解到Velocity有拓展工具類(org.apache.velocity.tools.generic.EscapeTool),查看EscapeTool類的源碼,可以發(fā)現(xiàn)該類中包含了很多工具方法,比如針對(duì)html,js等語言,也有對(duì)應(yīng)的轉(zhuǎn)義方法。當(dāng)前問題可以使用其中的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對(duì)象
VelocityContext context = new VelocityContext();
// 向VelocityContext中添加變量
context.put("content", content);
context.put("scene_id", 59);
// 添加自定義工具類
context.put("esc", new EscapeTool());并且在創(chuàng)建VelocityContext對(duì)象時(shí),將工具類加載進(jìn)去:
// 創(chuàng)建VelocityContext對(duì)象
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對(duì)象
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);
// 輸出結(jié)果
String params = writer.toString();
System.out.println(params);
JSONArray jsonArray = JSONObject.parseArray(params);
System.out.println(jsonArray);
}到此這篇關(guān)于Java中Velocity如何對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義的文章就介紹到這了,更多相關(guān)Velocity特殊字符轉(zhuǎn)義內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能
這篇文章主要介紹了springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Spring?IOC容器基于XML外部屬性文件的Bean管理
這篇文章主要為大家介紹了Spring?IOC容器Bean管理XML外部屬性文件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
springboot?log4j2.xml如何讀取application.yml中屬性值
這篇文章主要介紹了springboot?log4j2.xml如何讀取application.yml中屬性值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
javaweb實(shí)現(xiàn)投票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了javaweb實(shí)現(xiàn)投票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Java list.remove( )方法注意事項(xiàng)
這篇文章主要介紹了Java list.remove( )方法注意事項(xiàng),非常簡單易懂,需要的朋友可以參考下2018-08-08
Mybatis-plus批量去重插入ON DUPLICATE key update使用方式
這篇文章主要介紹了Mybatis-plus批量去重插入ON DUPLICATE key update使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
MyBatisPlus3.4.3版自動(dòng)生成代碼的使用過程
這篇文章主要介紹了MyBatisPlus3.4.3版自動(dòng)生成代碼的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

