如何通过 h:outputScript 包含 JavaScript 文件? [英] How to include JavaScript files by h:outputScript?
问题描述
我想将 jQuery Validate 插件与 JSF 一起用于客户端表单验证.我在导入资源时遇到了基本困难.
I want to use jQuery Validate plugin with JSF for client side form validation. I am finding basic difficulty in importing the resources.
在我的 JSF 页面中
In my JSF page I have
<h:outputScript library="js" name="jquery-1.6.2.js"></h:outputScript>
<h:outputScript library="js" name="jquery.validate.js"></h:outputScript>
<h:outputScript library="js" name="jquery.maskedinput.js"></h:outputScript>
<h:outputScript library="js" name="myapp.validate.js"></h:outputScript>
当我在 Firefox 中单击脚本选项卡时,在下拉列表中看不到任何脚本文件.有一条消息显示:
When I click on the script tab in the Firefox, I can't see any script files in the dropdown. There is a message shown:
如果标签有一个type"属性,它应该等于text/javascript"或application/javascript".此外,脚本必须是可解析的(语法正确).
If tags have a "type" attribute, it should equal "text/javascript" or "application/javascript". Also scripts must be parsable (syntactically correct).
此外,我的 jquery 效果如鼠标悬停、隐藏、显示等不起作用.我尝试使用通常的脚本标签
Futher my jquery effect like mouse hover, hide, show etc do not work. I tried with usual script tags
<script type="text/javascript" src="../js/jquery-1.6.2.js"></script>
<script type="text/javascript" src="../js/jquery.validate.js"></script>
<script type="text/javascript" src="../js/jquery.maskedinput.js"></script>
<script type="text/javascript" src="../js/myapp.validate.js"></script>
这没有用.它仍然无法找到我的 JS 文件.我所有的JS文件都放在
Which was of no use. Still it was not able to locate my JS files. All my JS files are placed under
Web pages
|_ js
|_jquery-1.6.2.js,my.validate.js,jquery.validate.js,jquery.maskedinput.js
我尝试了发布在 使用 jQuery 的解决方案之一使用 JSF 2.0 的资源,但没有成功.
I tried one of the solutions posted at Using jQuery with JSF 2.0's resource but had no success.
请建议我解决这个问题.我不想在 ajax 中使用 JSF 内置验证,因为我们将代码从 JSP 移到 JSF,并且验证已经编写.我想重用我之前编写的现有 jQuery 验证.
Kindly suggest me a solution for this. I don't want to use JSF builtin validation with ajax, because we moved the code from JSP to JSF and the validation is already written. I want to reuse the existing jQuery Validation which I previously wrote.
推荐答案
(和
)从/resources
文件夹.您需要将脚本放在该文件夹中.
The <h:outputScript>
(and <h:outputStylesheet>
) loads resources from /resources
folder. You need to put the scripts in that folder.
WebContent
|-- resources
| `-- js
| |-- jquery-1.6.2.js
| |-- myapp.validate.js
| |-- jquery.validate.js
| `-- jquery.maskedinput.js
|-- WEB-INF
:
那么以下脚本声明应该有效:
Then the following script declarations should work:
<h:outputScript name="js/jquery-1.6.2.js" />
<h:outputScript name="js/jquery.validate.js" />
<h:outputScript name="js/jquery.maskedinput.js" />
<h:outputScript name="js/myapp.validate.js" />
(请注意,我省略了 library
属性,因为名称js"并不表示 真正的图书馆)
(note that I omitted the library
attribute, because the name "js" does not indicate a real library)
您的 方法失败可能是由于使用了不正确的相对路径.您需要意识到资源是相对于当前请求 URL 而不是它们在服务器端磁盘文件系统中的路径来解析的.
That your <script>
approach failed is probably caused by using an incorrect relative path. You need to realize that resources are resolved relative to the current request URL and not to their path in the server side disk file system.
这篇关于如何通过 h:outputScript 包含 JavaScript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!