Java Server Faces (JSF) and Facelets have largely supplemented JSP and permit the use of DI. This simplifies access to the EJB but JNDI can be used if desired. JSF uses the concept of a managed bean to hold business logic. In EJB 3.1 it is not necessary to actually use a managed bean. However, both using an EJB directly and using a managed bean to access an EJB will be presented. Knowing how to use the managed bean approach can be useful especially when reviewing older code.
To access an EJB from a JSF page:
In this example we will also package the beans in the .war
file to demonstrate this new feature of EJB 3.1. Packaging in .war
file makes it easier to access EJBs from a web page.
Create a Java EE application called JSFExample
and include only the JSFExample-war
module. Be sure to enable contexts and dependency injection when creating the Java EE application. If you don't, you cannot use DI.
We will reuse the ConstantsBean
from the JSPExample
application detailed in the Accessing an EJB from JSP recipe in this chapter. Create a new package called packt
and recreate the ConstantsBean
inside of it. However, do not implement the ConstantsBeanRemote
interface. Add the following annotation after the @Stateless annotation in the Constantsbean
class. This will make the EJB visible to the JSF client.
@Named("constants")
Next, create a JSF managed bean called ConstantsManagedBean
. This class will use DI to create and use the ConstantsBean
.
@ManagedBean public class ConstantsManagedBean { @EJB ConstantsBean constants; public ConstantsManagedBean() { } public double getGoldenRatio() { EJBcalling, from JSFreturn constants.getGoldenRatio(); } public double getPI() { return constants.getPI(); } }
To demonstrate the use of both the EJB and the JSF-managed bean, create a JSF page titled index.xhtml
.
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>Constants</title> </h:head> <h:body> <f:view> <h:form> <h:outputLabel for="name"> <h:panelGrid columns="1"> <h:outputText value="Managed Bean Length: #{constantsManagedBean.PI}" /> <h:outputText value="Managed Bean Status: #{constantsManagedBean.goldenRatio}" /> <h:outputText value="EJB Bean Length: #{constants.PI}" /> <h:outputText value="EJB Bean Status: #{constants.goldenRatio}" /> </h:panelGrid> </h:outputLabel> </h:form> </f:view> </h:body> </html>
Execute the application. The following screenshot illustrates its output.
The ConstantsBean
was explained in the previous Accessing an EJB from JSP recipe. The JSF managed bean, ConstantsManagedBean
, was declared as such using the @ManagedBean annotation. DI was used to create and use the ConstantsBean
. The managed bean has two methods, getGoldenRatio
and getPI
, which called the corresponding methods of the ConstantsBean
.
The JSF page used both the ConstantsBean
directly and the managed bean with h:outputText
elements. As a developer you can choose to use either technique though the managed bean approach is no longer necessary.
<h:outputText value= "Managed Bean Length: #{constantsManagedBean.PI}" /> <h:outputText value= "Managed Bean Status: #{constantsManagedBean.goldenRatio}" /> <h:outputText value="EJB Bean Length: #{constants.PI}" /> <h:outputText value= "EJB Bean Status: #{constants.goldenRatio}" />
3.149.241.250