Here is an example that shows how to create tiles in Struts2 using the struts2 tiles plugin. First step will be to change deployment descriptor first setup the tiles definition file:
Now Listener Configuration will look like below:
Now layout of above example should look like below, which have header, Menu, Body and Footer elements in tiles layout.
I hope you get this blog useful for creating application with Strut2 tiles.
<context-param><param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name><param-value>/WEB-INF/tiles.xml</param-value></context-param>
Now Listener Configuration will look like below:
Now web.xml file should look like as below:<listener><listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class></listener>
Here is the Tiles.xml configuration file data which should be the key item with which absolute resource will be called at location of page layout.<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5"><display-name>Struts2 Tiles Example</display-name><context-param><param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name><param-value>/WEB-INF/tiles.xml</param-value></context-param><listener><listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class></listener><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter. StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
<?xml version="1.0" encoding="UTF-8" ?>baseLayout.jsp as defined in tiles.xml, will look like below.
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="baseLayout" template="/baseLayout.jsp">
<put-attribute name="title" value="Template"/>
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="menu" value="/menu.jsp"/>
<put-attribute name="body" value="/body.jsp"/>
<put-attribute name="footer" value="/footer.jsp"/>
</definition>
<definition name="welcome" extends="baseLayout">
<put-attribute name="title" value="Welcome"/>
<put-attribute name="body" value="/welcome.jsp"/>
</definition>
<definition name="friends" extends="baseLayout">
<put-attribute name="title" value="Friends"/>
<put-attribute name="body" value="/friends.jsp"/>
</definition>
<definition name="office" extends="baseLayout">
<put-attribute name="title" value="Office"/>
<put-attribute name="body" value="/office.jsp"/>
</definition>
</tiles-definitions>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title><tiles:insertAttributename="title"ignore="true"/></title></head><body><tableborder="1"cellpadding="2"cellspacing="2"align="center"><tr><tdheight="30"colspan="2"><tiles:insertAttributename="header"/></td></tr><tr><tdheight="250"><tiles:insertAttributename="menu"/></td><tdwidth="350"><tiles:insertAttributename="body"/></td></tr><tr><tdheight="30"colspan="2"><tiles:insertAttributename="footer"/></td></tr></table></body></html>
In the struts.xml file create a new result type for tiles as shown below.<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<struts><packagename="default"extends="struts-default"><result-types><result-typename="tiles"class="org.apache.struts2.views.tiles.TilesResult"/></result-types><actionname="*Link"method="{1}"class="com.vaannila.action.LinkAction"><resultname="welcome"type="tiles">welcome</result><resultname="friends"type="tiles">friends</result><resultname="office"type="tiles">office</result></action></package></struts>
Now layout of above example should look like below, which have header, Menu, Body and Footer elements in tiles layout.
I hope you get this blog useful for creating application with Strut2 tiles.

0 comments:
Post a Comment
Please write to us here...