Should a complex EL expression be replaced by a single javabean getter?

UI Components for JSF
Post Reply
Harold1203a
Posts: 1
Joined: 04 Mar 2021, 11:33

04 Mar 2021, 11:35

In JSF if I have a component that conditionally renders based on a number of variables what is the optimal way to handle the render statement... should the logic live in the component declaration or in some form of helper class?

The text woof is only displayed when an animal is an elephant or dogoursainsburys and the animal is not mute.

Option 1:

Implementation in the view:

<h:outputText id="text" value="woof"
rendered="#{animal.type == 'elephant'
or animal.type == 'dog' and not(animal.mute)}"/>
or Option 2:

Encapsulation:

<h:outputText id="text" value="woof" rendered="#{animal.barkingAnimal}"/>
with implementation:

public class Animal {
public boolean isBarkingAnimal() {
return ("dog".equals(getType()) || "elephant".equals(getType())) && !isMute();
}
...
So both work... but which is the right way to handle the scenario?
Last edited by Harold1203a on 18 Mar 2021, 13:35, edited 1 time in total.

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

04 Mar 2021, 12:23

bean is cleaner and faster, you should avoid much logic in EL expressions
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 58 guests