8. Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
9. Simple Mapping Mapping defaults to matching column name . Only configure if entity field and table column names are different public class Item { int id; String name; String description; String url ; } @Entity @Column(name=“DECS”) @Id Item ID NAME DESC URL
10. Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
11. Stateless Session EJB, JPA Query @Stateless public class ItemDAO @PersistenceContext(unitName=”PetCatalogPu”) EntityManager em; public List<Item> findRange (int firstItem , int batchSize ) { Query q = em . createQuery (" select i from Item as i "); q. setMaxResults ( batchSize ); q. setFirstResult ( firstItem ); return q.getResultList(); } }
12. JSF Component, Event Driven Web Framework Pages composed of server side components View Root Form id=guess Managed Bean getUserNumber() setUserNumber(..) guess() Business Service guess() Events call ManagedBean methods UIIput id=guessNum UICommand id=submit
13. What is a JSF UI Component? Some Standard Components
14. JSF Web Framework View Controller Model Request Service Interface Events
15. Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
21. UIComponents Binding to a Managed Bean public class Catalog { private List<Item> items = null; public List<Item> getItems() { . . . return items; } . . . } <h: dataTable value ="#{ catalog .items }" Managed Bean property Managed Bean dataTable useful to show a Collection in a table Client Server A B C Backing Bean Form c1 A c2 B c3 C c1 c2 c3 A B C Page
22. Managed Bean: ItemController public class ItemController { private List<Item> items = null; @EJB private ItemDAO itemDAO ; public List<Item> getItems() { getPagingInfo(); return itemDAO.findRange(pagingInfo.getBatchSize(), pagingInfo.getFirstItem()); } . . .
26. JSF controller handles action events, such as submitting a form or linking to another page. JSF Standard request processing lifecycle Request Restore View Response Render Response Apply Request Values Invoke Application Update Model Values Process Validations action events
To obtain an container managed EntityManager instance, inject the entity manager into the application component: @PersistenceContext EntityManager em; you don't need any cm lifecycle methods With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction. JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
note the taglib declaration. JSF comes with its own set of JSP custom tag libraries. Here we declare two JSF tag libraries, html and core. The html JSF tag library contains HTML rendering tags while the core JSF tag library contains JSF core tags. These libraries expose most of the JSF components, validators, event registers, and so on. Note that there are several UI components in this page. And they are <h:output_text>, <h:inputText>, <h:commandButton>. The <h:output_text>elements contains value attributes whose values come from the property values of a JavaBean called UserNumberBean. The names of these properties of this bean are called minimum and maximum. The <h:inputText> element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The <h:commandButton> displays a button that can be pressed for submitting the form data. ·form tag represents an input form with which the user can submit data to the server.
This slide shows the relationship between JSP page and backing bean declaration in the application configuration file. Every backing bean needs to be declared in the application configuration file. Here in this guessNumber example, there is only one bean called UserNumberBean, which has two properties called minimum and maximum whose types and initial values are also defined. In the JSP page, you refer to these properties using JSF EL expression notation like #{UserNumberBean.minimum} or #{UserNumberBean.maximum}. to provide an event-based, JavaBean model way of interacting with application data, JavaServer Faces provides an easy-to-use mechanism by which Web-accessible user interface components are bound to server-side JavaBeans that are registered as &quot;Managed Beans&quot; in an XML file (faces-config.xml). Beans are bound to a user interface with a simple-to-use Expression Language, which is almost identical to JSTL's Expression Language syntax.
note the taglib declaration. JSF comes with its own set of JSP custom tag libraries. Here we declare two JSF tag libraries, html and core. The html JSF tag library contains HTML rendering tags while the core JSF tag library contains JSF core tags. These libraries expose most of the JSF components, validators, event registers, and so on. Note that there are several UI components in this page. And they are <h:output_text>, <h:inputText>, <h:commandButton>. The <h:output_text>elements contains value attributes whose values come from the property values of a JavaBean called UserNumberBean. The names of these properties of this bean are called minimum and maximum. The <h:inputText> element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The <h:commandButton> displays a button that can be pressed for submitting the form data. ·form tag represents an input form with which the user can submit data to the server.
The value attribute of a dataTable tag references the data to be included in the table. The var attribute specifies a name that is used by the components within the dataTable tag as an alias to the data referenced in the value attribute of dataTable. In the dataTable tag from the List.jsp page, the value attribute points to a list of catalog items. The var attribute points to a single item in that list. As the UIData component iterates through the list, each reference to dataTableItem points to the current item in the list.
getItems() method wraps a List of items, returned from the CatalogFacade Stateless Session EJB, in a DataModel. dataTable, supports data binding to a collection of data objects represented by a DataModel instance, which is the current value of this component itself. The data collection underlying a DataModel instance is modeled as a collection of row objects that can be accessed by a row index. The APIs provide mechanisms to position to a specified row index, and to retrieve an object that represents the data that corresponds to the current row index.
note the taglib declaration. JSF comes with its own set of JSP custom tag libraries. Here we declare two JSF tag libraries, html and core. The html JSF tag library contains HTML rendering tags while the core JSF tag library contains JSF core tags. These libraries expose most of the JSF components, validators, event registers, and so on. Note that there are several UI components in this page. And they are <h:output_text>, <h:inputText>, <h:commandButton>. The <h:output_text>elements contains value attributes whose values come from the property values of a JavaBean called UserNumberBean. The names of these properties of this bean are called minimum and maximum. The <h:inputText> element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The <h:commandButton> displays a button that can be pressed for submitting the form data. ·form tag represents an input form with which the user can submit data to the server.
At the invoke application phase– the JSF controller invokes the application to handle Form submissions. The component values will have been converted, validated, and applied to the model objects, so you can now use them to execute the application's business logic. At this phase, you also get to specify the next logical view for a given sequence or number of possible sequences. You do this by defining a specific outcome for a successful form submission and returning that outcome. For example: on successful outcome, move the user to the next page. For this navigation to work, you will have to create a mapping to the successful outcome as a navigation rule in the faces-config.xml file. Once the navigation occurs, you move to the final phase of the lifecycle.
note the taglib declaration. JSF comes with its own set of JSP custom tag libraries. Here we declare two JSF tag libraries, html and core. The html JSF tag library contains HTML rendering tags while the core JSF tag library contains JSF core tags. These libraries expose most of the JSF components, validators, event registers, and so on. Note that there are several UI components in this page. And they are <h:output_text>, <h:inputText>, <h:commandButton>. The <h:output_text>elements contains value attributes whose values come from the property values of a JavaBean called UserNumberBean. The names of these properties of this bean are called minimum and maximum. The <h:inputText> element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The <h:commandButton> displays a button that can be pressed for submitting the form data. ·form tag represents an input form with which the user can submit data to the server.
At the invoke application phase– the JSF controller invokes the application to handle Form submissions. The component values will have been converted, validated, and applied to the model objects, so you can now use them to execute the application's business logic. At this phase, you also get to specify the next logical view for a given sequence or number of possible sequences. You do this by defining a specific outcome for a successful form submission and returning that outcome. For example: on successful outcome, move the user to the next page. For this navigation to work, you will have to create a mapping to the successful outcome as a navigation rule in the faces-config.xml file. Once the navigation occurs, you move to the final phase of the lifecycle.