解析XHTML时出错:元素的内容必须由格式良好的字符数据或标记组成 [英] Error parsing XHTML: The content of elements must consist of well-formed character data or markup
问题描述
作为此问题的扩展,我正在尝试将Javascript插入< h:commandButton />
的 onclick
属性为 action
已经呈现了ajax表。
As an extension of this question, I'm trying to insert Javascript to a <h:commandButton />
's onclick
property as action
is already rendering an ajax table.
我想做什么:
获取所选项目列表框并将它们转换为要在JSF FileServlet
中使用的参数。即 para2 = value1& param = value2& param = value3
What I want to do:
Get the selected items in a list box and turn them into parameters to be used in a JSF FileServlet
. i.e. para2=value1¶m=value2¶m=value3
以下是我所拥有的:
<script type ="text/javascript">
function myScript() {
var box = document.getElementbyId('myForm:box');
var length = box.options.length;
var paramstring = "";
for (var i = 0; i < length; i++) {
if (i != (length - 1) {
if (box.options[i].selected) {
paramstring = paramstring + "param=" + box.options[i].value + "&";
}
} else {
paramstring = paramstring + "param=" + box.options[i].value;
}
}
if (document.getElementById('myForm:checkbox').checked) {
window.location='fileServlet? + paramstring;
}
}
</script>
加载页面时得到的结果:
javax.servlet.ServletException:错误解析/page.xhtml:错误跟踪[第15行]元素的内容必须包含格式良好的字符数据或标记。
What I get when page is loaded:
javax.servlet.ServletException: Error Parsing /page.xhtml: Error Traced[line:15] The content of elements must consist of well-formed character data or markup.
什么不会触发异常:
<script type ="text/javascript">
function myScript() {
var box = document.getElementbyId('myForm:box');
var length = box.options.length;
var paramstring = "";
if (document.getElementById('myForm:checkbox').checked) {
window.location='fileServlet? + paramstring;
}
}
</script>
我一加入(var i = 0; i< ; length; i ++)
甚至 for(var i = 0; i< 10; i ++)
页面无法加载。为什么它不喜欢for循环?
As soon as I add in for (var i = 0; i < length; i++)
or even for (var i = 0; i < 10; i++)
the page wouldn't load. Why does it not like the for loop?
推荐答案
Facelets是一种基于XML的视图技术,它使用XHTML + XML生成HTML输出。 XML有五个特殊字符,由XML解析器进行特殊处理:
Facelets is a XML based view technology which uses XHTML+XML to generate HTML output. XML has five special characters which has special treatment by the XML parser:
-
<
标签的开头。 -
>
标签的结尾。 -
属性值的开头和结尾。
-
'
备选方案属性值的开始和结束。 -
&
实体的开头(以结尾;
)。
<
the start of a tag.>
the end of a tag."
the start and end of an attribute value.'
the alternative start and end of an attribute value.&
the start of an entity (which ends with;
).
如果<
,XML解析器隐式查找标记名称和结束标记>
。但是,在您的特定情况下,您使用<
作为JavaScript运算符,而不是XML实体。这完全解释了你得到的XML解析错误:
In case of <
, the XML parser is implicitly looking for the tag name and the end tag >
. However, in your particular case, you was using <
as a JavaScript operator, not as an XML entity. This totally explains the XML parsing error you got:
元素的内容必须包含格式良好的字符数据或标记。
The content of elements must consist of well-formed character data or markup.
本质上,你在错误的地方编写JavaScript代码,而不是XML文档一个JS文件,所以你应该相应地转义所有XML特殊字符。必须将<
转义为& lt;
。
In essence, you're writing JavaScript code in the wrong place, a XML document instead of a JS file, so you should be escaping all XML special characters accordingly. The <
must be escaped as <
.
所以,基本上,
for (var i = 0; i < length; i++) {
必须成为
for (var i = 0; i < length; i++) {
它是XML有效的。
然而,这使得JavaScript代码更难以阅读和维护。正如Mozilla Developer Network的优秀文档为XHTML编写JavaScript 中所述,您应该放置JavaScript字符数据(CDATA)块中的代码。因此,在JSF术语中,这将是:
However, this makes the JavaScript code harder to read and maintain. As stated in Mozilla Developer Network's excellent document Writing JavaScript for XHTML, you should be placing the JavaScript code in a character data (CDATA) block. Thus, in JSF terms, that would be:
<h:outputScript>
<![CDATA[
// ...
]]>
</h:outputScript>
XML解析器将块的内容解释为普通的vanilla字符数据,而不是XML,因此解释XML特殊字符按原样。
The XML parser will interpret the block's contents as "plain vanilla" character data and not as XML and hence interpret the XML special characters "as-is".
但更好的是将JS代码放在自己的JS文件中,其中包含< script src>
,或者以JSF术语,< h:outputScript>
。
But, much better is to just put the JS code in its own JS file which you include by <script src>
, or in JSF terms, the <h:outputScript>
.
<h:outputScript name="functions.js" target="head" />
这样您就不必担心JS代码中的XML特殊字符。
This way you don't need to worry about XML-special characters in your JS code.
- 实体名称必须紧跟'&'在实体参考中
- 是否可以将JSF + Facelets与HTML 4/5一起使用?
- 如何在Facelets模板中引用CSS / JS /图像资源?
- 为XHTML编写JavaScript
- The entity name must immediately follow the '&' in the entity reference
- Is it possible to use JSF+Facelets with HTML 4/5?
- How to reference CSS / JS / image resource in Facelets template?
- Writing JavaScript for XHTML
这篇关于解析XHTML时出错:元素的内容必须由格式良好的字符数据或标记组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!