jsf 2 - When to use <ui:include>, tag files, composite components and/or custom components? -


i started using jsf 2.0 facelets , got puzzled new composite components knowing existing <ui:include> , other templating techniques offered facelets 1.x.

what difference between approaches? functionally seem offer same: <ui:param> vs <cc:attribute>, <ui:insert>+<ui:define> vs tag files, reuse of existing templates. there besides syntax , clear interface specification in case of composite components? performance differ?

what difference between approaches?

facelet templates

use facelet templates (as in <ui:composition>, <ui:include> , <ui:decorate>) if want split main page layout fragments reuseable templates. e.g. header, menu, content, footer, etc.

examples:

facelet tag files

use facelet tag files if want have reuseable group of components in order prevent/minimize code duplication. e.g. group of label+input+message components. major difference composite components output of facelet tag file not represent single uicomponent , may in circumstances solution when composite component doesn't suffice. generally, having <ui:include> 1 or more <ui:param> signal include file can better tag file.

examples:

composite components

use composite components if want create single , reuseable custom uicomponent single responsibility using pure xml. such composite component consists of bunch of existing components and/or html , physically rendered single component , supposed bound single bean property. e.g. component represents single java.util.date property 3 dependent <h:selectonemenu> components, or component combines <p:fileupload> , <p:imagecropper> single <my:uploadandcropimage> referring single custom com.example.image entity property.

examples:

custom components

use custom component whenever functionality cannot achieved facelet tag files or composite components, because of lack of support in standard/available set of components. examples can found on place in source code of open source component libraries such primefaces , omnifaces.

tag handlers

when want control building of jsf component tree instead of rendering of html output, should use tag handler instead of component.

examples:

example projects

here example projects utilize of above mentioned techniques.


could performance differ?

technically, performance concern negligible. choice should made based on concrete functional requirements , final degree of abstraction, reusability , maintainability of implementation. each approach has own definied purpose , limitations.

composite components have significant overhead during building/restoring of view (specifically: during saving/restoring view state). and, in older versions of mojarra, composite components had performance issues assigning default values, fixed since 2.1.13. also, mojarra had memory leak when <cc:attribute method-signature> used method expressions, entire component tree re-referenced in http session, fixed since 2.1.29 / 2.2.8. memory leak can bypassed in older 2.1 versions below:

<context-param>     <param-name>com.sun.faces.serializeserverstate</param-name>     <param-value>true</param-value> </context-param> 

or in older 2.2 versions below:

<context-param>     <param-name>javax.faces.serialize_server_state</param-name>     <param-value>true</param-value> </context-param> 

still, when have relatively "a lot of" composite components, , have javax.faces.state_saving_method set client, performance pain. not abuse composite components if merely want basic functionality possible simple include file or tag file. not use ease of configuration (read: no *.taglib.xml file needed) excuse prefer composite components on tag files.

when using mojarra 2.2.10 or older, not forget disable relatively short facelets refresh period production mode:

<context-param>     <param-name>javax.faces.facelets_refresh_period</param-name>     <param-value>-1</param-value> </context-param> 

do not use setting development, otherwise you've restart whole server changes in facelets files reflected! mojarra 2.2.11 , newer, , myfaces defaults -1 when javax.faces.project_stage not set development.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -