JSF/Facelets: set `action` attribute to a dynamically evaluated string
- by harto
In my JSF/Facelets application, I want to dynamically generate a breadcrumb trail from a list of page IDs using a custom tag:
<foo:breadcrumbs trail="foo,bar,baz"/>
This should generate something like:
<h:commandLink action="foo" ... />
<h:commandLink action="bar" ... />
<!-- (etc.) -->
My code looks something like this:
<ui:repeat value="#{fn:split(trail, ',')}" var="key">
<h:commandLink action="#{key}" ... />
</ui:repeat>
The problem with this code is that #{key} is interpreted as a method binding. However, I just want the string value of #{key} to be returned as the navigation outcome. How can I achieve this?
The only thing I could think of was creating a dummy managed-bean that has an outcome field and an action handler, and invoke it like so:
<h:commandLink action="#{dummy.click}" ...>
<f:setPropertyActionListener target="#{dummy.outcome}" value="#{key}" />
</h:commandLink>
with the dummy class defined like so:
public class Dummy {
private String outcome;
public String click() {
return outcome;
}
public void setOutcome(String outcome) {
this.outcome = outcome;
}
public void getOutcome() {
return outcome;
}
}
That seems ugly though, and I don't know if it would work.