如何在 JSF 页面中包含外部 Javascript 文件

问题描述:

我想包含 jQuery UI 的 URL http://code.jquery.com/ui/1.9.0/jquery-ui.js 在我的 JSF 页面中.我看到很多问题都说应该使用 <h:outputScript> ,但在所有答案中我都没有看到有关如何包含 URL 的示例.仅当 js 文件存在于项目的某个文件夹中时,答案才相关.

I want to include the URL of jQuery UI http://code.jquery.com/ui/1.9.0/jquery-ui.js in my JSF page. I saw many questions which said <h:outputScript> should be used, but in none of the answers I saw an example as to how to include an URL. The answers were only relevant if the js file is present in some folder of the project.

有人可以帮我把它包含在我的页面中吗?

Can someone please help where I have to include it in my page?

只需使用普通的 HTML 元素即可.

Just use plain HTML <script> element the usual way.

<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>

只能引用本地脚本.您只会错过它的动态版本控制、模块化和重定位优势,但这在技术上应该不会对静态和外部脚本造成损害.

The <h:outputScript> can only refer local scripts. You'll only miss its dynamic versioning, modularity and relocation advantages, but that should technically not harm for a static and external script.

但是,可以使用自定义 ResourceHandler 的 URL 更改为用于纯 CDN 目的的外部 URL.OmniFaces CDNResourceHandler 就是这样一个例子.

It's however possible to use a custom ResourceHandler to change the URL of a <h:outputScript> to be an external URL for pure CDN purposes. OmniFaces CDNResourceHandler is such an example.

与具体问题无关,PrimeFaces 组件是围绕 jQuery/UI 构建的.您绝对肯定需要一个单独的 jQuery UI 库实例吗?

Unrelated to the concrete problem, PrimeFaces components are built around jQuery/UI. Are you absolutely positive that you need a separate instance of jQuery UI library?