#{resource} EL 在哪里指定?

Where is #{resource} EL specified?

在 JSF 页面中,我使用 EL 来定位放置在 /resource/images/ 文件夹中的图像。

<h:graphicImage value="#{resource['images:logo.gif']}"

EL如何定位图片? 也许一个规范定义了规则,但我不知道是哪个规范。 我试图在 JSR-000344 JavaServer Faces 2.2 ,the JSR-000342 Java Platform, Enterprise Edition 7, the JSR-000341 Expression Language 3.0 中找到它,但没有找到。

JSF 2.0 specification中,2.6.2章节首次提到:

2.6.2 Rendering Resources

Resources such as images, stylesheets and scripts use the resource handling mechanism as outlined in Section 2.6.1 “Packaging Resources”. So, for example:

<h:graphicImage name=”Planets.gif” library=”images”/>
<h:graphicImage value=”#{resource[‘images:Planets.gif’]}”/>

These entries render exactly the same markup. In addition to using the name and library attributes, stylesheet and script resources can be “relocated” to other parts of the view. For example, we could specify that a script resource be rendered within an HTML “head”, “body” or “form” element in the page.

然后在第 5.6.2.5 章中:

5.6.2.5 Resource ELResolver

This resolver is a means by which Resource instances are encoded into a faces request such that a subsequent faces resource request from the browser can be satisfied using the ResourceHandler as described in Section 2.6 “Resource Handling”.

ELResolver method implementation requirements

If base and property are not null, and base is an instance of ResourceHandler (as will be the case with an expression such as #{resource[‘ajax.js’]}, perform the following. (Note: This is possible due to the ImplicitObjectELResolver returning the ResourceHandler, see Section 5.6.2.1 “Implicit Object ELResolver for Facelets and Programmatic Access”)

  • If property does not contain a colon character ‘:’, treat property as the resourceName and pass property to ResourceHandler.createResource(resourceName).
  • If property contains a single colon character ‘:’, treat the content before the ‘:’ as the libraryName and the content after the ‘:’ as the resourceName and pass both to ResourceHandler.createResource(resourceName, libraryName)
  • If property contains more than one colon character ‘:’, throw a localized ELException, including property.

If one of the above steps results in the creation of a non-null Resource instance, call ELContext.setPropertyResolved(true) and return the result of calling the getRequestPath() method on the Resource instance.

它也在 JSF 2.1 和 2.2 规范的相同章节中提到。

与具体问题无关images是资源库名称的一个非常糟糕的例子。不要从规范示例中接管它。

另请参阅:

  • How to reference CSS / JS / image resource in Facelets template?
  • What is the JSF resource library for and how should it be used?