简体   繁体   English

通过h:outputScript包含带有资源的javascript

[英]Include javascript with resources via h:outputScript

I would like to include JScolor to my jsf application. 我想将JScolor包含到我的jsf应用程序中。 It is possible via <script> tag, but I mean it is more system via <h:outputScript> . 它可以通过<script>标签,但我的意思是它是通过<h:outputScript>更多系统。

However it is not working with resources. 但是它没有使用资源。 JSColor includes one js file and some picture files - it seems like the js file is included and the reousrces not. JSColor包含一个js文件和一些图片文件 - 它似乎包含了js文件,而不是reousrces。

Could anybody tell me why? 谁能告诉我为什么? And how to solve this? 以及如何解决这个问题?

Thank you. 谢谢。

The JS file is apparently referencing picture files via a relative path which do not represent a valid JSF resource URL. JS文件显然是通过相对路径引用图片文件,该路径不代表有效的JSF资源URL。

The <h:outputScript> generates a JSF resource URL which goes through the JSF resource handler which worries about among others automatic localization and versioning. <h:outputScript>生成一个JSF资源URL,该URL通过JSF资源处理程序,该资源处理程序担心其他自动本地化和版本控制。 It would generate an URL prefixed with /javax.faces.resource and also append the currently used FacesServlet URL mapping such as *.xhtml or /faces/* . 它将生成一个以/javax.faces.resource为前缀的URL,并附加当前使用的FacesServlet URL映射,例如*.xhtml/faces/*

Thus, if you mapped the faces servlet on *.xhtml and have a /resources/jscolor folder with the JS and image files and have referenced the JS file as follows, 因此,如果您在*.xhtml上映射了面部servlet,并且在JS和图像文件中有/resources/jscolor文件夹,并且已经引用了JS文件,如下所示,

<h:outputScript name="jscolor/jscolor.js" />

then it would generate 然后它会产生

<script type="text/javascript" src="/context/javax.faces.resource/jscolor/jscolor.js.xhtml"></script>

However, the image files are not physically available in /javax.faces.resource/jscolor folder, instead they are physically available in /resources/jscolor folder. 但是,图像文件在/javax.faces.resource/jscolor文件夹中不是物理可用的,而是在/resources/jscolor文件夹中物理可用。 The /javax.faces.resource would only be automatically resolved when you apply the faces servlet mapping on the resource name. 只有在资源名称上应用faces servlet映射时,才会自动解析/javax.faces.resource Thus, this specific case would only work if you manually edit the jscolor.js file to change image file names from eg arrow.gif to arrow.gif.xhtml . 因此,只有手动编辑jscolor.js文件以将图像文件名从arrow.gif更改为arrow.gif.xhtml ,此特定情况才有效。

If you don't utilize any automatic localization or versioning features of the JSF resource resolver, nor are using any special custom resource resolvers which requires real JSF resources rather than static HTML elements, such as this one , then you can also just go ahead with a plain vanilla HTML <script> element instead of a <h:outputScript> . 如果您没有使用JSF资源解析器的任何自动本地化或版本控制功能,也没有使用任何需要真正的JSF资源而不是静态HTML元素的特殊自定义资源解析器,例如这个 ,那么您也可以继续简单的vanilla HTML <script>元素而不是<h:outputScript>

<script type="text/javascript" src="#{request.contextPath}/resources/jscolor/jscolor.js"></script>

I may misunderstand your question, but this snippet will help: 我可能会误解你的问题,但这个片段会有所帮助:

<script
    type="text/javascript"
    src="#{facesContext.externalContext.requestContextPath}/path/on/WebContent/foo.js"></script>

I regularly use this kind of java resource include, instead of the <h:outputScript> 我经常使用这种java资源包含,而不是<h:outputScript>

Suppose your js file's path (file named jquery.js) into resources/js folder like that: 假设您的js文件的路径(名为jquery.js的文件)​​进入resources/js文件夹,如下所示:

resources/js/jquery.js

Then you have to write: 然后你必须写:

<h:outputScript name="./js/jquery.js"  target="body"/>

PS. PS。 Pay attention on attribute target (eg head, body) 注意属性目标(例如头部,身体)

add in web.xml 在web.xml中添加

<servlet-mapping>
  <servlet-name>Resource Servlet</servlet-name>
  <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM