Changes for page Basic Tiles Integration

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

From version Icon 2.1 Icon
edited by Ken McWilliams
on 2012/03/18 23:27
Change comment: There is no comment for this version
To version Icon 3.1 Icon
edited by Ken McWilliams
on 2012/03/18 23:46
Change comment: There is no comment for this version

Summary

Details

Icon Page properties
Content
... ... @@ -27,5 +27,112 @@
27 27  
28 28  Apache Tiles offers more than JSP includes. The main usage difference is that the tiles.xml file provieds a single source of definition, which defines what resources are brought together to produce a view. JSPs on the other hand mix the resources of the view (the JSP it self) with the content that must be included. What this means, is if a complicated layout has all the commonality factored out both JSPs using includes and JSPs assembled with Tiles, there would as many JSPs using either system however to understand the JSPs using includes you would need to visit each page and then visit each of those pages in turn to see what is being included. It's very much like Rusian Dolls, to know what you are getting you'll need to open each one up. Contrast this with tiles: Each JSP should be terminal and not include any other resources on their own (it is the job of Apache Tiles to insert the resources into the pages as defined in the tiles.xml and marked in the JSPs by Tiles tags). The result is that you should be able to find the resource which needs modification more readily with Tiles. Put another way, Apache Tiles reaches out, gathers all the resources it needs puts them together and spits out the result. It is easy to understand because the definition for everything it needs is defined in one place.
29 29  
30 +The Tiles XML file and Tiles tags do add a little overhead but with any new tool there is a cost of learning. The tags are quite simple and the XML is easy enough to understand that designers can definitly derive benifits from adding the management of the tiles.xml file to their list of responsibilities (it will make their lives easier). A site wide overhaul will be far easier with tiles than it would be without it.
30 30  
31 -The Tiles XML file and Tiles tags do add a little overhead but with any new tool there is a cost of learning. The tags are quite simple and the XML is easy enough to understand that designers can definitly derive benifits from adding the management of the tiles.xml file to their list of responsibilities (it will make their lives easier).
32 +== Adding Tiles to your Struts2 Project ==
33 +
34 +This tutorial only requires we have a working Struts2 project, to set one up see [[here>>doc:Setting up a new Struts2 Project in Netbeans]].
35 +
36 +**First:** Add struts2-tiles-plugin-VERSION.jar to your classpath, where VERSION is the same version as struts2-core
37 +
38 +=== Modify your web.xml to include the new sections (in bold) ===
39 +
40 +<?xml version="1.0" encoding="UTF-8"?>
41 +<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">
42 +**~ <listener>**
43 +**~ <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>**
44 +**~ </listener>**
45 +**~ <context-param>**
46 +**~ <param-name>tilesDefinitions</param-name>**
47 +**~ <param-value>/WEB-INF/tiles.xml</param-value>**
48 +**~ </context-param>**
49 + <filter>
50 + <filter-name>struts2</filter-name>
51 + <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
52 + </filter>
53 + <filter-mapping>
54 + <filter-name>struts2</filter-name>
55 + <url-pattern>/*</url-pattern>
56 + </filter-mapping>
57 +</web-app>
58 +
59 +=== Set up your struts.xml to process the action ===
60 +
61 +**[TODO: provide annotation based version as well]**
62 +
63 +
64 +<?xml version="1.0" encoding="UTF-8"?>
65 +<!DOCTYPE struts PUBLIC
66 + "-~/~/Apache Software Foundation~/~/DTD Struts Configuration 2.0~/~/EN"
67 + "http:~/~/struts.apache.org/dtds/struts-2.0.dtd">
68 +\\<struts>
69 + <constant name="struts.devMode" value="true" />
70 + <constant name="struts.ui.theme" value="simple" />
71 +**~ <package name="basicstruts2" namespace="/" extends="tiles-default">**
72 +**~ <result-types>**
73 +**~ <result-type name="tiles" default="true" class="org.apache.struts2.views.tiles.TilesResult"/>**
74 +**~ </result-types>**
75 +**~ <action name="test" >**
76 +**~ <result>tilesWorks</result>**
77 +**~ </action>**
78 +**~ </package>**
79 +</struts>
80 +
81 +=== Add the following example /WEB-INF/tiles.xml to your project: ===
82 +
83 +<?xml version="1.0" encoding="UTF-8"?>
84 +<!DOCTYPE tiles-definitions PUBLIC
85 + "-~/~/Apache Software Foundation~/~/DTD Tiles Configuration 2.1~/~/EN"
86 + "http:~/~/tiles.apache.org/dtds/tiles-config_2_1.dtd">
87 +<tiles-definitions>
88 + <definition name="baseLayout" template="/WEB-INF/tiles/template.jsp">
89 + <put-attribute name="title" value="My Title"/>
90 + <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
91 + <put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/>
92 + <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp"/>
93 + </definition>
94 + <definition name="tilesWorks" extends="baseLayout">
95 + </definition>
96 +</tiles-definitions>
97 +
98 +=== Create the following JSPs required by tiles.xml ===
99 +
100 +**Create file /WEB-INF/tiles/template.jsp**
101 +
102 +<%@taglib prefix="tiles" uri="http:~/~/tiles.apache.org/tags-tiles" %>
103 +<%@taglib prefix="s" uri="/struts-tags"%>
104 +<%@page contentType="text/html" pageEncoding="UTF-8" %>
105 +<!DOCTYPE html>
106 +<html>
107 + <head>
108 + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
109 + <title><tiles:insertAttribute name="title"/></title>
110 + </head>
111 + <body>
112 +<tiles:insertAttribute name="header"/>
113 +<tiles:insertAttribute name="body"/>
114 +<tiles:insertAttribute name="footer"/>
115 + </body>
116 +</html>
117 +
118 +**Create file /WEB-INF/tiles/body.jsp**
119 +
120 +<div>**
121 +** This is the default body.**
122 +**</div>
123 +
124 +**Create file /WEB-INF/tiles/header.jsp**
125 +
126 +<div>**
127 +** This is the default header.**
128 +**</div>
129 +
130 +**Create file /WEB-INF/tiles/footer.jsp**
131 +
132 +<div>**
133 +** This is the default body.**
134 +**</div>
135 +
136 +=== Test ===
137 +
138 +You should now be able to run the application supplying //test// as the action and you should see tiles in action.