Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

ui:composition and getComponent()  RSS feed

 
Ranch Hand
Posts: 1703
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, the composition defined by the facelet <ui:composition> should not be in the component tree. But why ActionEvent.getComponent can listen to the commandButton event inside <ui:composition>.
Here is an example:
User.java

index.xhtml
step1.JPG
[Thumbnail for step1.JPG]
Fill out the name field
step2.JPG
[Thumbnail for step2.JPG]
click the clear name button to make the name = none
step3.JPG
[Thumbnail for step3.JPG]
click the "click" button and navigate to welcome page, name=none
 
Bartender
Posts: 19986
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I recall my facelets tags correctly, a ui:composition is supposed to define a component that gets included in other components, so you would not normally use it directly in a primary View Template, which is presumably what index.html is. I don't see anything wrong with it doing what you say it's doing based on your sample. Seems to be exactly what I'd expect.

On the other hand you should almost never use an actionListener. Only really, really old (pre 1.0) JSF and very special cases (multiple actions from one submit) need an actionListener. Normally you should just use a simple action referencing (NOT calling!) an action method. AJAX uses listeners, but that's because AJAX responds to many different types of events. Actions are only fired when a command control is fired (commandButton or commandLink).

Also, you should never attempt to pass parameters on an action or an AJAX event. JSF expects that the action can get all the data it needs from the backing bean, which will have automatically been updated with the form values before the action method is invoked by JSF. Trying to force values in manually is redundant, and inefficient.
 
Himai Minh
Ranch Hand
Posts: 1703
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tim.
Thanks for your reply.
In this JSF Showcase https://www.liferayfaces.org/web/guest/jsf-showcase/-/jsf-tag/ui/composition/general,
it says :


ui:composition
Same as ui:component, ui:composition can add content to a Facelet page, but, as opposite to ui:component, it won't be added to the component tree.


ui:composition is not added to the component tree. But in my simple example above, e.getComponent still works. The getComponent means getting components from the component tree. Since ui:composition is not in the component tree, I think getComponent cannot see the button in the composition.
 
Tim Holloway
Bartender
Posts: 19986
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My memory was incorrect. A composition includes components, not the other way around.

I think that the documentation is imprecise here. The important thing about a composition tag is that JSF will use it as an indicator to ignore every non-JSF element outside of the composition. So the composition doesn't get included into the component tree, but it does signal the exclusion and inclusion of elements that do go into the component tree. Specifically, only what's inside the composition goes into the component tree and what's outside does not. One reason for this is to make it easier for HTML editors to work with JSF templates by allowing HTML tags that JSF doesn't want to see, but the editor needs to see.

However, the primary advantage of ui:composition is its ability to take templates and put content into them. So usually you'd have ui:include and ui:define sub-elements. Although I have one app where I have an h:messages element in my composition. Essentially, what this allows you is an easy way to tile pages but without the hair-pulling quirks of the older Tiles system.

So anyway, I think that it's doing what it's supposed to do, but your form and its children would more properly be pulled from a template.

And, as I said, you shouldn't be directly accessing the component tree or writing action listeners. Those features do have their uses, but of all the JSF apps I've ever written, only one did things with the component tree and if I was to design it over again, even that one probably wouldn't.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!