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

luni, 13 iulie 2015

OmniFaces 2.1 - Inject HTTP cookie via @Cookie annotation

The CDI annotation Cookie is available starting with OmniFaces 2.1 and it allows you to inject a HTTP request cookie value from the current JSF context in a CDI managed bean. It's basically like:

@ManagedProperty("#{cookie.cookieName.value}")
private String cookieName;

in a "plain old" JSF managed bean. You can use this as:

// By default the name of the cookie is taken from the name of the variable 
// into which injection takes place. The example below injects the cookie with name foo.
@Inject @Cookie
private String foo;

// The name can be optionally specified via the name attribute. 
// The example below injects the cookie with name foo into a variable named bar.
@Inject @Cookie(name="foo")
private String bar;

The implementation of this annotation can be a source of inspiration for creating new such annotations. The Cookie annotation is declared in org.omnifaces.cdi.Cookie as:

@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER })
public @interface Cookie {
 @Nonbinding  String name() default "";
}

Further, the producer for injecting a JSF request cookie as defined by the above Cookie annotation is defined in org.omnifaces.cdi.cookie.RequestCookieProducer:

public class RequestCookieProducer {

 @SuppressWarnings("unused") // Workaround for OpenWebBeans not properly passing it as produce() method argument.
 @Inject
 private InjectionPoint injectionPoint;

 @Produces
 @Cookie
 public String produce(InjectionPoint injectionPoint) {
  Cookie cookie = getQualifier(injectionPoint, Cookie.class);
  String name = cookie.name().isEmpty() ? injectionPoint.getMember().getName() : cookie.name();
  return getRequestCookie(name);
 }
}

The getQualifier() method returns the qualifier annotation of the given qualifier class from the given injection point and you can read further details here, where you can see an annotation for injecting the value of an application initialization parameter (declared in web.xml via <context-param>).

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