Wiki source code of Tiles 2.2.2 Integration

Last modified by Ken McWilliams on 2012/03/20 22:59

Show last authors
1 There are some great new features in Apache Tiles 2.2.2 one of these must-have features is wild card support. For a list of features visit the [[Apache Tiles Features List>>url:http://tiles.apache.org/2.2/framework/whats-new.html]].
2
3 This tutorial requires we have setup a [[Basic Struts2 Project>>doc:Setting up a new Struts2 Project in Netbeans]], and if you have never used tiles before it might be best to start with the [[Basic Tiles Tutorial>>doc:Basic Tiles Integration]].
4
5 == Gather the Required Dependencies ==
6
7 1) Add struts2-tiles-plugin-2.3.1.2.jar or greater added to your Struts2 2.3.1.2 project.
8
9 2a) [**optional**] Some of the features of tiles are only available by adding spring dependencies (you will be able complete this tutorial and get wild card support without these jars). If you wish to have these required dependancies it is recommended that you follow the [[spring tutorial>>doc:Adding Spring Dependancy Injection to Struts2 Project]].
10
11 2b) If 2a was not done then we must add slf4j-api-1.6.1.jar (it is a spring dependency so would not need to be explicitly added if the spring tutorial was followed)
12
13 3) Add the following tiles jars (For maven they all have a group of //org.apache.tiles//, and the artifact id follows the jar):
14
15 * tiles-api-2.2.2.jar **Artifiact id:** tiles-api
16 * tiles-core-2.2.2.jar **Artifiact id:** tiles-core
17 * tiles-jsp.2.2.2.jar **Artifiact id:** tiles-jsp
18 * tiles-servlet-2.2.2.jar **Artifiact id:** tiles-servlet
19
20 == Configure web.xml ==
21
22 <?xml version="1.0" encoding="UTF-8"?>
23 <web-app version="2.5" xmlns="http:~/~/java.sun.com/xml/ns/javaee" xmlns:xsi="http:~/~/www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:~/~/java.sun.com/xml/ns/javaee http:~/~/java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
24 <listener>
25 <listener-class>**org.apache.tiles.web.startup.TilesListener**</listener-class>
26 </listener>
27 <context-param>
28 <param-name>tilesDefinitions</param-name>
29 <param-value>/WEB-INF/tiles.xml</param-value>
30 </context-param>
31 <filter>
32 <filter-name>struts2</filter-name>
33 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
34 </filter>
35 <filter-mapping>
36 <filter-name>struts2</filter-name>
37 <url-pattern>/*</url-pattern>
38 </filter-mapping>
39 </web-app>
40
41 **NOTE**: Either //org.apache.struts2.tiles.StrutsTilesListener// or //org.apache.tiles.web.startup.TilesListener// may be used in this example. I am not certain which is better. Struts2 states that the StrutsTilesListener offers better integration of Tiles with Struts2 however since we are upgrading to a Tiles version greater than that provided by Struts2 I thought is best to use the one recommended by Tiles.
42
43 == Configure struts.xml ==
44
45 <?xml version="1.0" encoding="UTF-8"?>
46 <!DOCTYPE struts PUBLIC
47 "-~/~/Apache Software Foundation~/~/DTD Struts Configuration 2.0~/~/EN"
48 "http:~/~/struts.apache.org/dtds/struts-2.0.dtd">
49 \\<struts>
50 <constant name="struts.devMode" value="true" />
51 <constant name="struts.ui.theme" value="simple" />
52 **~ <constant name="struts.enable.SlashesInActionNames" value="true" />**
53 **~ <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>**
54 <package name="basicstruts2" namespace="" extends="tiles-default">
55 <result-types>
56 <result-type name="tiles" default="true" class="org.apache.struts2.views.tiles.TilesResult"/>
57 </result-types>
58
59 <action name="/~*~*" >
60 <result>{1}</result>
61 </action>
62 </package>
63 </struts>
64
65
66 **NOTE**: The double asterix in the action name. This selects slashes in the name while a single asterix does not. For more information on this and other struts2 pattern matching see [[here>>url:http://struts.apache.org/2.3.1.2/docs/wildcard-mappings.html]].
67
68 == Configure tiles.xml ==
69
70 Add the following to** /WEB-INF/tiles.xml**:
71
72 <?xml version="1.0" encoding="UTF-8"?>
73 <!DOCTYPE tiles-definitions PUBLIC
74 "-~/~/Apache Software Foundation~/~/DTD Tiles Configuration 2.1~/~/EN"
75 "http:~/~/tiles.apache.org/dtds/tiles-config_2_1.dtd">
76 <tiles-definitions>
77 <definition name="baseLayout" template="/WEB-INF/tiles/template.jsp">
78 <put-attribute name="title" value="Default Title"/>
79 <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
80 <put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/>
81 <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp"/>
82 </definition>
83 <definition name="test" extends="baseLayout">
84 <put-attribute name="Test Title" value="Default Title"/>
85 </definition>
86 <definition name="package/*" extends="baseLayout">
87 <put-attribute name="title" value="{1}" type="string"/>
88 </definition>
89 <definition name="" extends="baseLayout">
90 </definition>
91 </tiles-definitions>
92
93 **Note**: With no welcome file specified in the web.xml the empty "" action will be invoked which due to the Struts2 wild cards will in turn call the empty tiles definition.
94
95 == Add the required JSPs to satisfy the requirements of tiles.xml ==
96
97 **/WEB-INF/tiles/template.jsp**
98
99 <%@taglib prefix="tiles" uri="http:~/~/tiles.apache.org/tags-tiles" %>
100 <%@taglib prefix="s" uri="/struts-tags"%>
101 <%@page contentType="text/html" pageEncoding="UTF-8" %>
102 <!DOCTYPE html>
103 <html>
104 <head>
105 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
106 <title><tiles:insertAttribute name="title"/></title>
107 </head>
108 <body>
109 <tiles:insertAttribute name="header"/>
110 <tiles:insertAttribute name="body"/>
111 <tiles:insertAttribute name="footer"/>
112 </body>
113 </html>
114
115
116 **/WEB-INF/tiles/body.jsp**
117
118 <div>
119 This is the default body.
120 </div>
121
122
123 **/WEB-INF/tiles/footer.jsp**
124
125 <div>
126 This is the default footer.
127 </div>
128
129
130 **/WEB-INF/tiles/header.jsp**
131
132 <div>
133 This is the default header.
134 </div>
135
136
137 == Test ==
138
139 We can see the only thing that will change between invocations in this test is the page title. The body of the document will remain constant.
140
141 Thanks to the initial empty action, which calls the empty tiles definition we have an initial page rendered by tiles.
142
143 If we enter an action of test, we will see the page tile becomes "Test Title".
144
145 If we enter a value of "/package/xxx" where xxx can be anything we find the page tile becomes xxx, showing our wild cards in action.