Can JSF's h: outputScript attribute be dynamic / how to inject Javascript into JSF from dynamic locations?

I am using com.sun.faces

version 2.1.18

. I am showing a list of questions and for some questions (based on the database id) I want to insert some dynamic Javascripts.

According to the attribute, it is of type: (must be evaluated before ). h:outputScript

name

javax.el.ValueExpression

java.lang.String

However, this code works for me:

<ui:repeat value="#{js.questionScripts[question.id]}" var="script">
  <h:outputScript name="myScript.js" library="js" target="head"/>
</ui:repeat>

      

But this code is not:

<ui:repeat value="#{js.questionScripts[question.id]}" var="script">
  <h:outputScript name="#{script}" library="js" target="head"/>
</ui:repeat>

      

#{question}

comes from the environment of <ui:repeat>

iterating over the list of questions.

I added an output to see if it was not #{script}

empty but contained the correct resource name.

Any ideas on how to solve this problem or implement an alternative?

+3


source to share


1 answer


<h:outputScript>

must be created at build time to be recognized by JSF resource management. <ui:repeat>

runs while the render is being viewed and is therefore too late. You must use <c:forEach>

. I'm not sure how it worked for you, but it works great for me, provided it #{js}

is a request, session, or bean-scoped application, the property is questionScripts

already provisioned at the time of its (post) build and that #{question.id}

is available at build time.

<c:forEach items="#{js.questionScripts[question.id]}" var="script">
    <h:outputScript name="js/#{script}" target="head"/>
</c:forEach>

      

(note that you must use an attribute items

instead of an attribute value

, and also note that I have corrected the seemingly incorrect use of the attributelibrary

).



See also:

+3


source







All Articles