JSF rendered question
Asked Answered
O

1

6

Is EL-parsing of children to elements with rendered="false" really supposed to be evaluated? This is causing me alot of trouble with null pointer exceptions and similar. Looking at the following example:

<p:tab title="#{userCompetenceController.getTreeName(3)}" rendered="#{!empty userCompetenceController.getTreeName(3)}">
  <xdin:competenceTable id="competenceBox3"
                        profile="#{userCompetenceController.selectedProfile}"
                        tree="#{userCompetenceController.getCompetenceTree(3)}"
                        maxHeight="500px"/>
</p:tab>

The main issue (besides performance) is that xdin:competenceTable does not support a null tree-attribute. getTreeName(int index) returns null in this case, and is followed by a call to getCompetenceTree(3) which returns null, even though its parent (p:tab) has rendered="false"

In short: xdin:competenceTable is parsed by EL even though it's parent has rendered="false". Why?

Odilia answered 11/5, 2011 at 7:49 Comment(4)
One thing that springs to mind - if you're using Facelets and the type of the attribute is not ValueExpression then the expression may be evaluated at tree creation time.Daughtry
I'm afraid I'm not knowledgeable enough to get your hint.Odilia
common view technologies are either Facelets (XHTML) or JSPs - they behave differently with respect to EL. In terms of attribute types - if you take an example like panelGroup, most are defined as type javax.el.ValueExpression - this means they can take deferred expressions.Daughtry
Makes sense. I assume the primefaces components I use are using ValueExpressions, so the whole tree is built without knowing if it should be rendered or not.Odilia
W
3

Take a look at the JSF lifecylce below.

Rendering is only the last phase, and rendered="false" only affects that last phase, while errors in constructing a component happen in the first.

Non-rendered components should in fact not do anything during all phases, but it looks as though your component does not conform to that part of the spec.

enter image description here

Windburn answered 11/5, 2011 at 8:24 Comment(8)
Makes sense. So there is no way to conditionally parse EL-expressions?Odilia
@Rasmus: conditionally parse? Definitely not. Conditionally evaluate? Perhaps - not sure if && and || are short-circuiting in EL. But I wouldn't put too much logic into EL anyway and prefer to add methods to the backing bean.Windburn
My issue is not really with heavy EL logic, but with backing bean methods that lazily loads alot of data from a db. EL parsing of unrendered objects causes them to fetch data that is not needed yet.Odilia
@Rasmus: so couldn't those methods refrain from loading anything based on the same criteria you use for the rendered attribute?Windburn
Very true, but clutters up the code abit in the beans. I guess I don't have much choice though. Thanks for the info.Odilia
@Michael - it is not true to say that the rendered attribute only affects the last phase - in a conforming component, it affects every phase - Property: rendered. Access: RW. Type: boolean. Description: A flag that, if set to true, indicates that this component should be processed during all phases of the request processing lifecycle. The default value is “true”. Components should check this value prior to processing at every phase. This is primarily a security mechanism.Daughtry
@McDowell: Hm, so it looks like I was wrong, and the problem is that Rasmus' component does not follow the spec. Or would the EL of attributes have to be evaluated even in a conforming component?Windburn
@Michael - deferred evaluation expressions are (or should be) evaluated lazily, so if a container is not rendered, no processing should take place that causes them to be evaluated. In this case, I do assume that the implementation of p:tab follows the standard algorithm for processing children and facets. (There may be an exception in Facelets where the attribute is not defined to be of type ValueExpression - I think the expression will be evaluated at tree creation time in this case. I haven't done enough research to be sure.)Daughtry

© 2022 - 2024 — McMap. All rights reserved.