I am working with JSF Facelets and have initialized parameters using <ui:param>
in different templates. Here's the relevant code snippet:
<ui:composition xmlns="; xmlns:h="jakarta.faces.html" xmlns:f="jakarta.faces.core" xmlns:ui="jakarta.faces.facelets" xmlns:c="jakarta.tags.core">
<ui:param name="param1" value="value 1" />
<ui:param name="param2" value="value 2" />
<ui:param name="param3" value="value 3" />
param1: #{param1}
<p></p>
param2: #{param2}
<p></p>
param3: #{param3}
<c:forEach begin="1" end="3" var="idx">
<p></p>
<h:outputText value="**How can I dynamically set the parameter name here (e.g., param${idx}) to get the corresponding value?**" />
</c:forEach>
</ui:composition>
How can I dynamically access the ui:param
values using a loop (like <c:forEach>
)? I want to retrieve the corresponding values based on the dynamic parameter names.
I am working with JSF Facelets and have initialized parameters using <ui:param>
in different templates. Here's the relevant code snippet:
<ui:composition xmlns="http://www.w3./1999/xhtml" xmlns:h="jakarta.faces.html" xmlns:f="jakarta.faces.core" xmlns:ui="jakarta.faces.facelets" xmlns:c="jakarta.tags.core">
<ui:param name="param1" value="value 1" />
<ui:param name="param2" value="value 2" />
<ui:param name="param3" value="value 3" />
param1: #{param1}
<p></p>
param2: #{param2}
<p></p>
param3: #{param3}
<c:forEach begin="1" end="3" var="idx">
<p></p>
<h:outputText value="**How can I dynamically set the parameter name here (e.g., param${idx}) to get the corresponding value?**" />
</c:forEach>
</ui:composition>
How can I dynamically access the ui:param
values using a loop (like <c:forEach>
)? I want to retrieve the corresponding values based on the dynamic parameter names.
1 Answer
Reset to default 1The <ui:param>
is not suitable for this. It's not stored in a publicly accessible scope and there's no such thing as #{faceletScope}
(that's probably a good idea for next Faces version though).
Use <c:set>
instead. On that you can explicitly specify the desired scope. The request
scope is your best bet, but the view
scope can also, depending on the nature of the param value. Then you can simply access the param via either #{requestScope}
or #{viewScope}
map respectively.
<c:set var="param1" value="value 1" scope="request" />
<c:set var="param2" value="value 2" scope="request" />
<c:set var="param3" value="value 3" scope="request" />
<p>param1: #{param1}</p>
<p>param2: #{param2}</p>
<p>param3: #{param3}</p>
<c:forEach begin="1" end="3" var="idx">
<p>#{requestScope['param' += idx]}</p>
</c:forEach>
See also:
- JSTL in JSF2 Facelets... makes sense?