jacekn
jacekn

Reputation: 1541

How to bind included xhtml with managed JSF bean?

Certain fields in my application are common between entities. I don't want to copy them from one xhtml to another, so I created a single xhtml which is then ui:included where it needs to be. The challenge I'm facing, is with connecting that included xhtml to the bean that backs the form.

Just a bare bones example here. Player and Coach are separate entities and have dedicated xhtml files. They both have a field named firstName and both have a Save button. These two elements are in a separate common.xhtml file.

<h:inputText value="#{playerBean.player.firstName}" '/>
<h:commandButton value="Save" action="#{playerBean.save}" />

This code clearly will not work when it's included under `coach_edit.xhtml' because in that case, the bean name is coachBean and the entity is stored in coachBean.coach.

Is it possible to bind the input text and command button to beans/properties by some kind of aliases, instead of explicit names?

Update

Lada's solution works well for the bean part. As far as the entity on the bean, I added a getEntity() method to both Player and Coach beans to return player and coach respectively. That way, the included xhtml refers to entity rather than player or coach.

Upvotes: 2

Views: 706

Answers (1)

skybber
skybber

Reputation: 409

You can pass the appropriate bean to the included XHTML file by a param. Use this in your case:

<ui:include source="common.xhtml">
  <ui:param name="commonBean" value="#{playerBean}"/> <!-- or coachBean in second case -->
</ui:include>

Upvotes: 3

Related Questions