-
Available since OmniFaces 2.2
The CDI annotation @
ContextParam
allows you to inject a web.xml
context parameter from the current application in a CDI managed bean. It's basically like @ManagedProperty("#{initParam['some.key']}") private String someKey;
in a "plain old" Faces managed bean.
By default the name of the context parameter is taken from the name of the variable into which injection takes place. The example below injects the context parameter with name foo
.
@Inject @ContextParam
private String foo;
The name can be optionally specified via the name
attribute, which shall more often be used as context parameters may have a.o. periods and/or hyphens in the name, which are illegal in variable names. The example below injects the context parameter with name foo.bar
into a variable named bar
.
@Inject @ContextParm(name="foo.bar")
private String bar;
Injected context params:
jakarta.faces.FACELETS_BUFFER_SIZE
=65535
jakarta.faces.FACELETS_LIBRARIES
=/WEB-INF/showcase.taglib.xml
jakarta.faces.FACELETS_SKIP_COMMENTS
=true
<p>Injected context params:</p>
<ul>
<li><code>jakarta.faces.FACELETS_BUFFER_SIZE</code> = <code>#{cdiContextParamBean.faceletsBufferSize}</code></li>
<li><code>jakarta.faces.FACELETS_LIBRARIES</code> = <code>#{cdiContextParamBean.faceletsLibraries}</code></li>
<li><code>jakarta.faces.FACELETS_SKIP_COMMENTS</code> = <code>#{cdiContextParamBean.faceletsSkipComments}</code></li>
</ul>
package org.omnifaces.showcase.cdi;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.omnifaces.cdi.ContextParam;
@Named
@RequestScoped
public class CdiContextParamBean {
@Inject @ContextParam(name="jakarta.faces.FACELETS_BUFFER_SIZE")
private String faceletsBufferSize;
@Inject @ContextParam(name="jakarta.faces.FACELETS_LIBRARIES")
private String faceletsLibraries;
@Inject @ContextParam(name="jakarta.faces.FACELETS_SKIP_COMMENTS")
private String faceletsSkipComments;
public String getFaceletsBufferSize() {
return faceletsBufferSize;
}
public String getFaceletsLibraries() {
return faceletsLibraries;
}
public String getFaceletsSkipComments() {
return faceletsSkipComments;
}
}