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:
- how include xhtml in xhtml using jsf 2.0 facelets?
- what real conceptual difference between ui:decorate , ui:include?
- how customize h:head when using ui:composition template?
- how change head elements of page when using ui:composition
- how ajax-refresh dynamic include content navigation menu? (jsf spa)
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:
- how create custom facelets tag?
- how make grid of jsf composite component?
- how create composite component datatable column?
- primefaces outputlabel composite component
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:
- our composite component wiki page
- the balusc code: composite component multiple input fields
- split java.util.date on 2 h:inputtext fields representing hour , minute f:convertdatetime
- select items in multiple selectmanycheckbox dynamic ids
- extending jsf commandlink component
- avoiding duplicate ids when reusing facelets compositions in same naming container
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:
- custom facelet component in jsf
- how can access content of created <ui:define> programmatically?
- conditional render in tagfile depending on whether attribute specified or not
- performing redirect, when conversion / validation associated query parameters fails
example projects
here example projects utilize of above mentioned techniques.
- java ee kickoff app (templates - includes - tagfiles - composite)
- omnifaces showcase (templates - includes - tagfiles - composite)
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
Post a Comment