Java Reference
In-Depth Information
Our controller has a
saveCustomer()
method that will be invoked when the user
clicks on the
Submit
button on the page, the method simply invokes the
persist()
method on the DAO, then uses standard JSF navigation to go to a confirmation page
if everything went fine, or to an error page if an exception was thrown.
Markup for the confirmation an error pages is not shown, but available
from this topic's code download.
Notice the
Controller
class has two instance variables for our JPA entity and our
DAO. There is no need for us to explicitly instantiate these variables, instead we can
use a JSF feature called
managed properties
. Managed properties are injected at
runtime by using dependency injection. In order to configure this the application's
faces-config.xml
needs to be modified.
<managed-bean>
<managed-bean-name>CustomerDAO</managed-bean-name>
<managed-bean-class>
com.ensode.jpaweb.CustomerDAO
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>Customer</managed-bean-name>
<managed-bean-class>
com.ensode.jpaweb.Customer
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>Controller</managed-bean-name>
<managed-bean-class>
com.ensode.jpaweb.Controller
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>customerDAO</property-name>
<property-class>
com.ensode.jpaweb.CustomerDAO
</property-class>
<value>#{CustomerDAO}</value>
</managed-property>
<managed-property>
<property-name>customer</property-name>