Java Technologies Lab 4
Total Page:16
File Type:pdf, Size:1020Kb
Java Technologies Lab 4 Java Server Faces Setup ● NetBeans Maven Project - Project Properties / Frameworks / Add JSF ● pom.xml remains the same, JSF is part of Java EE specifications <artifactId>javaee-web-api</artifactId> ● web.xml ● register JSF FrontController servlet, called FacesServlet ● map the URL pattern /faces/* web.xml in a JSF Project <web-app ...> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class> javax.faces.webapp.FacesServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> ... </web-app> Checking your setup ● Create new JSF page: index.xhtml <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h:outputText value="Hello from Facelets" style="font-size: xx-large"/> </h:body> </html> ● Run: http://localhost:8080/HelloWorld/faces/index.xhtml XHTML represents "XML well-formed" HTML documents. "Rich" JSF implementations ● PrimeFaces, IceFaces, RichFaces, HighFaces, BootsFaces, etc. ● Components: "PrimeFaces is a popular open source framework for JavaServer Faces featuring over 100 components, touch optimized mobilekit, client side validation, theme engine and more." ● Productivity: "Allocate your valuable time to business logic rather than dealing with the complex user interface requirements." ● Responsive How to use PrimeFaces https://www.primefaces.org/showcase ● pom.xml <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>7.0</version> </dependency> ● "Enhance" your previous page <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:body> <h:outputText value="Hello from PrimeFaces"/> <p:clock displayMode="analog" /> </h:body> </html> faces-config.xml ● Messages, supported locales, listeners, navigation rules (page flow), etc. ● To create it: "New JSF Configuration" <faces-config ...> <application> <resource-bundle> <base-name>Messages</base-name> <var>msg</var> </resource-bundle> </application> ... </faces-config> Messages.properties ● Create this file in src/main/resources ● Add key=value pairs greeting = Hello from Facelets app.name = My First JSF Application ● Use the messages in your pages <h:outputText value="#{msg.greeting}"/> <h:outputText value="#{msg['app.name']}"/> Web Pages ● /admin ● /config ● /pages /edit /view ● /resources ● main.xhtml, error.xhtml, forbidden.xhtml ● WEB-INF: decorators/ report-templates/ Source Packages ● ro.uaic.info.app ● .model (entity) ● .dao (repo) ● .service ● .edit ● .view ● .reports ● .converters, .validators, .exception Next steps... ● Create two pages and navigate between them ● Create a backing bean ● Use validators and converters ● Use templates ● Create custom components ● ... .