【问题标题】:JSF facelet page doesn't javascript string with '&' characterJSF facelet 页面没有带有“&”字符的 javascript 字符串
【发布时间】:2011-11-01 07:51:56
【问题描述】:

在 JSF facelet 页面 (.xhtml) 我有这个 javascript 代码

<script type="text/javascript">
        function navigateToDetail() {
            var id = document.getElementById("idElemento").value;
            alert(id);
            var isPratica = document.getElementById("isPratica").value;
            alert(isPratica);
            var box = "#{boxCtrl.idBox}";
            alert(box);             
            if (isPratica==true)
                window.location = "DettaglioRichiesta.xhtml?id=" + id + "&box=" + box;
            else
                window.location = "../Richieste/DettaglioRichiesta.xhtml?id=" + id + "&box=" + box;

        }
    </script>

它不起作用,因为 jfs 引擎认为“&box”是相对于绑定的,它说:

Error Parsing /Box/ListaRichieste.xhtml: Error Traced[line: 20] The reference to entity "box" must end with the ';' delimiter

我可以避免这种行为吗?

【问题讨论】:

    标签: javascript jsf facelets


    【解决方案1】:

    Facelets 是一种基于 XML 的视图技术。 &amp;amp;XML special character。它被解释为像&amp;nbsp;&amp;#160; 等XML 实体的开始。因此它正在寻找结束字符;,但没有找到,所以它抛出了这个错误。

    要在 XML 文档中逐字表示 &amp;amp;,您需要使用 &amp;amp; 而不是 &amp;amp;

    window.location = "DettaglioRichiesta.xhtml?id=" + id + "&amp;box=" + box;
    

    您也可以将该 JS 代码放在它自己的 .js 文件中,该文件由 &lt;script src&gt; 包含,这样您就无需在 JS 代码中摆弄 XML 特殊字符。

    <script type="text/javascript" src="your.js"></script>
    

    【讨论】:

    • 感谢我知道外部 js 文件,但我很想在“页面内”解决它。感谢您的解决方案
    猜你喜欢
    • 2013-12-21
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    相关资源
    最近更新 更多