如何通过h:outputScript包含JavaScript文件? [英] How to include JavaScript files by h:outputScript?

查看:141
本文介绍了如何通过h:outputScript包含JavaScript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将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页面中

<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:

如果标记具有类型"属性,则该属性应等于文本/javascript"或应用程序/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.

推荐答案

<h:outputScript>(和<h:outputStylesheet>)从/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)

您的<script>方法失败可能是由于使用了错误的相对路径引起的.您需要认识到资源是相对于当前请求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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆