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