“>”的奇怪问题和“<”被转换为“<”和“>>”在我的 jsf 应用程序中的 javascript 中

发布于 2024-11-18 16:43:54 字数 7875 浏览 1 评论 0 原文

我有一些 javascript 试图在 jsf 应用程序中使用。我已经缩小了范围,这样如果我取出下面的行,一切都会正常,但是当我输入这些行时,我会在 chrome 控制台中收到一个错误,显示“意外;”它显示第一行为 if(maxdays &gt; 1000) {

为什么它将大于符号转换为 &gt;

if(maxdays > 1000) {
    maxdays = 1000;
}

编辑: 这是整个 JSF 页面。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">


<h:head>


    <title>Protocol Dashboard</title>
    <link type="text/css" rel="stylesheet" href="../css/styles.css" />
    <script type="text/javascript" src="../js/jquery-1.5.2.js"></script>
    <script type="text/javascript" src="../js/highcharts.src.js"></script>
    <script type="text/javascript">
        $(document).ready(
                function() {
                    //var chartData;
                    //var goodData;
                    var chart;
                    var studyType;
                    var categories;
                    var maxdays;
                    //var chart = new Highcharts.Chart({
                    var options = {

                        chart : {
                            renderTo : 'container',
                            type : 'bar'
                        },

                        title : {
                            text : 'Gantt module development plan'
                        },
                        subtitle: {
                            text: studyType
                        },
                        xAxis : {
                            categories : [ 'Planning', 'Development',
                                    'Testing', 'Documentation' ]
                        },

                        yAxis : {
                            type : 'datetime',
                            min : Date.UTC(2012, 0, 1),
                            labels : {
                                formatter : function() {
                                    return Highcharts.dateFormat('%m/%d/%Y',
                                            this.value);
                                }
                            }

                        },

                        tooltip : {
                            formatter : function() {
                                var point = this.point;
                                return '<b>'
                                        + point.category
                                        + '</b><br/>'
                                        + Highcharts.dateFormat('%b %e, %Y',
                                                point.low)
                                        + ' - '
                                        + Highcharts.dateFormat('%b %e, %Y',
                                                point.y);
                            }
                        },

                        series : [ {
                            data : [ {
                                low : Date.UTC(2012, 0, 1),
                                y : Date.UTC(2012, 0, 15)
                            }, {
                                low : Date.UTC(2012, 0, 10),
                                y : Date.UTC(2012, 4, 28)
                            }, {
                                low : Date.UTC(2012, 3, 15),
                                y : Date.UTC(2012, 4, 28)
                            }, {
                                low : Date.UTC(2012, 4, 15),
                                y : Date.UTC(2012, 4, 28)
                            } ]
                        }, {
                            data : [ {
                                low : Date.UTC(2012, 0, 1),
                                y : Date.UTC(2012, 1, 15)
                            }, {
                                low : Date.UTC(2012, 0, 10),
                                y : Date.UTC(2012, 3, 15)
                            }, {
                                low : Date.UTC(2012, 3, 15),
                                y : Date.UTC(2012, 6, 14)
                            }, {
                                low : Date.UTC(2012, 4, 15),
                                y : Date.UTC(2012, 4, 25)
                            } ]
                        } ]

                    };

                    function loadData() {

                        //options.series.length = 0;
                        //options.xAxis.categories.length = 0;

                        $('#hiddenData').val('09-034,99|10-053,194');
                        $('#hiddenCategories').val('09-034|10-053');
                        $('#clickedMenu').val('P2C');
                        $('#hiddenMaxDays').val('194');



                        var chartData = $('#hiddenData').val();
                        console.log('hiddenData = '+chartData);

                        categories = $('#hiddenCategories').val();
                        console.log('hiddenCategories = '+categories);

                        studyType = $('#clickedMenu').val();
                        console.log('clickedMenu = '+studyType);

                        maxdays = $('#hiddenMaxDays').val();
                        console.log('hiddenMaxDays = '+maxdays);
                        console.log('maxdays = '+maxdays);



                        //options.yAxis.max = maxdays;

                        options.subtitle.text = studyType;

                        var goodData = chartData.split('|');
                        //console.log('goodData = '+goodData);

                        //var goodCategories = categories.split(",");
                        //console.log('goodCategories = '+goodCategories);
                        /*
                        var series = {

                            data : []
                        };

                        var cat = {
                            categories : []
                        };

                        try {
                            $.each(goodData, function(index, value) {
                                var goodData2 = value.split(",");
                                //series.name = goodData2[0];
                                series.data.push(parseFloat(goodData2[1]));

                            });

                            $.each(goodCategories, function(index, value) {
                                var prot = value;
                                options.xAxis.categories.push(value);

                            });

                            options.series.push(series);
                            //console.log(options);
                        } catch (err) {
                            //console.log("ERROR ..." + err.description + '  message:'
                            //+ err.message);

                        }*/

                    }

                    function loadDataAndCreateChart() {
                        loadData();
                        //console.log(options);
                        chart = new Highcharts.Chart(options);
                    }

                    loadDataAndCreateChart();

                });
    </script>

</h:head>
<h:body>

    <div id="container"></div>

    <h:inputHidden value="#{selectCategory.jsonResults }" id="hiddenData" />
    <h:inputHidden value="#{selectCategory.jsonResultsCategories }" id="hiddenCategories" />
    <h:inputHidden value="#{selectCategory.menuItem}" id="clickedMenu" />
    <h:inputHidden value="#{selectCategory.maxDays}" id="hiddenMaxDays" />

</h:body>
</html>

I have some javascript that i'm trying to use in a jsf app. I've narrowed it down so that if I take out the lines below, things work fine, but when I have these lines in, I get an error in the chrome console which says "unexpected ;" and it shows the first line as if(maxdays > 1000) {

Why is it converting the greater than symbol to >?

if(maxdays > 1000) {
    maxdays = 1000;
}

EDIT:
Here's the entire JSF page.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">


<h:head>


    <title>Protocol Dashboard</title>
    <link type="text/css" rel="stylesheet" href="../css/styles.css" />
    <script type="text/javascript" src="../js/jquery-1.5.2.js"></script>
    <script type="text/javascript" src="../js/highcharts.src.js"></script>
    <script type="text/javascript">
        $(document).ready(
                function() {
                    //var chartData;
                    //var goodData;
                    var chart;
                    var studyType;
                    var categories;
                    var maxdays;
                    //var chart = new Highcharts.Chart({
                    var options = {

                        chart : {
                            renderTo : 'container',
                            type : 'bar'
                        },

                        title : {
                            text : 'Gantt module development plan'
                        },
                        subtitle: {
                            text: studyType
                        },
                        xAxis : {
                            categories : [ 'Planning', 'Development',
                                    'Testing', 'Documentation' ]
                        },

                        yAxis : {
                            type : 'datetime',
                            min : Date.UTC(2012, 0, 1),
                            labels : {
                                formatter : function() {
                                    return Highcharts.dateFormat('%m/%d/%Y',
                                            this.value);
                                }
                            }

                        },

                        tooltip : {
                            formatter : function() {
                                var point = this.point;
                                return '<b>'
                                        + point.category
                                        + '</b><br/>'
                                        + Highcharts.dateFormat('%b %e, %Y',
                                                point.low)
                                        + ' - '
                                        + Highcharts.dateFormat('%b %e, %Y',
                                                point.y);
                            }
                        },

                        series : [ {
                            data : [ {
                                low : Date.UTC(2012, 0, 1),
                                y : Date.UTC(2012, 0, 15)
                            }, {
                                low : Date.UTC(2012, 0, 10),
                                y : Date.UTC(2012, 4, 28)
                            }, {
                                low : Date.UTC(2012, 3, 15),
                                y : Date.UTC(2012, 4, 28)
                            }, {
                                low : Date.UTC(2012, 4, 15),
                                y : Date.UTC(2012, 4, 28)
                            } ]
                        }, {
                            data : [ {
                                low : Date.UTC(2012, 0, 1),
                                y : Date.UTC(2012, 1, 15)
                            }, {
                                low : Date.UTC(2012, 0, 10),
                                y : Date.UTC(2012, 3, 15)
                            }, {
                                low : Date.UTC(2012, 3, 15),
                                y : Date.UTC(2012, 6, 14)
                            }, {
                                low : Date.UTC(2012, 4, 15),
                                y : Date.UTC(2012, 4, 25)
                            } ]
                        } ]

                    };

                    function loadData() {

                        //options.series.length = 0;
                        //options.xAxis.categories.length = 0;

                        $('#hiddenData').val('09-034,99|10-053,194');
                        $('#hiddenCategories').val('09-034|10-053');
                        $('#clickedMenu').val('P2C');
                        $('#hiddenMaxDays').val('194');



                        var chartData = $('#hiddenData').val();
                        console.log('hiddenData = '+chartData);

                        categories = $('#hiddenCategories').val();
                        console.log('hiddenCategories = '+categories);

                        studyType = $('#clickedMenu').val();
                        console.log('clickedMenu = '+studyType);

                        maxdays = $('#hiddenMaxDays').val();
                        console.log('hiddenMaxDays = '+maxdays);
                        console.log('maxdays = '+maxdays);



                        //options.yAxis.max = maxdays;

                        options.subtitle.text = studyType;

                        var goodData = chartData.split('|');
                        //console.log('goodData = '+goodData);

                        //var goodCategories = categories.split(",");
                        //console.log('goodCategories = '+goodCategories);
                        /*
                        var series = {

                            data : []
                        };

                        var cat = {
                            categories : []
                        };

                        try {
                            $.each(goodData, function(index, value) {
                                var goodData2 = value.split(",");
                                //series.name = goodData2[0];
                                series.data.push(parseFloat(goodData2[1]));

                            });

                            $.each(goodCategories, function(index, value) {
                                var prot = value;
                                options.xAxis.categories.push(value);

                            });

                            options.series.push(series);
                            //console.log(options);
                        } catch (err) {
                            //console.log("ERROR ..." + err.description + '  message:'
                            //+ err.message);

                        }*/

                    }

                    function loadDataAndCreateChart() {
                        loadData();
                        //console.log(options);
                        chart = new Highcharts.Chart(options);
                    }

                    loadDataAndCreateChart();

                });
    </script>

</h:head>
<h:body>

    <div id="container"></div>

    <h:inputHidden value="#{selectCategory.jsonResults }" id="hiddenData" />
    <h:inputHidden value="#{selectCategory.jsonResultsCategories }" id="hiddenCategories" />
    <h:inputHidden value="#{selectCategory.menuItem}" id="clickedMenu" />
    <h:inputHidden value="#{selectCategory.maxDays}" id="hiddenMaxDays" />

</h:body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素衣风尘叹 2024-11-25 16:43:54

您正在使用 Facelets,这是一种基于 XML 的视图技术。 JavaScript 运算符如 >& 等是

我强烈建议将所有 JS 代码放在自己的 .js 文件中,并通过

例如

<script src="#{request.contextPath}/js/global.js"></script>

<h:outputScript name="js/global.js" />

其他优点是,您可以通过这种方式微调浏览器缓存,并最终获得更高效服务的网站。


与具体问题无关:请注意我如何引用这些库。您应该更喜欢域相对路径而不是 URI 相对路径。使用 ../ 是一种维护上的痛苦。如果您移动视图或更改请求 URI,它就会中断。

You're using Facelets, which is a XML based view technology. JavaScript operators like >, &, etc are special characters in XML and are illegal when used for other purposes inside a XML file. Facelets is taking care of them this way.

I strongly recommend to put all that JS code in its own .js file and reference it by a <script src> or a <h:outputScript>.

E.g.

<script src="#{request.contextPath}/js/global.js"></script>

or

<h:outputScript name="js/global.js" />

Additional advantage is that you can finetune browser caching this way and end up in a more efficiently served website.


Unrelated to the concrete problem: please note how I referenced the libraries. You should prefer domain-relative paths over URI-relative paths. Using ../ is a maintenance pain. If you move the view or change the request URI, it'll break.

柏林苍穹下 2024-11-25 16:43:54

> 是 HTML 实体,必须用于输出 HTML 中的 > 字符。 <> 是 HTML 标记使用的特殊字符,因此必须转义为 <& gt; 如果这些字符必须显示在页面中。我怀疑您正在使用转义其输入的 JSF 标记。

我对 JSF 没有太多经验,但您可能会尝试将脚本包含在 CDATA 部分中:

<script type="text/javascript">
<![CDATA[
   your JavaScript code here
]]>
</script>

但最好的解决方案是根本不在 HTML 中使用 JavaScript,而是从外部文件加载它。

> is the HTML entity which must be used to output the > character in HTML. < and > are special characters used by the HTML markup and must thus be escaped to < and > if these characters must be displayed in the page. I suspect you're using a JSF tag which escapes its input.

I don't have much experience with JSF, but you might try to enclose the script in a CDATA section:

<script type="text/javascript">
<![CDATA[
   your JavaScript code here
]]>
</script>

But the best solution is not to have JavaScript in HTML at all, and load it from an external file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文