JSF and Tiles
JSF and Tiles
JSF and Tiles
Customized J2EE Training: http://courses.coreservlets.com/ Courses developed and taught by coreservlets.com experts (edited by Marty)
Java 5, Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF, Ajax, GWT, custom courses.
JSP, Spring, Hibernate, EJB3, Ruby/Rails Servlets, Struts, JSF/MyFaces, Hibernate, Ajax, GWT, Java 5, Java 6, etc. Ruby/Rails coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location. Contact [email protected] for details
Agenda
Tiles motivations Setting up JSF (any implementation) for simple Tiles Tiles basics
Sketch out desired layout Make template file that represents layout Create JSP pages that define layout pieces Create JSP pages that populate layout
Tiles Motivations
Reuse (not rewrite) repeated sections of pages Simplify the creation of similar pages Increase flexibility and ease of maintenance compared to <jsp:include />. Migrate widely used facility from Struts
Tiles Basics
Customized J2EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces, Hibernate, Ajax, GWT, Java 5, Java 6, etc. Ruby/Rails coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Portability
Simple tiles work in all JSF implementations just by including JAR file However, definitions files and special view handlers require implementation-specific setup
See later sub-section for use in MyFaces
8
Tiles Basics
1. Sketch out desired layout
Do this on paper Use tiles:insert wherever layout piece goes Use tiles:getAsString wherever changeable text goes To prevent direct access, put template in WEB-INF JSP pages containing HTML snippets To prevent direct access, put JSP pages in WEB-INF Use tiles:insert to refer to the layout from #1 above Use tiles:put to specify layout pieces Unless forwarded to, JSP not in WEB-INF J2EE training: http://courses.coreservlets.com
3. Create JSP pages that define layout pieces 4. Create JSP pages that populate layout
Often only one or two parts (e.g., the body) changes from page to page Helpful to sketch layout on paper
10
11
Use tiles:insert to stub out sections that will be filled in by template pieces
<tiles:insert attribute="header"/> The eventual JSP page will associate a real file with the header attribute
Use tiles:getAsString to stub out locations that will be replaced by simple text
<tiles:getAsString name="title"/>
13
These pages can themselves be tiles Pages can use JSP and JSF tags
Layout pieces can access beans that are in request scope Only top-level page should have f:view Any tile:insert statements that are within a JSF tag must say flush="false"
<tiles:insert attribute="whatever" flush="false"/>
WEB-INF/tiles/body1.jsp
<H1 ALIGN="CENTER">Page 1 Body</H1> Blah, blah, blah, blah, blah. Blah, blah, blah, blah, blah. <P> Yadda, yadda, yadda, yadda. Yadda, yadda, yadda, yadda.
WEB-INF/tiles/footer.jsp
15
Use tiles:put to specify the layout pieces that apply to this specific page
<tiles:insert > <tiles:put name="title" value="Page 1 Title"/> <tiles:put name="header" value="/WEB-INF/tiles/header.jsp"/> </tiles:insert> J2EE training: http://courses.coreservlets.com
16
Results: page1.jsp
18
Results: page2.jsp
19
Example: e-boats
Customized J2EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces, Hibernate, Ajax, GWT, Java 5, Java 6, etc. Ruby/Rails coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
21
Footer
22
23
24
27
28
29
Result: index.jsp
31
Result: yachts.jsp
33
Result: tankers.jsp
35
Result: carriers.jsp
37
Motivation
Avoiding repetition
In the previous examples, each final JSP page repeated the definition of all elements
Even though only the title and body changed, each final JSP page still had to do tiles:put on the header, searchmenu, and footer
Centralized changes
The locations of the various template pieces are scattered around multiple JSP pages
So, if locations change, multiple files need to be edited
Add to web.xml
<context-param> <param-name>tiles-definitions</param-name> <param-value>/WEB-INF/tiles-defs.xml</param-value> </context-param>
2. Make template file that represents layout 3. Create JSP pages that define layout pieces 4. Define layouts in WEB-INF/tiles-defs.xml 5. Create JSP pages that use layouts
Two line JSP pages
41
Derived layouts
<definition name="" extends="top-level-layout-name">
Advantages
Derived layouts need only specify parts that differ from parent layout Can make changes in a single file Definitions are in config files, not JSP pages J2EE training: http://courses.coreservlets.com
42
43
Footer
44
45
46
47
48
49
yachts.jsp
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <tiles:insert definition="yachts.page"/>
tankers.jsp
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <tiles:insert definition="tankers.page"/>
carriers.jsp
50
Result: index.jsp
51
Result: yachts.jsp
52
Result: tankers.jsp
53
Result: carriers.jsp
54
To:
<navigation-case> <from-outcome>...</from-outcome> <to-view-id>carriers.tiles</to-view-id> </navigation-case>
55
If JSP pages are only used as results pages, J2EE pages training: http://courses.coreservlets.com you can omit the 2-line JSP
Summary
Setup
Basics: put struts.jar in WEB-INF/lib (portable) Definitions: edit faces-config.xml and web.xml (MyFaces only)
Basic Usage
56
Sketch out desired layout Make template file that represents layout Create JSP pages that define layout pieces Create JSP pages that populate layout Sketch out desired layout Make template file that represents layout Create JSP pages that define layout pieces Define layouts in WEB-INF/tiles-defs.xml Create JSP pages that use layouts
Can omit and refer to definition names if named .tiles J2EE training: http://courses.coreservlets.com
Questions?
Customized J2EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces, Hibernate, Ajax, GWT, Java 5, Java 6, etc. Ruby/Rails coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.