21
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Embed Size (px)

Citation preview

Page 1: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

CSCI 6962: Server-side Design and Programming

Introduction to Java Server Faces

Page 2: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

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

Page 3: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Java Server Faces

• Model-based architecture

JJava 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.)

JJSF

JJSF

JBean

JBean

Page 4: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

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

Page 5: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Creating a JSF

• New Project Java Web Web Application Enter name and press NEXT twice (not finish)

• Check Java Server Facesunder Frameworks

Page 6: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Creating a JSF Page

• New File Web JSF Page• Make sure Facelets selected

Page 7: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

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)

Page 8: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

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)

Page 9: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Defining Member Variables

• Data stored or computed by bean

• Any form elements bean binds to

• Must be type String for text elements

Page 10: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

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

Page 11: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Getter/Setter Methods

• Can use ALT-INSERT to do automatically

Page 12: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Java Server Faces Tags

• JSF tags instead of html

Page 13: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Java Server Faces Tags

• JSF tags instead of html– Tags of form <h:tagname…> or <f:tagname…>

(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/

Page 14: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Java Server Faces Tags

• Translated to html in response sent to client

Page 15: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

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:<h: inputText value=‘#{beanname.variablename}’/>

Page 16: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

JSF Text Output• Like input field, but “read only”– Variable value automatically inserted into response– Translated into simple text

• Syntax:<h: outputLabel value=‘#{beanname.variablename}’/>

Page 17: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

JSF Submit Buttons

• Syntax:<h:commandButton value=‘label on button' action=“page to redirect to"/>

• Note that address defined in button rather than form– Crucial for bean-directed redirection

Page 18: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Computed Values

• Can bind to computed values instead of bean variables• Example: – Bean computes cost of widget order– Receipt gets and displays that value

Page 19: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

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

Page 20: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

Initial Values

• Can load values into form elements when page loaded• Assign corresponding bean variables initial values

Page 21: CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces

JSF/Bean Lifecycle

Request for JSF page(initial form request)

JJSF

JBean

bean constructed (if it does not already exist)

bean values loaded into bound elements using get methods

Jhtml

Sent as html form

Form submitted

Parameter values parsed parameter values

from bound elements stored in bean using set methods