craigcaulfield
craigcaulfield

Reputation: 3538

Passing and using parameters between JSF includes

I've been trying to understand JSF templating and include attributes and passing parameters between components. In Mastering JavaServer Faces 2.2 by Anghel Leonard, I came across the following example of passing parameters, which I don't fully understand.

Given this bean:

@Named
@ViewScoped
public class TemplatesBean implements Serializable {

    private String msgTopDefault="";
    private String msgBottomDefault="";
    private String msgCenterDefault="No center content ... press the below button!";

    public void centerAction(){
        this.msgCenterDefault="This is default content";
    }

    // Getters and setters
}

Parameters are passed to contentDefault.xhtml with:

<ui:insert name="content">
  <ui:include src="/template/default/contentDefault.xhtml">
    <ui:param name="templatesBeanName" value="#{templatesBean}"/>
    <ui:param name="contentPropertyName" value="msgCenterDefault"/>
  </ui:include>
</ui:insert>

Then, within contentDefault.xhtml the parameters are used as follows:

<ui:composition>            
     <h:outputText value="#{templatesBeanName[contentPropertyName]}"/>
     <h:form>
         <h:commandButton value="Center Button" action="#{templatesBeanName['centerAction']()}"/>               
     </h:form>
</ui:composition>

I've never used the square-bracket syntax before, but if a reference to templatesBean is being passed in, why not just use that to access the properties or invoke action methods? For example, the following code works for me too and seems simpler:

<h:form>
     <h:commandButton value="Center Button" action="#{templatesBeanName.centerAction()}"/>
</h:form>

Recognising that the example in the book may be a contrived example to illustrate a point, are there use cases where the other syntax is appropriate?

Upvotes: 0

Views: 837

Answers (1)

Kukeltje
Kukeltje

Reputation: 12337

I do not know or own the book, so I cannot investigate that way what they want to illustrate, but I can sort of deduce that by looking at the full example you posted, not just the part about the centerAction.

If you look at

<ui:insert name="content">
  <ui:include src="/template/default/contentDefault.xhtml">
    <ui:param name="templatesBeanName" value="#{templatesBean}"/>
    <ui:param name="contentPropertyName" value="msgCenterDefault"/>
  </ui:include>
</ui:insert>

you'll see that 2 params are passesd on, templatesBeanName and contentPropertyName

In

<ui:composition>            
     <h:outputText value="#{templatesBeanName[contentPropertyName]}"/>
     <h:form>
         <h:commandButton value="Center Button" action="#{templatesBeanName['centerAction']()}"/>               
     </h:form>
</ui:composition>

from which you just pointed to the line with action="#{templatesBeanName['centerAction']()}", a dynamic bean with a static value in suqare brackets made into a method by adding () as a postfix, you'll see another line of code above it

<h:outputText value="#{templatesBeanName[contentPropertyName]}"/>

What effectively is done here is to have a dynamic bean AND a dynamic property name being used.

So my conclusion is that with this example what they are trying to illustrate is that you are able to pass on a dynamic bean, and on that bean use either both static or dynamic methods and properties(static properties and dynamic methods not being in the example)

Upvotes: 2

Related Questions