Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
What is JavaServer Faces (JSF)? Framework for web applications – Works on the server side Part of Java Enterprise Edition – Runs on an application server with a JSF container Example: GlassFish Current version – JSF 2.0, JavaServer Faces (JSF)
Some web technologies Client-side technologies (X)HTML JavaScript Cascading Styles Sheets (CSS) Java Applets Server-side technologies Java Servlets JavaServer Pages (JSP) JavaServer Faces (JSF) PHP Microsoft ASP.NET web forms 3JavaServer Faces (JSF)
JSF Pages JSF Pages look like HTML pages Application server translates JSF Pages to HTML pages – The HTML is then send to the client (browser) The translation is dynamic – Depends on the state of Java objects JSF Managed Beans Syntax: #{beanName.propertyName} 4JavaServer Faces (JSF)
NetBeans support 5JavaServer Faces (JSF)
JSF managed beans Value expression – #{beanName.propertyName} – Invokes a method on a Java object Called a JSF managed bean JavaBeans controlled by JSF 6JavaServer Faces (JSF)
JavaBeans No-arguments constructor Properties – Get + set methods – Public type getPropertyName() – Public void setPropertyName(Type newValue) – Can be read-only No set method, only a get method Other methods Implements java.io.Serializable The model component in the Model-View-Controller architecture (MVC) 7JavaServer Faces (JSF)
Scope of managed beans Who can access a bean – and for how – Available throughout a single HTTP request. – Survives navigation to another – Available on a single page / single HTTP – Stored in the HTTP session – Each user has a HTTP session – Time out – Example: Shopping cart – Examples from “Big Java” – Available throughout the life of the application – Accessible to all users and all pages Example: counters 8JavaServer Faces (JSF)
Separation of concerns: Presentation and Business Logic Presentation – User interface – JSF Page Also known as Facelet – Made by graphics designers Business logic – Rules for business decisions – JSF managed JavaBean – Made by Java programmers 9JavaServer Faces (JSF)
Navigation between pages Command button – – Action can be a static name, or a method expression Action=”#{bean.method}” The method produces a String depending on the bean object’s state – The name of the page to navigate to – Similar 10JavaServer Faces (JSF)
JSF components JSF components are translated into HTML elements using value expressions Example – Example: LotteryGame 11JavaServer Faces (JSF)
Three-tier applications Presentation tier – Browser Business logic tier – JSF Container JSF pages + managed beans Managed beans can make connections to a database Storage tier – Database Management System (DBMS) Database 12JavaServer Faces (JSF)
Database connections from JSF managed beans JSF Managed beans can be annotated name”) private DataSource source; – Called “resource injection” – Connection pooling – Using the connection object Connection conn = source.getConnection() 13JavaServer Faces (JSF)
JSF using MVC But where is the controller? Model – JSF managed beans View – JSF page Controller – Javax.faces.webapp.FacesServlet pp/FacesServlet.html pp/FacesServlet.html – Reads web.xml from the project – Dispatches the request to the right JSF page 14JavaServer Faces (JSF)
Declarative programming A program that describes what computation should be performed and not how to compute it. – Examples: – SQL, executed by a DBMS – HTML + CSS, executed by a web browser Executed by a JSF container (application server) name”) Executed by a JSF container (application server) 15JavaServer Faces (JSF)
References and further readings Horstmann: Big Java 4th edition, Wiley 2010 – Chapter 23 Web Applications, page The Java EE 6 Tutorial, Part II The Web Tier – Geary & Horstmann: Core JavaServer Faces, 3 rd edition, Prentice Hall 2010 Burns & Schalk: JavaServer Faces 2.0: The Complete Reference, McGraw-Hill 2011 Hall: JSF 2.0 Tutorials, ${coreservlets.com} – JSF 2.0 Support in NetBeans IDE – JavaServer Faces (JSF)