java ee 6 Petcatalog

2,521 views

Published on

  • Be the first to comment

java ee 6 Petcatalog

  1. 1. JavaEE 6 Pet Catalog Carol McDonald Java Architect
  2. 2. About the Speaker <ul><li>Carol cDonald: </li><ul><li>Java Architect at Sun Microsystems
  3. 3. Before Sun, worked on software development of: </li><ul><li>Application to manage car Loans for Toyota (>10 million loans)
  4. 4. Pharmaceutical Intranet apps ( Roche Switzerland)
  5. 5. Telecom Network Mgmt ( Digital France)
  6. 6. X.400 Email Server ( IBM Germany) </li></ul></ul></ul>
  7. 7. Sample Application
  8. 8. Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
  9. 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. 10. Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
  11. 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 (&quot; select i from Item as i &quot;); q. setMaxResults ( batchSize ); q. setFirstResult ( firstItem ); return q.getResultList(); } }
  12. 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. 13. What is a JSF UI Component? Some Standard Components
  14. 14. JSF Web Framework View Controller Model Request Service Interface Events
  15. 15. Catalog Java EE Application DB Registration Application Managed Bean JSF Components Session Bean Entity Class Catalog Item ManagedBean
  16. 16. List Items page Data Table
  17. 17. 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>
  18. 18. Binding UI to Managed Bean list.xhtml <h: dataTable value =&quot;#{ catalog .items }&quot; @ManagedBean (name = &quot;Catalog&quot;) @SessionScoped public class Catalog { }
  19. 19. Managed Bean Value Binding <ul><li>component value attribute : </li></ul><ul><ul><li>The property getter is invoked when the component is rendered </li></ul></ul><h: dataTable value =&quot;#{catalog .items }&quot;/> <ul><ul><li>The property setter is invoked when the component input is processed </li></ul></ul><h: input Text value =&quot;#{ user .name }&quot;/> GET SET
  20. 20. 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>
  21. 21. 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
  22. 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()); } . . .
  23. 23. Relationship Between Objects findRange() via EntityBean #{catalog.items} Item Entity ItemDAO Catalog list.xhtml
  24. 24. List Items page Link to see Item Details
  25. 25. 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
  26. 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
  27. 27. Invoke Application Phase <ul><ul><li>UICommand Action : Refers to a backing bean method that performs navigation processing and returns a logical outcome String </li></ul></ul><ul><li><h: commandLink action =&quot;#{ catalog.showDetail(row) } &quot; ... /> </li></ul><ul><li>public class Catalog {
  28. 28. public String showDetail (Item item ) {
  29. 29. this.item = item;
  30. 30. return &quot;detail&quot;;
  31. 31. } </li></ul>
  32. 32. Standard request processing lifecycle JavaServer Faces Lifecycle Request Restore View Response Render Response Apply Request Values Invoke Application Update Model Values Process Validations
  33. 33. Item Detail Page
  34. 34. 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()
  35. 35. Resources <application> <resource-bundle> <base-name>web.WebMessages</base-name> <var>msgs</var> </resource-bundle> </application>
  36. 36. UIOutput component value attribute <h: outputText value=&quot;#{catalog .item.name }&quot; title=&quot;Name&quot; /> <ul><li>public class Catalog {
  37. 37. private Item item ;
  38. 38. public Item getItem() {
  39. 39. return item;
  40. 40. } </li></ul>Calls catalog.getItem().getName()
  41. 41. JavaEE 6 Carol McDonald Java Architect Sun Microsystems

×