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?