速度模板和Java字符串

时间:2015-10-10 17:04:31

标签: java highcharts jira velocity

我开发了jira report plugin.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类别中显示:[&#39; W1 &#39;]错误'Uncaught SyntaxError:Unexpected token&amp;'当然不会画画。

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

1 个答案:

答案 0 :(得分:1)

显然,'被编码为&#39;

见这个帖子:

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

WithHtml附加到变量名称会阻止HTML转义

你可以这样做:

#set($horizontalAxisWithHtml = $horizontalAxis)

然后使用$horizontalAxisWithHtml