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

marți, 23 iunie 2015

[OmniFaces utilities 2.1] Get the qualifier annotation of the given qualifier class from the given injection point


[OmniFaces utilities] The getQualifier() method returns the qualifier annotation of the given qualifier class from the given injection point.

Method:
Usage:

In order to see at work the Beans#getQualifier(), let's define an annotation for injecting the value of an application initialization parameter in a bean. First, let's write the CDI annotation named, ContextParam:

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface ContextParam {

    @Nonbinding
    String name() default "";

}

Further, let's write the CDI producer for injecting the value of an application initialization parameter (declared in web.xml via <context-param>) in a bean as defined by the above ContextParam annotation:

public class ContextParameterProducer {

 public static final String CONTEXT_PARAM_NOT_FOUND = "CONTEXT_PARAM_NOT_FOUND";

  // Workaround for OpenWebBeans not properly passing it as produce() method argument.
  @SuppressWarnings("unused")     

  @Inject
  private InjectionPoint injectionPoint;

  @Produces
  @ContextParam
  public String produce(InjectionPoint injectionPoint) {
   ContextParam contextParam = Beans.getQualifier(injectionPoint, ContextParam.class);
   return contextParam.name().isEmpty() ? CONTEXT_PARAM_NOT_FOUND : Faces.getInitParameter(contextParam.name());
  }
}

You can find the complete application on GitHub.

Further, let's see it in a bean named, MyBean:

@Named
@RequestScoped
public class MyBean {

 private static final Logger LOG = Logger.getLogger(MyBean.class.getName());

 @Inject
 @ContextParam(name = "javax.faces.PROJECT_STAGE")
 private String projectStage;

 @Inject
 @ContextParam(name = "dummy")
 private String dummy;

 @Inject
 @ContextParam
 private String none;

 public void doSomething() {
  LOG.log(Level.INFO, "projectStage = {0}", projectStage); // e.g. Development
  LOG.log(Level.INFO, "dummy = {0}", dummy); // null
  LOG.log(Level.INFO, "none = {0}", none); // CONTEXT_PARAM_NOT_FOUND
 }
}

Moreover, you may want to check the OmniFaces @Cookie implementation.

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