Built-in functions and utils are parts of PrimeFaces Extensions: the pe:switch tag and some configuration helpers
for the OmniFaces CombinedResourceHandler, HTML sanitizing and MongoDB lazy data models.
Sometimes you need to display different outputs or components depending on a value. Usually you can achieve this with
ui:fragment and different rendered checks. The pe:switch util tag does it without
declaring those fragments.
System event listener that sanitizes HTML content in components with escape="false". This helps prevent XSS
vulnerabilities while still allowing some HTML formatting using an OWASP Java HTML Sanitizer PolicyFactory.
See also Sanitizing Converter.
<dependency>
<groupId>com.googlecode.owasp-java-html-sanitizer</groupId>
<artifactId>owasp-java-html-sanitizer</artifactId>
<version>20240325.1</version>
</dependency>
<application>
<system-event-listener>
<system-event-listener-class>org.primefaces.extensions.application.EscapeSanitizerComponentListener</system-event-listener-class>
<system-event-class>jakarta.faces.event.PostAddToViewEvent</system-event-class>
<source-class>jakarta.faces.component.html.HtmlBody</source-class>
</system-event-listener>
</application>
The OmniFaces CombinedResourceHandler optimizes your website to send only 1 CSS and 1 JS file to the browser. It requires overriding some core PrimeFaces code, because PrimeFaces hard codes some resources in the HeadRenderer, which leaves them outside the scope of being combined, like the theme.css or beanvalidation.js.
BalusC has an excellent tutorial on how to make this work found here. PrimeFaces Extensions includes these classes so you don't have to write your own or keep them up to date with every PrimeFaces release.
<application>
<system-event-listener>
<system-event-listener-class>org.primefaces.extensions.application.PrimeFacesScriptProcessor</system-event-listener-class>
<system-event-class>jakarta.faces.event.PostAddToViewEvent</system-event-class>
<source-class>jakarta.faces.component.UIViewRoot</source-class>
</system-event-listener>
</application>
<lifecycle>
<phase-listener>org.primefaces.extensions.application.PrimeFacesResourceProcessor</phase-listener>
</lifecycle>
<render-kit>
<renderer>
<component-family>jakarta.faces.Output</component-family>
<renderer-type>jakarta.faces.Head</renderer-type>
<renderer-class>com.sun.faces.renderkit.html_basic.HeadRenderer</renderer-class>
</renderer>
</render-kit>
<render-kit>
<renderer>
<component-family>jakarta.faces.Output</component-family>
<renderer-type>jakarta.faces.Head</renderer-type>
<renderer-class>org.apache.myfaces.renderkit.html.HtmlHeadRenderer</renderer-class>
</renderer>
</render-kit>
PrimeFaces Extensions provides an out-of-the-box implementation of a lazy data model for MongoDB using Morphia, which supports basic features.
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
<version>2.2.3</version>
<optional>true</optional>
</dependency>
new MorphiaLazyDataModel<>(MyEntity.class, () -> datastore);
new MorphiaLazyDataModel<>(MyEntity.class, () -> datastore, "uuid");
// let's implement a global filter here for the string fields
dataModel.globalFilter((query, filterMeta) -> {
Object val = filterMeta.getFilterValue();
query.filter(
Filters.or(
Filters.regex("brand").pattern(val + "").caseInsensitive(),
Filters.regex("previousOwners").pattern(val + "").caseInsensitive(),
Filters.regex("color.name").pattern(val + "").caseInsensitive()
)
);
});
// override the default match mode query for the "sold" field to allow filtering with strings
dataModel.overrideFieldQuery("sold", (query, filterMeta) -> {
if (filterMeta.getFilterValue().toString().startsWith("y")) {
query.filter(Filters.eq("sold", true));
} else if (filterMeta.getFilterValue().toString().startsWith("n")) {
query.filter(Filters.eq("sold", false));
}
});