• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
java ee 6 Petcatalog
 

java ee 6 Petcatalog

on

  • 2,529 views

 

Statistics

Views

Total Views
2,529
Views on SlideShare
2,439
Embed Views
90

Actions

Likes
1
Downloads
58
Comments
0

5 Embeds 90

http://owen.com 73
http://localhost 7
http://www.slideshare.net 4
http://sysdecom-projects.com 4
http://www.lmodules.com 2

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 , , . The 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 element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The 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 "Managed Beans" 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 , , . The 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 element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The 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 , , . The 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 element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The 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 , , . The 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 element displays the userNumber property of the UserNumberBean and also prompts the user to enter a new number in the textfield. The 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.

java ee 6 Petcatalog java ee 6 Petcatalog Presentation Transcript

  • JavaEE 6 Pet Catalog Carol McDonald Java Architect
  • About the Speaker
    • Carol cDonald:
      • Java Architect at Sun Microsystems
      • Before Sun, worked on software development of:
        • Application to manage car Loans for Toyota (>10 million loans)
        • Pharmaceutical Intranet apps ( Roche Switzerland)
        • Telecom Network Mgmt ( Digital France)
        • X.400 Email Server ( IBM Germany)
  • Sample Application
  • Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
  • 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
  • Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
  • 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 (&quot; select i from Item as i &quot;); q. setMaxResults ( batchSize ); q. setFirstResult ( firstItem ); return q.getResultList(); } }
  • 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
  • What is a JSF UI Component? Some Standard Components
  • JSF Web Framework View Controller Model Request Service Interface Events
  • Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
  • List Items page Data Table
  • list.xhtml Page < h:dataTable var=&quot; row &quot; value=&quot;#{ catalog.items }&quot;> <h:column> … < h:commandLink action=&quot;#{ catalog.showDetail ( row )}&quot; value=&quot;#{ row.name }&quot; /> <h:column> ... < h:graphicImage name=&quot;#{ row.imagethumburl }&quot; /> … <h:column> < h:outputTex t value =&quot;#{ row.price }&quot;/> ... </h:dataTable>
  • Binding UI to Managed Bean list.xhtml <h: dataTable value =&quot;#{ catalog .items }&quot; @ManagedBean (name = &quot;Catalog&quot;) @SessionScoped public class Catalog { }
  • Managed Bean Value Binding
    • component value attribute :
      • The property getter is invoked when the component is rendered
    <h: dataTable value =&quot;#{catalog .items }&quot;/>
      • The property setter is invoked when the component input is processed
    <h: input Text value =&quot;#{ user .name }&quot;/> GET SET
  • list.xhtml Page < h:dataTable var=&quot; row &quot; value=&quot;#{ catalog.items }&quot;> <h:column> … < h:commandLink action=&quot;#{ catalog.showDetail ( row )}&quot; value=&quot;#{ row.name }&quot; /> <h:column> ... < h:graphicImage name=&quot;#{ row.imagethumburl }&quot; /> … <h:column> < h:outputTex t value =&quot;#{ row.price }&quot;/> ... </h:dataTable>
  • UIComponents Binding to a Managed Bean public class Catalog { private List<Item> items = null; public List<Item> getItems() { . . . return items; } . . . } <h: dataTable value =&quot;#{ catalog .items }&quot; 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
  • 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()); } . . .
  • Relationship Between Objects findRange() via EntityBean #{catalog.items} Item Entity ItemDAO Catalog list.xhtml
  • List Items page Link to see Item Details
  • list.jsp <h:dataTable var=&quot;row&quot; value=&quot;#{catalog.items}&quot; > <h:column> <f:facet name=&quot;header&quot;> <h:outputText value=&quot;#{msgs.Name}&quot;/> </f:facet> <h :commandLink action = &quot;#{catalog.showDetail(row)}&quot; value=&quot;#{row.name}&quot; /> </h:column> <h:column> <f:facet name=&quot;header&quot;> <h:outputText value=&quot;#{msgs.Photo}&quot;/> </f:facet> <h:graphicImage library=&quot;images&quot; name=&quot;#{row.imagethumburl}&quot;/> </h:column> <h:column> <f:facet name=&quot;header&quot;> <h:outputText value=&quot;#{msgs.Price}&quot;/> </f:facet> <h:outputText value=&quot;#{row.price}&quot; /> </h:column> </h:dataTable> Click causes Action Event
  • 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
  • Invoke Application Phase
      • UICommand Action : Refers to a backing bean method that performs navigation processing and returns a logical outcome String
    • <h: commandLink action =&quot;#{ catalog.showDetail(row) } &quot; ... />
    • public class Catalog {
    • public String showDetail (Item item ) {
    • this.item = item;
    • return &quot;detail&quot;;
    • }
  • Standard request processing lifecycle JavaServer Faces Lifecycle Request Restore View Response Render Response Apply Request Values Invoke Application Update Model Values Process Validations
  • Item Detail Page
  • detail.jsp . . . <h:form> <h: outputStylesheet name=&quot;css/styles.css&quot;/> <h:panelGrid columns=&quot;2&quot; border=&quot;0&quot; > <h:outputText value=&quot;#{ msgs.Name }:&quot;/> <h:outputText value=&quot;#{ catalog.item.name }&quot; title=&quot;Name&quot; /> <h:outputText value=&quot;#{ msgs.Description }:&quot;/> <h:outputText value=&quot;#{ catalog.item.description }&quot; title=&quot;Description&quot; /> <h:outputText value=&quot;#{msgs.Photo}:&quot;/> <h:graphicImage library=&quot;images&quot; name=&quot;#{catalog.item.imageurl}&quot; title=&quot;url&quot; /> <h:outputText value=&quot;#{msgs.Price}:&quot;/> <h:outputText value=&quot;#{catalog.item.price}&quot; title=&quot;Price&quot; /> </h:panelGrid> <h:commandButton id=&quot;back&quot; value=&quot;Back&quot; action=&quot;list&quot;/> </h:form> Calls itemController.getItem().getName()
  • Resources <application> <resource-bundle> <base-name>web.WebMessages</base-name> <var>msgs</var> </resource-bundle> </application>
  • UIOutput component value attribute <h: outputText value=&quot;#{catalog .item.name }&quot; title=&quot;Name&quot; />
    • public class Catalog {
    • private Item item ;
    • public Item getItem() {
    • return item;
    • }
    Calls catalog.getItem().getName()
  • JavaEE 6 Carol McDonald Java Architect Sun Microsystems