问题描述
作为这个问题的扩展,我正在尝试将 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¶m=value2¶m=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: Error Parsing/page.xhtml: Error Traced[line: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>
只要我添加 for (var i = 0; i 甚至
for (var i = 0; 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 were using <
as a JavaScript operator, not as an XML entity. This totally explains the XML parsing error you got:
元素的内容必须由格式良好的字符数据或标记组成.
本质上,您在错误的地方编写了 JavaScript 代码,即 XML 文档而不是 JS 文件,因此您应该相应地转义所有 XML 特殊字符.<
必须转义为 <
.
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 有效.
to make it XML-valid.
然而,这使得 JavaScript 代码更难阅读和维护.正如 Mozilla 开发者网络的优秀文档 Writing JavaScript for XHTML 所述,您应该将 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 解析器会将块的内容解释为普通的";字符数据而不是 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 特殊字符.另一个好处是,这让浏览器有机会缓存 JS 文件,从而使平均响应大小更小.
This way you don't need to worry about XML-special characters in your JS code. Additional advantage is that this gives the browser the opportunity to cache the JS file so that average response size is smaller.
- 实体名称必须立即遵循&"在实体引用中
- 是否可以将 JSF+Facelets 与HTML 4/5?
- 如何在 Facelets 模板中引用 CSS/JS/图像资源?一个>
- 为 XHTML 编写 JavaScript
这篇关于解析 XHTML 时出错:元素的内容必须由格式正确的字符数据或标记组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!