欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中Velocity快速對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義

 更新時(shí)間:2023年07月24日 11:16:02   作者:Sword_Shi  
Velocity是一個(gè)基于Java的模板引擎,與Freemarker類似,這篇文章主要介紹了Java中Velocity如何對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義,主要記錄一下在使用中碰到的要對(duì)引號(hào)特殊字符進(jìn)行轉(zhuǎn)義的問(wèn)題,需要的朋友可以參考下

簡(jiǎn)介

Velocity是一個(gè)基于Java的模板引擎,與Freemarker類似。相較于Freemarker更輕量,但帶來(lái)的問(wèn)題就是功能不如Freemarker強(qiáng)大,所以實(shí)際項(xiàng)目中可能會(huì)更傾向于用Freemarker,這里不作過(guò)多介紹了,本文主要記錄一下在使用中碰到的要對(duì)引號(hào)特殊字符進(jìn)行轉(zhuǎn)義的問(wèn)題。

問(wè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)行處理。

解決方法

后來(lái)經(jīng)過(guò)查找資料,了解到Velocity有拓展工具類(org.apache.velocity.tools.generic.EscapeTool),查看EscapeTool類的源碼,可以發(fā)現(xiàn)該類中包含了很多工具方法,比如針對(duì)html,js等語(yǔ)言,也有對(duì)應(yīng)的轉(zhuǎn)義方法。當(dāng)前問(wèn)題可以使用其中的org.apache.velocity.tools.generic.EscapeTool#java方法來(lái)解決。

引入依賴:

<!-- 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());

問(wèn)題得到解決。

完整代碼如下:

    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)文章

最新評(píng)論