Monday, October 06, 2014

JSF 2 introduction

faces-config.xml contains
  • definition of Managed beans, 
  • defines navigation rules (map return conditions to results page), 
  • register validators, 
  • declare locates, 
  • inject bean properties, 

Sample

 <faces-config … version="2.2">
 <managed-bean>
 <managed-bean-name>messageHandler</managed-bean-name>
 <managed-bean-class>
 coreservlets.SimpleController2
 </managed-bean-class>
 <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
 <from-view-id>/starting-page.xhtml</from-view-id>
 <navigation-case>
 <from-outcome>return-value-1</from-outcome>
 <to-view-id>/result-page-1.xhtml</to-view-id>
 </navigation-case>
 <navigation-case>
 <from-outcome>return-value-2</from-outcome>
 <to-view-id>/result-page-2.xhtml</to-view-id>
 </navigation-case>

View

 <!DOCTYPE … >
 <html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://xmlns.jcp.org/jsf/html">
 <h:head><title>JSF 2: Basic Navigation Rules</title>
 …
 </h:head>
 <h:body>
 …
 <h:form>
 Your message:
 <h:inputText value="#{simpleController.message}"/>
 <br/>
 <h:commandButton value="Show Results"
 action="#{simpleController.doNavigation}"/>
 </h:form>
 …
 </h:body></html>
 @ManagedBean
 public class SimpleController {
  private String message="";
 // getMessage and setMessage
 public String doNavigation() {
 if (message.trim().length() < 2) {
 return("too-short");
 } else {
 String[] results =
 { "page1", "page2", "page3" };
 return(RandomUtils.randomElement(results));
 }
 }
 }

Managed beans have following scope
  • request
  • application
  • session
  • flow
  • none
Other JSF info
  • web.xml declars extension of url *.jsf
  • You will enter URL as xyz.jsf but actual file name would be xyz.xhtml
  • view technology is facelets
  • JSF has integrated AJAX support (f:ajax tag) and can be thought of as an alternative to jQuery and Dojo etc
  • Event handling
  • built in capabilities for validation
  • Page templating
JSF life cycle
  1. Restore view 
  2. Apply request values; process events 
  3. Process validations; process events 
  4. Update model values; process events
  5. Invoke application; process events
  6. Render response

No comments: