• cdi
  • components
  • contexts
  • converters
  • eventlisteners
  • exceptionhandlers
  • facesviews
  • filters
  • functions
  • managedbeans
  • push
  • resourcehandlers
  • scripts
  • servlets
  • taghandlers
  • utils
  • validators
  • viewhandlers
-
  • ContextParam
  • Cookie
  • Eager
  • FacesConverter
  • FacesValidator
  • GraphicImageBean
  • Param
  • ViewScoped

The CDI annotation @Cookie 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.

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;

Validation is by design not supported as cookies are usually beyond enduser's control. TODO: conversion?

Demo

Cookie injected in bean:

Actual cookie in view:

Demo source code
<h:form>
    <h:commandButton value="Add/update a session cookie with a random value" action="#{cdiCookieBean.add}" />
</h:form>

<h:panelGroup id="result" layout="block">
    <p>Cookie injected in bean: #{cdiCookieBean.testCookie}</p>
    <p>Actual cookie in view: #{cookie.testCookie.value}</p>
</h:panelGroup>