Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nice idea. This is not supported by the JSF API, but it's in theory possible with a custom script renderer. I played somewhat around it and it's indeed possible. Just create a <code>MultiScriptRenderer</code> which <code>extends ScriptRenderer</code> and override <a href="http://download.oracle.com/javaee/6/api/javax/faces/render/Renderer.html#encodeEnd%28javax.faces.context.FacesContext,%20javax.faces.component.UIComponent%29" rel="nofollow"><code>encodeEnd()</code></a> to check if the <code>name</code> attribute contains the <code>*</code> wildcard and then handle accordingly by scanning for files matching this pattern in the resources folder.</p> <p>Here's a kickoff example:</p> <pre><code>package com.example; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.Map; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import com.sun.faces.renderkit.html_basic.ScriptRenderer; public class MultiScriptRenderer extends ScriptRenderer { @Override public void encodeEnd(FacesContext context, UIComponent component) throws IOException { Map&lt;String, Object&gt; attributes = component.getAttributes(); String name = (String) attributes.get("name"); if (name.contains("*")) { String pattern = name.replace(".", "\\.").replace("*", ".*"); String library = (String) attributes.get("library"); File root = new File(context.getExternalContext().getRealPath("/resources/" + (library != null ? library : ""))); for (File file : root.listFiles()) { if (file.getName().matches(pattern)) { attributes.put("name", file.getName()); super.encodeEnd(context, component); } } attributes.put("name", name); // Put original name back. You never know. } else { super.encodeEnd(context, component); } } } </code></pre> <p>Register it in <code>faces-config.xml</code> as follows <em>(Sorry, <a href="http://download.oracle.com/javaee/6/api/javax/faces/render/FacesRenderer.html" rel="nofollow"><code>@FacesRenderer</code></a> annotation isn't going to work until it's fixed in JSF 2.2 for this specific corner case, see also <a href="http://java.net/jira/browse/JAVASERVERFACES-1748" rel="nofollow">JSF issue 1748</a>)</em>:</p> <pre><code>&lt;render-kit&gt; &lt;renderer&gt; &lt;component-family&gt;javax.faces.Output&lt;/component-family&gt; &lt;renderer-type&gt;javax.faces.resource.Script&lt;/renderer-type&gt; &lt;renderer-class&gt;com.example.MultiScriptRenderer&lt;/renderer-class&gt; &lt;/renderer&gt; &lt;/render-kit&gt; </code></pre> <p>Works fine on Mojarra 2.0.3 here. You can use patterns like <code>*.js</code> and <code>prefix*.js</code>. The particular code example is only tight coupled to a specific JSF implementation to save code boilerplate. It also requires that the WAR is expanded on deploy, otherwise browsing the directory by <code>File#listFiles()</code> won't be possible (which thus excludes certain (older) servletcontainer versions/configurations). For other JSF implementations you'll have to extend its <code>ScriptRenderer</code> instead, or write a whole new one if you want to be implementation independent (which should be pretty simple though, just look at standard <code>ScriptRenderer</code> source if you stucks).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload