如何通过 h:outputScript 包含 JavaScript 文件?
我想使用 jQuery Validate 插件和 JSF 进行客户端表单验证。我发现导入资源有基本困难。
在我的 JSF 页面中,
<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 中的脚本选项卡时,我在下拉列表中看不到任何脚本文件。有一条消息显示:
如果标签有“type”属性,它应该等于“text/javascript”或“application/javascript”。此外,脚本必须是可解析的(语法正确)。
此外,我的 jquery 效果(如鼠标悬停、隐藏、显示等)不起作用。我尝试使用常用的脚本标签
<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 文件都放在
Web pages
|_ js
|_jquery-1.6.2.js,my.validate.js,jquery.validate.js,jquery.maskedinput.js
我尝试了 将 jQuery 与 JSF 2.0 的资源结合使用 但没有成功。
请建议我解决这个问题。我不想将 JSF 内置验证与 ajax 一起使用,因为我们将代码从 JSP 移至 JSF 并且已经编写了验证。我想重用我之前编写的现有 jQuery 验证。
I want to use jQuery Validate plugin with JSF for client side form validation. I am finding basic difficulty in importing the resources.
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>
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:
If tags have a "type" attribute, it should equal "text/javascript" or "application/javascript". Also scripts must be parsable (syntactically correct).
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>
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
I tried one of the solutions posted at Using jQuery with JSF 2.0's resource but had no success.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(和
)从/resources
文件夹加载资源。您需要将脚本放入该文件夹中。那么以下脚本声明应该可以工作:
(请注意,我省略了
library
属性,因为名称“js”并不表示 真正的库)方法失败可能是由于使用了不正确的相对路径造成的。您需要认识到资源是相对于当前请求 URL 进行解析的,而不是相对于它们在服务器端磁盘文件系统中的路径进行解析。
The
<h:outputScript>
(and<h:outputStylesheet>
) loads resources from/resources
folder. You need to put the scripts in that folder.Then the following script declarations should work:
(note that I omitted the
library
attribute, because the name "js" does not indicate a real library)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.