java - Velocity 模板和 Java 字符串

标签 java highcharts jira velocity

我开发了 jira 报告插件。Velocity 模板在 java 字符串中显示 ascii 代码单引号或双引号。

Java 代码:

主要:

    Map velocityParams = new HashMap();
    String horizontalAxis = convertKeysToWeekCategories(bugtrends.keySet());

    velocityParams.put("horizontalAxis", horizontalAxis);

convertKeysToWeekCategories 方法:

    StringBuilder build = new StringBuilder();

    for (Integer integer : keySet) {
        build.append("\'");
        build.append("W");
        build.append(integer.toString());
        build.append("\'");
        build.append(",");
    }

    String result = build.toString();

    result = result.substring(0, result.length() - 1);

    return result;

}

我用 HighChart api 画画。

bugstrendview.vm:

<head >
    <meta charset="utf-8">
</head>

<script>AJS.$(document).ready(function(){ 


    chart = new Highcharts.Chart({

        chart: {
            type: 'spline',
            renderTo: 'container'
        },
        exporting: {
            enabled: true,
            filename: "open-bugs-trend-chart"
        },
        title: {
            text: 'Open Bugs Trend',
            x: -20 //center
        },
        subtitle: {
            text: 'results are displayed in weeks',
            x: -20 //center
        },
        xAxis: {
            categories: [$horizontalAxis],
            labels: {
                rotation: -90,
                align: 'right',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },

        tooltip: {

        formatter: function() {
            var myDates = $tooltipDates;
            return "<b>W"+(this.point.x+1)+"</b><br/>"+
            myDates[this.point.x]+" - "+myDates[this.point.x + 1]+
            "<br/>"+"Total Bugs : "+this.point.y ;

        },
    },


        yAxis: {
            gridLineWidth: 1,
            minorGridLineWidth: 1,
            title: {
                text: 'Number of Open Bugs'
            },
            min: 0,
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        plotOptions: {
            spline: {
                dataLabels: {
                    enabled: true
                },
                marker: {
                    enable: true
                }
            }
        },
        series: [
        {
            showInLegend: false,
            data: [$verticalAxis]
        }]
    });        });

</script>

然后在 bugstrendview.vm 类别中显示:['W1'] 作为错误 'Uncaught SyntaxError: Unexpected token &' 当然不要画画。

为什么显示单引号的ascii码?

最佳答案

显然 ' 被编码为 '

查看此主题:

https://answers.atlassian.com/questions/24351/how-to-prevent-velocity-escape-html

WithHtml 附加到变量名应该可以防止 HTML 转义

你可以这样做:

#set($horizontalAxisWithHtml = $horizontalAxis)

然后使用$horizo​​ntalAxisWithHtml

关于java - Velocity 模板和 Java 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056618/

相关文章:

java - 我可以使用 Java 来运行 .jasper 文件,而不必即时编译 jrxml 吗?

java - 无法编译 Atmosphere GWT 客户端

java - 如何通过方法调用(ClassCastException)从对象中转换类

javascript - HighCharts - 在 xAxis 中显示最后的标签

gradle - 无法找到zephyr的gradle依赖项-ZFJCloudRestClient

jira - 将某些 JIRA 开发人员限制在 JIRA 4.4 中的单个项目

java - 检索对象期间运行时异常

asp.net - 图表处理程序配置中的临时图像目录无效

javascript - 在 highcharts-react-official 上定义和应用主题

java - 如何解决 AsynchronousJiraRestClientFactory 的 java.lang.NoClassDefFoundError?