Download presentation
Presentation is loading. Please wait.
Published byAngelica Woods Modified over 9 years ago
1
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces
2
Outline Model-based architectures and value binding Creating JSF page and managed beans in NetBeans Adding member variables to managed beans Basic JSF tags Binding variables to JSF form elements
3
Java Server Faces Model-based architecture J Java Server Faces page User interface components Managed Bean Support class Data storage and manipulation Validation Navigation and redirection Long term data storage (sessions, databases, etc.) J JSF J Bean
4
Java Server Faces Value Binding Form elements can be directly linked to variables inside bean Changing values on form changes value of those variables when form submitted
5
Creating a JSF New Project Java Web Web Application Enter name and press NEXT twice (not finish) Check Java Server Faces under Frameworks
6
Creating a JSF Page New File Web JSF Page Make sure Facelets selected
7
Creating a Managed Bean New File Java Server Faces JSF Managed Bean Give it a name and a package Set its scope to request (for now)
8
Managed Beans Generated bean file contains – Name that any JSF can use to refer to it – Scope for which bean exists Request (single page) Session (multiple pages in same user session) Application (accessible by all user sessions)
9
Defining Member Variables Data stored or computed by bean Any form elements bean binds to Must be type String for text elements
10
Getter/Setter Methods Each member variable variable should have methods – public String getVariable() – public void setVariable(String) Required if variable bound to form element – Get called when page loaded to insert value – Set called when form submitted to change bean value Done “behind the scenes” with request.getParameter calls
11
Getter/Setter Methods Can use ALT-INSERT to do automatically
12
Java Server Faces Tags JSF tags instead of html
13
Java Server Faces Tags JSF tags instead of html – Tags of form or (XML syntax) – Many similar in form to html tags – Tag libraries set in enclosing html tag – Reference: http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/
14
Java Server Faces Tags Translated to html in response sent to client
15
JSF Text Input Goal: Bind input field to member variable in bean – Form submitted variable value updated automatically – Page sent back as response variable value automatically inserted Syntax:
16
JSF Text Output Like input field, but “read only” – Variable value automatically inserted into response – Translated into simple text Syntax:
17
JSF Submit Buttons Syntax: Note that address defined in button rather than form – Crucial for bean-directed redirection
18
Computed Values Can bind to computed values instead of bean variables Example: – Bean computes cost of widget order – Receipt gets and displays that value
19
Computed Values Must define get and set method for input field Must define set method for input field Cost not a member variable getCost computed from member variables
20
Initial Values Can load values into form elements when page loaded Assign corresponding bean variables initial values
21
JSF/Bean Lifecycle Request for JSF page (initial form request) J JSF J Bean bean constructed (if it does not already exist) bean values loaded into bound elements using get methods J html Sent as html form Form submitted Parameter values parsed parameter values from bound elements stored in bean using set methods
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.