Java Reference
In-Depth Information
All of the preceding annotations except
@FlowScoped
are defined in the
javax.
enterprise.context
package. The
@FlowScoped
annotation is defined in the
javax.faces.flow
package.
We turn our Java class into a request scoped CDI named bean by adding the
appropriate annotations:
package com.ensode.jsf.namedbeans;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
public class RegistrationBean {
}
The
@Named
annotation designates our class as a CDI named bean, and the
@RequestScoped
annotation designates that our CDI named bean will have
a scope of request.
NetBeans may not be able to find the
@RequestScoped
annotation,
if this the case, we need to add
cdi-api.jar
to our project by
right-clicking on
Libraries
, selecting
Add JAR/Folder...
, and selecting
cdi-api.jar
from the
modules
folder of our glassfish installation.
Now, we need to modify our CDI named bean by adding properties that will hold
the values entered by the users.
Automatic generation of getter and setter methods
NetBeans can automatically generate getter and setter methods for
our properties. We simply need to click the keyboard shortcut to
insert code,
Alt
+
Insert
in Windows and Linux (
Ctrl
+
I
on Mac OS),
and select
Getters and Setters
.
package com.ensode.jsf.namedbeans;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
public class RegistrationBean {