Outside of my tutorial, and therefore be compressed, I would like to report on this recent experience with JSF.
In a Web application, the user can register for various services (features). After login, a summary page, which optionally provides information about individual features, is presented. The app comes with a bean, called SessionControlle, which instantiates the features by a factory. Each feature provides themselves a part (Part) of the overview page. . As a part may have any number of components, they are each stored as <ui:composition> in a separate file. The idea was to provide by SessionController a dynamic list of the file names. This list should now be processed in a loop and using <ui:repeat> and <ui:include> to call the files:
<ui:repeat value="#{sessionController.parts}" var="part"> <ui:include src="#{part}"/> </ui:repeat>
In the session controller, there is a suitable method
public List<String> getParts() { return parts; }
Parts is again filled with the file names while registering the features. The output, however, consisted of – nothing.
So, to check the variable, I added an output of the file names.
<ui:repeat value="#{sessionController.parts}" var="part"> <h:outputText value="#{part}" style="background-color: green"/> <ui:include src="#{part}"/> </ Ui: repeat>
Yep, the correct file names have been displayed.
Now this version, to access the first element of the list:
<ui:repeat value="#{sessionController.parts}" var="part"> <h:outputText value="#{part}" style="background-color: green"/> <ui:include src="#{sessionController.parts[0]}"/> </ui:repeat>
Now the contents of the first registered file is displayed – and repeated as often as file entries are in total. An analysis of behavior showed that getParts is called only once. It seems as if the contents of src="…" is evaluated before calling the loop. This also explains why in the first case nothing was found: Before execution of the loop, part is not defined.
If you use, however, instead of the construct <ui:repeat> <c:forEach> from the JSP library, so the insertion of a variable number of files works without problems.
<c:forEach items="#{sessionController.parts}" var="part"> <ui:include src="#{part}"/> </c:forEach>
A bug in the implementation of Faclets?
Do you want to support this blog? You may help to finance the needed hardware.
Want to read more about software development? Purchase one of my books:
Start application development with Java. Learn development foundation, quality control and more.
A Journey through Java EE Technologies whilst developing Web Applications with JavaServer Faces.
Java Lambdas and Parallel Streams
The compact starter: Foundation, supporting structures, parallel processing
No software development, just nature:
A photographic image book. Get it for free. Or pay whatever you like.
No Comments Yet