Java Server FacesJSF and RBAC Web Application Zongwei
Java. Server Faces(JSF) and RBAC Web Application Zongwei Yuan 1
Why Java. Server Faces � Model-view-controller (MVC) architecture � Easy to � Drop components onto a web page by adding component tags. � Bind components on a page to server-side data. � Wire component-generated events to server-side application code. � Save and restore application state beyond the life of server requests. � Reuse and extend components through customization. � Provide important services: � Data conversion � Validation and error handling � Internationalization � Custom component � Alternative render � Tool support � Extensibility � Install JSF � Jsf-api. jar , jsf-impl. jar and jstl. jar 2
Sample JSF File index. jsp 1. <html> 2. <%@ taglib uri="http: //java. sun. com/jsf/core" prefix="f" %> 3. <%@ taglib uri="http: //java. sun. com/jsf/html" prefix="h" %> 4. <f: view> 6. <head> <title>A Simple Java. Server Faces Application</title> </head> 9. <body> 10. <h: form> 11. <h 3>Please enter your name and password. </h 3> 12. <table> 13. <tr> 14. <td>Name: </td> 15. <td><h: input. Text value="#{user. name}"/> </td> 16. </tr> 17. <tr> 18. <td>Password: </td> 19. <td><h: input. Secret value="#{user. password}"/> </td> 20. </tr> 21. </table> 22. <p> <h: command. Button value="Login" action="login"/> </p> 25. </h: form> 26. </body> 27. </f: view> 28. </html> 3
Managed bean �Definition �Bean Properties � T get. Foo() � void set. Foo(T new. Value) �Value binding expression � <h: output. Text value="#{user. name}"/> �Bean scope � Request � Session � application �Backing beans 4
Configure Beans � WEB-INF/web. xml <web-app> <context-param> <param-name>javax. faces. CONFIG_FILES</param-name> <param-value>WEB-INF/navigation. xml, WEB-INF/faces-configure. xml</param-value> </context-param>. . . </web-app> � WEB-INF/faces-configure. xml <faces-config> <managed-bean-name>user</managed-bean-name> <managed-bean-class>com. corejsf. User. Bean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config> � In faces-configure. xml we can setting property values initializing lists and maps collection. changing bean definitions (for backing bean) string conversions (convert text value to different data type) 5
Value Binding expressions (1) � Value binding � <h: input. Text rendered="#{!bean. hide}". . . /> � Reference to implicit objects � header. Values � param. Values � cookie � init. Param � request. Scope � session. Scope � application. Scope � faces. Context � view 6
Value binding expressions (2) �Immediate and deferred evaluation expression (${ } and #{ }) �Composite expressions: � arithmetic operators + - * / % � relational operators < <= > >= == != � logical operators && || ! � the “empty”, “not” operator. � the ternary ? : selection operator �Method binding � <h: command. Button action="#{user. check. Password}"/> � Attributes can bind to method: � � action validator action. Listener value. Change. Listener 7
Navigation �Navigation rules �In JSF file: � <h: command. Button label="Login" action="login"/> �In faces-configure. xml: � <navigation-rule> <from-view-id>/index. jsp</from-view-id> <navigation-case> <from-outcome>login</from-outcome> <to-view-id>/welcome. jsp</to-view-id> </navigation-case> </navigation-rule> �Dynamic navigation � <h: command. Button label="Login“ action="#{login. Controller. verify. User}"/> � <from-action>#{quiz. answer. Action}</from-action> 8
Load Property Bundle � <f: load. Bundle basename="bundle. messages" var="msg"/> � <h: output. Text value="#{msg. inputname_header}"/> � localization 9
Standard JSF Tags �Core library tag total 18 � <%@ taglib uri="http: //java. sun. com/jsf/core" prefix="f" %> �HTML library tag total 25 � <%@ taglib uri="http: //java. sun. com/jsf/html" prefix="h" %> �Such as: <f: view> <h: form>. . . </h: form> </f: view> 10
Core Tags � � � � � view subview facet attribute param action. Listener value. Change. Listener convert. Date. Time convert. Number validator validate. Double. Range validate. Length validate. Long. Range load. Bundle selectitems selectitem verbatim Creates the top-level view Creates a subview of a view Adds a facet to a component Adds an attribute (key/value) to a component Adds a parameter to a component Adds an action listener to a component Adds a valuechange listener to a component Adds an arbitrary converter to a component Adds a datetime converter to a component Adds a number converter to a component Adds a validator to a component Validates a double range for a component's value Validates the length of a component's value Validates a long range for a component's value Loads a resource bundle, stores properties as a Map Specifies items for a select one or select many component Specifies an item for a select one or select many component Adds markup to a JSF page 11
HTML Tags � � � � � � � form input. Textarea input. Secret input. Hidden output. Label output. Link output. Format output. Text command. Button command. Link messages graphic. Image select. One. Listbox select. One. Menu select. One. Radio select. Boolean. Checkbox select. Many. Listbox select. Many. Menu panel. Grid panel. Group data. Table column HTML form Single-line text input control Multiline text input control Password input control Hidden field Label for another component for accessibility HTML anchor Like output. Text, but formats compound messages Single-line text output Button: submit, reset, or pushbutton Link that acts like a pushbutton Displays the most recent message for a component Displays all messages Displays an image Single-select listbox Single-select menu Set of radio buttons Checkbox Set of checkboxes Multiselect listbox Multiselect menu HTML table Two or more components that are laid out as one A feature-rich table control Column in a data. Table 12
Tag Attribute �Shared, unique, �converter, validator, values �Html and dhtml event attribute �Action and action. Listener �Rendering and style 13
JSTL tags � Reference � <%@ taglib uri="http: //java. sun. com/jsp/jstl/core" prefix="c" %> � <%@ taglib uri="http: //java. sun. com/jsp/jstl/functions" prefix="fn" %> � Tags � Core � Functions � Database � XML � I 18 N (format) � Examples � <x: choose> � <sql: update> � <c: if> � <c: out> � <c: set> 14
h: form �Has most of HTML and DHTML event attributes �Has no method and action �Use only post method �Navigation submitted by components �Works with Java. Script 15
Textfield and Textarea �immediate, required, redisplay attribute �<h: output. Format> <h: output. Format value="{0} is {1} years old"> <f: param value=“Tom"/> <f: param value="38"/> </h: output. Format> 16
Buttons and links �Action and action. Listener attribute �Immediate attribute 17
Message tag �<h: message> and <h: mesages> �Some special attributes: �global. Only �for �show. Detail �error. Class � Information � Warning � Error � fatal 18
Data table (1) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. <%@ taglib uri="http: //java. sun. com/jsf/core" prefix="f" %> <%@ taglib uri="http: //java. sun. com/jsf/html" prefix="h" %> <html> <f: view> <head> <f: load. Bundle basename="com. corejsf. messages" var="msgs"/> <title> <h: output. Text value="#{msgs. window. Title}"/> </title> </head> <body> <h: output. Text value="#{msgs. page. Title}"/> <p> <h: form> <h: data. Table value="#{table. Data. names}” var="name"> <h: column> <h: output. Text value="#{name. last}"/> <f: verbatim>, </f: verbatim> </h: column> <h: output. Text value="#{name. first}"/> </h: column> </h: data. Table> </h: form> </body> </f: view> </html> 19
Data table (2) � Data – one of follow types: � Array � java. util. List � java. sql. Result. Set � javax. servlet. jsp. jstl. sql. Result � javax. faces. model. Data. Model Array. Data. Model List. Data. Model Result. Set. Data. Model Scalar. Data. Model � Header and footer facet <h: column> <f: facet name="header"> <%-- header components go here --%> </f: facet> </h: column> � Scrolling � scroll bar. use <div> � pageant. use page widget 20
Conversion and Validation �Standard Conversion tag �<f: convert. Number> �<f: convert. Date. Time> <h: input. Text value="#{payment. amount}"> <f: convert. Number min. Fraction. Digits="2"/> </h: input. Text> �Use converter attribute �<h: output. Text value="#{payment. date}" converter="javax. faces. Date. Time"/> 21
Using standard validators � Built-in validation � Checking the length of a string � Checking limits for a numerical value � Checking that a value has been supplied � Validation tag � f: validate. Double. Range � f: validate. Long. Range � f: validate. Length � Overwrite messages � create your own message bundle property � overwrite message property value on the same key � update configure. xml file to load the new bundle � “required” attribute � Bypass validation � <h: command. Button value="Cancel" action="cancel" immediate="true"/> 22
Implement custom converter and validator �Converter � Object get. As. Object(Faces. Context context, UIComponent component, String new. Value) String get. As. String(Faces. Context context, UIComponent component, Object value) �Validator �Implement interface javax. faces. validator. Validator �Register in configure. xml �Validate with bean method � <h: input. Text id="card" value="#{payment. card}" required="true" validator="#{payment. luhn. Check}"/> � public class Payment. Bean { . . . public void luhn. Check(Faces. Context context, UIComponent component, Object value) {. . . } } 23
Event Handling (1) � JSF support 3 kind events: � value change events � action events � phase events � Request life cycle � Restore View � Apply Request Values � Process Validations � Update Model Values � Invoke Application � Render Response � action an action. Listener attribute � Action. Listener � Action – for user interfaces logic and interface info (first) – for business logic (second triggered) 24
Event Handling (2) � Use action listener tag � f: action. Listener and f: value. Change. Listener <h: select. One. Menu value="#{form. country}" onchange="submit()"> <f: value. Change. Listener type="com. corejsf. Country. Listener"/> <f: select. Items value="#{form. country. Names}"/> </h: select. One. Menu> � Listener implements public class Country. Listener implements Value. Change. Listener { private static final String US = "United States"; public void process. Value. Change(Value. Change. Event event) { Faces. Context context = Faces. Context. get. Current. Instance(); if (US. equals((String) event. get. New. Value())) context. get. View. Root(). set. Locale(Locale. US); else context. get. View. Root(). set. Locale(Locale. CANADA); } } 25
Event Handling (3) �“immediate” to bypass other component validation � Add immediate attribute � Calling Faces. Context. render. Response() �Phase event listener � In configure. xml <faces-config> <lifecycle> <phase-listener>com. corejsf. Phase. Tracker</phase-listener> </lifecycle> </faces-config> � Listener implements interface javax. faces. event. Phase. Listener Phase. Id get. Phase. Id() � void after. Phase(Phase. Event) � void before. Phase(Phase. Event) � 26
My. Faces project �Introduction � Compatible with tomcat and easy to deploy � Rich debugging and diagnostic info � Rich components (pagination) � Provide eclipse plug-in �Sub project � Tomahawk � Trinidad � Ext. Val (Extensions Validator) �Installation and deployment �http: //myfaces. apache. org/gettingstarted. html � http: //www. irian. at/myfaces/ �Facelets � Template, composite component 27
RBAC web app on My. Faces �Database access application �User View �Source code example � http: //www-bd. fnal. gov/rbac/inputname. jsf 28
- Slides: 28