My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

sâmbătă, 26 septembrie 2015

[OmniFaces utilities 2.2] Encode the given component locally as HTML


[OmniFaces utilities] The encodeHtml() method encodes the given component locally as HTML, with UTF-8 character encoding, independently from the current view. The current implementation, however, uses the current faces context. The same managed beans as in the current faces context will be available as well, including request scoped ones. But, depending on the nature of the provided component, the state of the faces context may be affected because the attributes of the context, request, view, session and application scope could be (in)directly manipulated during the encode. This may or may not have the desired effect. If the given view does not have any component resources, JSF forms, dynamically added components, component event listeners, then it should mostly be safe. In other words, use this at most for "simple templates" only, e.g. a HTML based mail template, which usually already doesn't have a HTML head nor body.

Method:
Usage:

Let's suppose that we have programmatically created the below email template (well, just the "main" block):

HtmlPanelGroup panel = new HtmlPanelGroup();
panel.setId("emailTemplateId");
panel.setStyle("border: 1;");
panel.setLayout("block");
...

Now, we encode it locally as HTML via Components#encodeHtml():

import org.omnifaces.util.Components;
...
String encodedPanel = Components.encodeHtml(panel);

The produced HTML will be: <div id="emailTemplateId" style="border: 1;">...</div>

Or, we can find the component in page, as below:

<h:panelGroup id="emailTemplateId" layout="block">...</h:panelGroup>

import org.omnifaces.util.Components;
...
UIComponent panel = FacesContext.getCurrentInstance().getViewRoot().findComponent("emailTemplateId");
        
String encodedPanel = Components.encodeHtml(panel);

The produced HTML will be: <div id="emailTemplateId">...</div>

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors