advanced web technologies 8) facelets in jsf · advanced web technologies 8) facelets in jsf first...

27
Berner Fachhochschule, Technik und Informatik Advanced Web Technologies 8) Facelets in JSF Dr. E. Benoist Fall Semester 2010/2011 Advanced Web Technologies 8) Facelets in JSF 1

Upload: others

Post on 11-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Berner Fachhochschule, Technik und Informatik

Advanced Web Technologies8) Facelets in JSF

Dr. E. Benoist

Fall Semester 2010/2011

Advanced Web Technologies 8) Facelets in JSF

1

Page 2: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Using Facelets

� MotivationThe gap between JSP and JSF

� First Example : The Birds Directory

� JSP Standard Template Library: JSTLThe “if” Tag

� Reusable Composite Components

Advanced Web Technologies 8) Facelets in JSF

2

Page 3: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The gap between JSP and JSF

I They both write output to the browser

• JSP container writes out output as soon as it finds JSPcontent

• JSF components dictate their own rendering.

I Examples• JSP file:

<h:outputText value=”I am first” />I am second

• Output:

I am firstI am second

Advanced Web Technologies 8) Facelets in JSF

Motivation: The gap between JSP and JSF 3

Page 4: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The gap between JSP and JSF(cont.)

I Another Example• JSP file:

<h:panelGroup><h:outputText value=”I am first” />I am second

</h:panelGroup>

• Output:

I am secondI am first

I PanelGroup is a component that renders his own children• I am first is only produced when the closing tag is seen.

Advanced Web Technologies 8) Facelets in JSF

Motivation: The gap between JSP and JSF 4

Page 5: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Advantages of Facelets over JSP

I Intertwining JSP and JSF can cause difficulties

• JSTL (Jsp Standard Template Library) can not be used withJSF

I Facelets provide a Template solution for JSF

I Allows creation of lightweight components

• Quite trivial to produce• You don’t need to create tags for your new components

I Facelets use Unified Expression Language EL

Advanced Web Technologies 8) Facelets in JSF

Motivation: The gap between JSP and JSF 5

Page 6: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Creating a project with FaceletsI Download Facelets library

• https://facelets.dev.java.net• it includes some demonstration applications

I Adding Dependencies

I Configure the Web Descriptor (web.xml)

<web−app><!−− Use Documents Saved as ∗.xhtml −−><context−param>

<param−name>javax.faces.DEFAULT SUFFIX

</param−name><param−value>.xhtml</param−value>

</context−param></web−app>

Advanced Web Technologies 8) Facelets in JSF

Motivation: The gap between JSP and JSF 6

Page 7: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Configure JSF

I Facelets replaces the default JSF ViewHandler with itsown implementation

faces-config.xml

<faces−config><application>

<view−handler>com.sun.facelets.FaceletViewHandler

</view−handler></application>

</faces−config>

Advanced Web Technologies 8) Facelets in JSF

Motivation: The gap between JSP and JSF 7

Page 8: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

First Example : The ModulesDirectory

I We need to create a simple directory of Modules

I We create various xhtml files

• template.xhtml for storing the main design for the site• index.xhtml the welcome file• awt.xhtml file presenting the Advanced Web Technology

Module• webProg.xhtml file presenting the webProgramming• menu.xhtml contains just the menu. It is included in the other

files

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 8

Page 9: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The template

I We include the name spaces corresponding to jsf andfacelets tags• JSF tags are prefixed with h or f• Facelets specific tags are prefixed with ui

<!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://java.sun.com/jsf/html”xmlns:f=”http://java.sun.com/jsf/core”xmlns:ui=”http://java.sun.com/jsf/facelets”>

<head>

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 9

Page 10: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The template (Cont.)I We define the basic layout.

• We insert CSS and Javascript in such a file

<title>Facelets−Test</title><style type=”text/css”><!−−

.box {float: right;width: 50%;border: black dotted 1px;padding: 5px}−−>

</style></head><body>

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 10

Page 11: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The template (Cont.)I Template contains placeholders that can be overwritten

• We use ui:insert to define areas that can be overwritten• each insert receives a name and a default value (the content

of the tag).

<h:form><h1>Facelets−Template</h1><div class=”box”>

<ui:insert name=”navigation” /></div><ui:insert name=”content”>

Default Text for content (only if no content has been defined)</ui:insert></h:form></body></html>

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 11

Page 12: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The home pageI The index.xhtml file greets a user

• Must be a valid xhtml document, so includes all the xhtml“stuff”

• Everything that is outside the ui:composition tag is ignored.

<!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://java.sun.com/jsf/html”xmlns:f=”http://java.sun.com/jsf/core”xmlns:ui=”http://java.sun.com/jsf/facelets”>

<body>This text will never be displayed(text before composition is ignored)<ui:composition template=”template.xhtml”>

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 12

Page 13: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The home page (Cont.)I We are using new facelets tags:

• ui:composition used to reference the template for this page• ui:define (its name must match the one of the ui:insert

in the template).• ui:include to include the content from another document

<ui:composition template=”template.xhtml”><ui:define name=”navigation”>

<ui:include src=”menu.xhtml” /></ui:define>

</ui:composition>This text will neither be used(text after composition is ignored too)</body></html>

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 13

Page 14: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

A menu pageI This page in included in all the other pagesI It must contain the same “xhtml” stuff (like definitions

of name spaces).

<!DOCTYPE html PUBLIC ”−//W3C//DTD XHTML 1.0 Transitional//↘→EN”...<ui:composition><h3>Content table</h3><hr /><h:panelGrid column=”1”><h:commandLink value=”Home” action=”home” /><h:commandLink value=”Web Programming” action =”webProg” /><h:commandLink value=”Advanced Web Technology” action =”awt” /></h:panelGrid></ui:composition>This text will neither be used(text after composition is ignored too)</body></html> Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 14

Page 15: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

We need the correspondingnavigation

I For navigating from any page to the modules or home pages

<navigation−rule><navigation−case>

<from−outcome>home</from−outcome><to−view−id>/index.xhtml</to−view−id>

</navigation−case></navigation−rule><navigation−rule>

<navigation−case><from−outcome>webProg</from−outcome><to−view−id>/webProg.xhtml</to−view−id>

</navigation−case></navigation−rule><navigation−rule>...

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 15

Page 16: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Facelets TagLibs

Facelets support the following tag libraries

I Templating library The one we have already seen (with theui:... tags.

I JSF libraries core and html jsf libraries are supported byFacelets.

I JSTL facelets provides partial support for JSTL tags.Function library is fully supported, Core is only partially:

• c:if for conditional branching• c:forEach for looping in a collection• c:catch For emulating a try catch• c:set For defining manualy some attribute variables

Advanced Web Technologies 8) Facelets in JSF

First Example : The Birds Directory 16

Page 17: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

JSP Standard Template Library:JSTL

I Introduces some programming in the view part• Not used as a template language• JSTL is not fully supported

Advanced Web Technologies 8) Facelets in JSF

JSP Standard Template Library: JSTL 17

Page 18: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

JSTL (Cont.)

I Looping with explicit numeric values

<c:forEach var=”name” begin=”x” end=”y” step=”z”>Blah, blah <h:outputText value=”#{name}”/>

</c:forEach>

I Looping over data structures• Can loop down arrays, strings, collections, maps

<c:forEach var=”name”items=”array−or−collection”>

Blah, blah <h:outputText value=”#{name}”/></c:forEach>

Advanced Web Technologies 8) Facelets in JSF

JSP Standard Template Library: JSTL 18

Page 19: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

The ”if” Tag

<ul><c:forEach var=”i” begin=”1” end=”10”>

<li><h:outputText value=”#{i}”/><c:if test=”#{i > 7}”>(greater than 7)</c:if></li>

</c:forEach></ul>

Advanced Web Technologies 8) Facelets in JSF

JSP Standard Template Library: JSTL: The “if” Tag 19

Page 20: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Reusable Composite Components

I Facelets allows to create lightweight compositioncomponentsTwo basic steps

• Create a tag source file, which will contain the XHTML codethat defines your component

• Register the tag in your custom tag library, so it can be used inyour Facelets applications.

I You can reuse this component as much as possible

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 20

Page 21: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Example: Custom“inputTextLabeled” Component

I We need a component that can be used like this:

<custom:inputTextLabeledlabel=’’Name’’value=’’#{module.name} />

I What it does:

• Is a combination of a label and an input text

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 21

Page 22: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Creating a tag source fileIn the directory: WEB-INF/facelets/components/ we write thefollowing file:

I InputTextLabeled.xhtml

<!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://java.sun.com/jsf/html”xmlns:f=”http://java.sun.com/jsf/core”xmlns:ui=”http://java.sun.com/jsf/facelets”>

<ui:component><h:outputLabel value=”#{label}: ”>

<h:inputText value=”#{value}” /></h:outputLabel>

</ui:component></html>

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 22

Page 23: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Registering the tag in the tag libraryI We need to create a tag lib file.

WEB-INF/facelets/mycustom.taglib.xml

Defines a new name space that needs to be included in any file using thiscomponent(s).

<!DOCTYPE facelet−taglib PUBLIC”−//Sun Microsystems. Inc//DTD Facelet Taglib 1.0//EN””http://java.sun.com/dtd/facelet−taglib 1 0.dtd”>

<facelet−taglib><namespace>http://myfaces.benoist.ch/custom</namespace>

<tag><tag−name>inputTextLabelled</tag−name><source>components/inputTextLabeled.xhtml</source>

</tag></facelet−taglib>

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 23

Page 24: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Declare the taglib to Facelets

I We need to edit web.xml

• declare our new taglib in the facelets.LIBRARIES contextparameter

<context−param><param−name>facelets.LIBRARIES</param−name><param−value>

/WEB−INF/facelets/mycustom.taglib.xml</param−value></context−param>

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 24

Page 25: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Let’s use the new component / tag<!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://java.sun.com/jsf/html”xmlns:f=”http://java.sun.com/jsf/core”xmlns:ui=”http://java.sun.com/jsf/facelets”xmlns:custom=”http://myfaces.benoist.ch/custom”>

<head><title>head</title></head><body><f:view>

<h:form><h:panelGrid columns=”1”>

<custom:inputTextLabeled label=”Name” value=”#{module.name}↘→” /><custom:inputTextLabeled label=”Number” value=”#{module.↘→number}” /><custom:inputTextLabeled label=”ECTS” value=”#{module.ects}” ↘→/><h:commandButton value=”Add module”

actionListener=”#{moduleDictionary.addModule}” /></h:panelGrid>

</h:form> </f:view> </body> </html>

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 25

Page 26: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

Conclusion

I Facelets will be the standard• JSF 2.0 will only support facelets for its view part

I Facelets allow easy creation of components• Custom composition components are much easier to write

than with JSP

I Facelets allow to enrich view part of JSF• You can use a part of JSTL (if / foreach / try catch)• You can use the Unified Expression Language

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 26

Page 27: Advanced Web Technologies 8) Facelets in JSF · Advanced Web Technologies 8) Facelets in JSF First Example : The Birds Directory 11. The home page IThe index.xhtml le greets a user

References

I The definitive Guide to Apache MyFaces and Facelets,Z. Wadia, M. Marinschek, H. Saleh and D. Byrne,Apress, 2008

I http://www.ibm.com/developerworks/java/library/j-facelets/

Advanced Web Technologies 8) Facelets in JSF

Reusable Composite Components 27