JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.
-
Upload
beverly-skinner -
Category
Documents
-
view
218 -
download
2
Transcript of JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.
![Page 1: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/1.jpg)
JavaServer Faces Antipatterns and Best
Practices Kito D. MannPrincipal Consultant
![Page 2: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/2.jpg)
Kito D. Mannwww.twitter.com/kito99
» Principal Consultant at Virtua» http://www.virtua.com» Training, consulting, architecture, mentoring, » JSF product development
» Author, JavaServer Faces in Action» Founder, JSF Central
» http://www.jsfcentral.com» Internationally recognized speaker
» JavaOne, JavaZone, Devoxx, NFJS, TSSJS, etc.Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
![Page 3: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/3.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Kito D. Mannwww.twitter.com/kito99
» JCP Member» JSF, WebBeans, JSF Portlet Bridge, Portlets
![Page 4: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/4.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
![Page 5: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/5.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
![Page 6: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/6.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Avoid the Big Ball of Mud
» http://www.laputan.org/mud/
![Page 7: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/7.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
ANTIPATTERNS
![Page 8: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/8.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant backing bean
![Page 9: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/9.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant backing bean
» Several hundred or thousand lines
![Page 10: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/10.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant backing bean
» Several hundred or thousand lines» Unnecessary comments
![Page 11: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/11.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant backing bean
» Several hundred or thousand lines» Unnecessary comments» Very complicated view that uses Ajax
![Page 12: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/12.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant backing bean
» Several hundred or thousand lines» Unnecessary comments» Very complicated view that uses Ajax» Business logic in backing beans
![Page 13: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/13.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant backing bean
» Several hundred or thousand lines» Unnecessary comments» Very complicated view that uses Ajax» Business logic in backing beans» Data access logic in backing beans
![Page 14: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/14.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant backing bean
» Refactor» Multiple backing beans» Move code
» Other layers» Utility classes» Other scoped objects
![Page 15: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/15.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
![Page 16: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/16.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Backing bean subclasses domain object
![Page 17: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/17.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Backing bean subclasses domain object
» Violates separation of concerns and OO principles
![Page 18: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/18.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Backing bean subclasses domain object
» Violates separation of concerns and OO principles
![Page 19: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/19.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
![Page 20: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/20.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Flat backing bean hierarchy
![Page 21: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/21.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Flat backing bean hierarchy
![Page 22: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/22.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
public void validateName (FacesContext facesContext, UIComponent sender, Object newValue) throws
ValidatorException { if (!nameExists((String) newValue)) { facesContext.addMessage("newWidgetForm:name",
new FacesMessage("Duplicate widget name")); } }
![Page 23: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/23.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
public void validateName (FacesContext facesContext, UIComponent sender, Object newValue) throws
ValidatorException { if (!nameExists((String) newValue)) { facesContext.addMessage("newWidgetForm:name",
new FacesMessage("Duplicate widget name")); } }
![Page 24: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/24.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies
public void validateName (FacesContext facesContext, UIComponent sender, Object newValue) throws
ValidatorException { if (!nameExists((String) newValue)) { facesContext.addMessage("newWidgetForm:name",
new FacesMessage("Duplicate widget name")); } }
![Page 25: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/25.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies
public void validateName(FacesContext facesContext, UIComponent sender,
Object newValue) throws ValidatorException { if (!nameExists((String) newValue)) { throw new ValidatorException(new FacesMessage( "Duplicate widget name.")); } }
![Page 26: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/26.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies
public void deleteWidget (ActionEvent event) { FacesContext facesContext = FacesContext.getCurrentInstance(); String id = event.getComponent().getClientId(facesContext);
int beginIndex = id.indexOf(":", 0); beginIndex = id.indexOf(":", beginIndex + 1); int endIndex = id.indexOf(":", beginIndex + 1); String rowIndex = id.substring(beginIndex + 1, endIndex); deleteWidget(Integer.parseInt(rowIndex)); }
![Page 27: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/27.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies
public void deleteWidget (ActionEvent event) { FacesContext facesContext = FacesContext.getCurrentInstance(); String id = event.getComponent().getClientId(facesContext);
int beginIndex = id.indexOf(":", 0); beginIndex = id.indexOf(":", beginIndex + 1); int endIndex = id.indexOf(":", beginIndex + 1); String rowIndex = id.substring(beginIndex + 1, endIndex); deleteWidget(Integer.parseInt(rowIndex)); }
![Page 28: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/28.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies<h:dataTable id="widgetTable" value="#{widgetEditorController.widgets}" var="widget"> <h:column> <f:facet name="header">Name</f:facet> <h:outputText value="#{widget.name}" /> </h:column> <h:column> <f:facet name="header">Size</f:facet> <h:outputText value="#{widget.size}" /> </h:column> <h:column> <h:commandLink value="#{widgetEditorController.delete}"> <f:setPropertyActionListener value="widget"
target="#{widgetEditorController.currentWidget}"/> </h:commandLink> </h:column></h:dataTable>
![Page 29: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/29.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies
<h:dataTable id="widgetTable" value="#{widgetEditorController.widgets}"var="widget">
<h:column> <f:facet name="header">Name</f:facet> <h:outputText value="#{widget.name}" /> </h:column> <h:column> <f:facet name="header">Size</f:facet> <h:outputText value="#{widget.size}" /> </h:column> <h:column> <h:commandLink value="#{widgetEditorController.delete(widget)}"/></h:column></h:dataTable>
![Page 30: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/30.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies
» Use value or component bindings
![Page 31: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/31.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Magic string dependencies
» Use value or component bindings» Retrieve the sending UI component in event
listener methods
![Page 32: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/32.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Unnecessary FacesContext lookups
![Page 33: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/33.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Unnecessary FacesContext lookups
» Calling FacesContext.getCurrentInstance() multiple times in the same method
![Page 34: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/34.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Unnecessary FacesContext lookups
» Use a variable
![Page 35: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/35.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Interdependent backing beans
![Page 36: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/36.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Interdependent backing beans
» Factor out behavior to other layers or helper objects» Inject the dependency
![Page 37: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/37.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Interdependent backing beans
![Page 38: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/38.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant view
![Page 39: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/39.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant view
» Similar markup repeated in every page
![Page 40: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/40.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant view
» Similar markup repeated in every page» No templating or reuse
![Page 41: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/41.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant view
» Use templating or includes for any common markup
![Page 42: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/42.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant view
» Use templating or includes for any common markup
» Break up pages into smaller fragments
![Page 43: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/43.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Giant view
» Use templating or includes for any common markup
» Break up pages into smaller fragments» Use composite components
![Page 44: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/44.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
![Page 45: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/45.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Bloated component tree
![Page 46: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/46.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Bloated component tree
» Consume lots of memory» Render large pages» Slower
![Page 47: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/47.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Bloated component tree
» The rendered attribute does not affect the size of the component tree
![Page 48: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/48.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Bloated component tree
» Is Ajax really required?» Consider transient markup (<div>, <span>,
text output, etc.)» Consider stateless views (JSF 2.2)» Investigate dynamic loading features
![Page 49: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/49.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Bloated component tree
» Use a scope other than session» Use <c:if> (sparingly)» Split into separate views
![Page 50: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/50.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
public List<Widget> getWidgets () { WidgetProvider widgetProvider =
new WidgetProvider(); return widgetProvider.getWidgets(); }
![Page 51: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/51.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
public List<User> getUsers () { ELContext elContext = FacesContext.
getCurrentInstance().getELContext(); UserProvider userProvider = (UserProvider) elContext.getELResolver() .getValue(elContext, null,
"userProvider"); return userProvider.getUsers(); }
![Page 52: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/52.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Lack of dependency injection
public List<User> getUsers () { ELContext elContext = FacesContext.
getCurrentInstance().getELContext(); UserProvider userProvider = (UserProvider) elContext.getELResolver() .getValue(elContext, null,
"userProvider"); return userProvider.getUsers(); }
![Page 53: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/53.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Lack of dependency injection
» Don't create singletons or scoped objects with new operator
» Use dependency injection to wire up backing beans to other objects
![Page 54: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/54.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Lack of dependency injection
@Inject private UserProvider userProvider; public UserProvider getUserProvider() { return userProvider; }
public void setUserProvider(UserProvider userProvider) { this.userProvider = userProvider; } public List<User> getUsers() { return userProvider.getUsers(); }
![Page 55: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/55.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
public void getData() { return service.getData();}
![Page 56: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/56.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Getter grabber
public void getData() { return service.getData();}
![Page 57: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/57.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Getter grabber
» Do not perform any expensive operations in getters
» Retrieve data when view is loaded
![Page 58: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/58.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
» widgetViewer.xhtml» WidgetEditorPage.xhtml» wiget_search.xtml
![Page 59: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/59.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Inconsistent artifact names
» widgetViewer.xhtml» WidgetEditorPage.xhtml» wiget_search.xtml
![Page 60: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/60.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Inconsistent artifact names
» Define and enforce naming conventions
![Page 61: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/61.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Initializing backing beans in constructor
» Dependency injection has not taken place
![Page 62: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/62.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Initializing backing beans in constructor
» Lazy initialization» Use @PostConstruct annotation» Initialize before the view is displayed
» JSF 1.2: PhaseListener » <f:view beforePhase="#{myBean.init}">
» JSF 2: BeforeRender event » <f:event type="beforeRender"
listener="#{myBean.init}">
![Page 63: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/63.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Eager view initialization
![Page 64: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/64.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Eager view initialization
» Lazy initialization
![Page 65: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/65.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Lazy library initialization
![Page 66: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/66.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Lazy library initialization
» Causes unnecessary delays for the first user
![Page 67: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/67.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Lazy library initialization
» Initialize expensive objects at startup» Spring ContextLoader» JSF 1.x: ServletContextListener» JSF 2: Load managed bean at startup (eager) with
@PostConstruct
![Page 68: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/68.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Static variables in scoped objects
![Page 69: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/69.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Static variables in scoped objects
» Breaks the semantics of scoped state» Can cause synchronization issues» Does not work in a clustered environment
![Page 70: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/70.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Static variables in scoped objects
» Use an application-scoped object or a constant
![Page 71: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/71.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Non-serializable session-scoped objects
» Make all session, view and conversation-scoped objects serializable
![Page 72: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/72.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Overcomplicated expressions
![Page 73: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/73.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Overcomplicated expressions
» Hard to read» Mixes business logic with display logic
![Page 74: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/74.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Overcomplicated expressions
» Move logic into backing bean properties
![Page 75: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/75.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Exception eating
public List<User> getUsers () { try { return userProvider.getUsers(); } catch (DataAccessException e) { e.printStackTrace(); } return null; }
![Page 76: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/76.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Exception eating
» If you cannot properly handle the exception, don't
» Let exceptions bubble up to the container» Make sure you use a Logger instead of
System.out.println()» Use the JSF2 ExceptionHandler to centralize
exception handling
![Page 77: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/77.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Unnecessary UI Component Bindings
![Page 78: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/78.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Unnecessary UI Component Bindings
» Component bindings can be problematic for non-request scoped beans» http://leadingyouastray.blogspot.com/2010/02/
references-to-uicomponents-in-session.html» Component bindings in view-scoped backing
beans break partial state saving (fixed in JSF 2.2)
![Page 79: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/79.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Unnecessary UI Component Bindings
» Use value bindings» <f:setPropertyActionListener>» Retrieve components from event objects in
event listeners» Use request-scoped holder pattern
![Page 80: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/80.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Backing bean messages
![Page 81: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/81.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Backing bean messages
» Use ordinary JSF messages
![Page 82: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/82.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Backing bean messages
» Use ordinary JSF messages » Create convenience methods in your base
backing bean class
![Page 83: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/83.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Backing bean messages
» Use ordinary JSF messages » Create convenience methods in your base
backing bean class» Issue: scope
» Will redisplay for request scoped beans unless you do a redirect (fixed in JSF 2.0)
![Page 84: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/84.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Immediate overuse
![Page 85: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/85.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Immediate overuse
» Avoid using immediate for non-Command components
![Page 86: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/86.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Immediate overuse
» Avoid using immediate for non-Command components
» Consider putting input controls in a separate form
![Page 87: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/87.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Immediate overuse
» Avoid using immediate for non-Command components
» Consider putting input controls in a separate form
» Use Ajax features» RichFaces: <a4j:ajaxOnly>» JSF 2: <f:ajax ... execute="@this"/>
![Page 88: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/88.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Nameless UI components
<h:form><h:inputText value="#{widgetEditor.currentWidget}" /><h:commandButton value="Submit”
action="#{widgetEditor.save}" /></h:form>
![Page 89: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/89.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Nameless UI components
» Makes testing difficult» Hard to integrate with custom JavaScript or
Ajax features» Increases rendered page size
![Page 90: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/90.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
<h:form id="frm"><h:inputText id="name" value="#{widgetEditor.currentWidget}" /><h:commandButton id="submit" value="Submit”
action="#{widgetEditor.save}" /></h:form>
![Page 91: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/91.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Nameless UI components
» Assign short, meaningful ids» All input controls» Components with Ajax behavior » Naming containers
![Page 92: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/92.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Nameless UI components
» Develop naming conventions» frm» dtb» widgetFrm» widgetDtb
![Page 93: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/93.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
CSS style soup
» Hardcoded styles in JSF views
![Page 94: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/94.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
CSS style soup
» Use CSS selectors instead of inline styles» Most suites let you override default selectors» Develop custom theme if possible
![Page 95: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/95.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Session overload
![Page 96: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/96.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Session overload
» Put beans in request scope when possible» For Ajax, use view scope
» JSF 1.x: not supported» Seam Page scope » Tomahawk <t:saveState> » RichFaces <a4j:keepAlive>
![Page 97: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/97.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Session overload
» Conversation scope» Not implemented natively» JSF 1.x
» Seam, MyFaces Orchestra» MyFaces Trinidad PageFlowScope» Spring Web Flow» Spring 3.1
» JSF 2: CDI
![Page 98: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/98.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
GOTCHAS
![Page 99: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/99.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Null input values
» JSF 1.2: Null values are converted to a 0 or false » https://jsp-spec-public.dev.java.net/issues/
show_bug.cgi?id=183» Tomcat and JBoss
» "-Dorg.apache.el.parser.COERCE_TO_ZERO=false" as a VM parameter.
» JSF 2: javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context parameter
![Page 100: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/100.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Null input values
» JSF 2: javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context parameter
![Page 101: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/101.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Validating empty fields
» JSF 1.x: Validators not called for empty values» JSF 2: javax.faces.VALIDATE_EMPTY_FIELDS
context parameter
![Page 102: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/102.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
BEST PRACTICES
![Page 103: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/103.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Use constants or enums for action method outcomes
public enum Outcome { SUCCESS, FAILURE, PREVIOUS, NEXT, ERROR; @Override public String toString() { return super.toString().toLowerCase(); }}
![Page 104: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/104.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Use constants or enums for action method outcomes
public Outcome updateWidget() { .... return Outcome.SUCCESS; }
![Page 105: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/105.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Build a library of composite components
» Standardize behavior and look and feel
![Page 106: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/106.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Build a library of composite components
» Standardize behavior and look and feel» Examples
» Input control layout» Panels» Buttons» Icons» Forms
![Page 107: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/107.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Build a library of composite components
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:h="http://java.sun.com/jsf/html"xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface><composite:attribute name="label" /><composite:attribute name="value" required="true" />
</composite:interface>
<composite:implementation><h:outputLabel for="input" value="#{cc.attrs.label}" /><h:inputText id="input" value="#{cc.attrs.value}" />
</composite:implementation></html>
![Page 108: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/108.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Development team best practices
» Documentation» Coding guidelines» Code review
» Static analysis» Build process» Continuous integration» Unit testing
![Page 109: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/109.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
More Best Practices
» Externalize all messages to resource bundles» Use another object for state in PhaseListeners
![Page 110: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/110.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
R
![Page 111: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/111.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
RT
![Page 112: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/112.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
RTF
![Page 113: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/113.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
RTFM
![Page 114: JavaServer Faces Antipatterns and Best Practices Kito D. Mann Principal Consultant.](https://reader035.fdocuments.us/reader035/viewer/2022062714/56649d155503460f949ea840/html5/thumbnails/114.jpg)
Copyright (C) 2010-14 Virtua, Inc. All rights reserved.
Resources
» http://www.javaserverfaces.org» http://www.jsfcentral.com» Books
» Core JSF» JSF: The Complete Reference