The following example shows how to integrate Struts 2 and Tiles using the struts2 tiles plugin. In the deployment descriptor first setup the tiles definition file.
1.
<
context-param
>
2.
<
param-name
> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </
param-name
>
3.
<
param-value
>/WEB-INF/tiles.xml</
param-value
>
4.
</
context-param
>
Then setup the tiles listener.
1.
<
listener
>
2.
<
listener-class
>org.apache.struts2.tiles.StrutsTilesListener</
listener-class
>
3.
</
listener
>
The complete web.xml file.
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
03.
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
04.
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
05.
id
=
"WebApp_ID"
version
=
"2.5"
>
06.
<
display-name
>Struts2Example15</
display-name
>
07.
08.
<
context-param
>
09.
<
param-name
> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </
param-name
>
10.
<
param-value
>/WEB-INF/tiles.xml</
param-value
>
11.
</
context-param
>
12.
13.
<
listener
>
14.
<
listener-class
>org.apache.struts2.tiles.StrutsTilesListener </
listener-class
>
15.
</
listener
>
16.
17.
<
filter
>
18.
<
filter-name
>struts2</
filter-name
>
19.
<
filter-class
>
20.
org.apache.struts2.dispatcher.ng.filter. StrutsPrepareAndExecuteFilter
21.
</
filter-class
>
22.
</
filter
>
23.
24.
<
filter-mapping
>
25.
<
filter-name
>struts2</
filter-name
>
26.
<
url-pattern
>/*</
url-pattern
>
27.
</
filter-mapping
>
28.
29.
<
welcome-file-list
>
30.
<
welcome-file
>index.jsp</
welcome-file
>
31.
</
welcome-file-list
>
32.
</
web-app
>
The tiles.xml file contains the following tile definitions.
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
03.
<!DOCTYPE tiles-definitions PUBLIC
04.
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
06.
07.
<
tiles-definitions
>
08.
09.
<
definition
name
=
"baseLayout"
template
=
"/baseLayout.jsp"
>
10.
<
put-attribute
name
=
"title"
value
=
"Template"
/>
11.
<
put-attribute
name
=
"header"
value
=
"/header.jsp"
/>
12.
<
put-attribute
name
=
"menu"
value
=
"/menu.jsp"
/>
13.
<
put-attribute
name
=
"body"
value
=
"/body.jsp"
/>
14.
<
put-attribute
name
=
"footer"
value
=
"/footer.jsp"
/>
15.
</
definition
>
16.
17.
<
definition
name
=
"welcome"
extends
=
"baseLayout"
>
18.
<
put-attribute
name
=
"title"
value
=
"Welcome"
/>
19.
<
put-attribute
name
=
"body"
value
=
"/welcome.jsp"
/>
20.
</
definition
>
21.
22.
<
definition
name
=
"friends"
extends
=
"baseLayout"
>
23.
<
put-attribute
name
=
"title"
value
=
"Friends"
/>
24.
<
put-attribute
name
=
"body"
value
=
"/friends.jsp"
/>
25.
</
definition
>
26.
27.
<
definition
name
=
"office"
extends
=
"baseLayout"
>
28.
<
put-attribute
name
=
"title"
value
=
"Office"
/>
29.
<
put-attribute
name
=
"body"
value
=
"/office.jsp"
/>
30.
</
definition
>
31.
32.
</
tiles-definitions
>
Here we define a "baseLayout" that contains a title, header, menu, body and footer regions. The header, menu and footer region remains the same for all the layouts only the title and body content changes.
In the baseLayout.jsp page we create a classic tiles layout as shown below.
01.
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
02.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
04.
05.
<
html
>
06.
<
head
>
07.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
08.
<
title
>
09.
<
tiles:insertAttribute
name
=
"title"
ignore
=
"true"
/>
10.
</
title
>
11.
</
head
>
12.
<
body
>
13.
<
table
border
=
"1"
cellpadding
=
"2"
cellspacing
=
"2"
align
=
"center"
>
14.
<
tr
>
15.
<
td
height
=
"30"
colspan
=
"2"
>
16.
<
tiles:insertAttribute
name
=
"header"
/>
17.
</
td
>
18.
</
tr
>
19.
<
tr
>
20.
<
td
height
=
"250"
>
21.
<
tiles:insertAttribute
name
=
"menu"
/>
22.
</
td
>
23.
<
td
width
=
"350"
>
24.
<
tiles:insertAttribute
name
=
"body"
/>
25.
</
td
>
26.
</
tr
>
27.
<
tr
>
28.
<
td
height
=
"30"
colspan
=
"2"
>
29.
<
tiles:insertAttribute
name
=
"footer"
/>
30.
</
td
>
31.
</
tr
>
32.
</
table
>
33.
</
body
>
34.
</
html
>
In the struts.xml file create a new result type for tiles as shown below.
01.
<!DOCTYPE struts PUBLIC
02.
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04.
05.
<
struts
>
06.
<
package
name
=
"default"
extends
=
"struts-default"
>
07.
<
result-types
>
08.
<
result-type
name
=
"tiles"
class
=
"org.apache.struts2.views.tiles.TilesResult"
/>
09.
</
result-types
>
10.
<
action
name
=
"*Link"
method
=
"{1}"
class
=
"com.vaannila.action.LinkAction"
>
11.
<
result
name
=
"welcome"
type
=
"tiles"
>welcome</
result
>
12.
<
result
name
=
"friends"
type
=
"tiles"
>friends</
result
>
13.
<
result
name
=
"office"
type
=
"tiles"
>office</
result
>
14.
</
action
>
15.
</
package
>
16.
</
struts
>
For each result instead of forwarding to the jsp page forward it to the tiles definition.
When you execute the example the following page gets displayed.
The menu.jsp page has the menu items, on clicking each menu item the title and body content alone changes.
1.
<%@taglib uri="/struts-tags" prefix="s"%>
2.
3.
<
a
href
=
"<s:url action="
friendsLink"/>" >Friends</
a
>
4.
5.
<
a
href
=
"<s:url action="
officeLink"/>" >The Office</
a
>
When each menu item is clicked a different method in the LinkAction class is invoked.
01.
package
com.vaannila.action;
02.
03.
import
com.opensymphony.xwork2.ActionSupport;
04.
05.
public
class
LinkAction
extends
ActionSupport {
06.
07.
private
static
final
long
serialVersionUID = -2613425890762568273L;
08.
09.
public
String welcome()
10.
{
11.
return
"welcome"
;
12.
}
13.
14.
public
String friends()
15.
{
16.
return
"friends"
;
17.
}
18.
19.
public
String office()
20.
{
21.
return
"office"
;
22.
}
23.
}
The directory structure of the example is shown below.
No comments:
Post a Comment