Skip to Sample Code
- Download the struts complete bundle from the struts 2 website
- Download tiles 2 from tiles 2 website
- Download the tiles dependencies from the jakarta commons site
- Commons BeanUtils 1.7.0 or above
- Commons Digester 1.8 or above
- Commons Logging 1.1 or above
- Create the layout page, and related files (except the layout, all the other files are basic jsps )
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> <html> <head> <title>Insert title here</title> </head> <body> <table width="100%" height="100%"> <tr height="20%"> <td colspan="2" align="center" bgcolor="skyblue"> <tiles:insertAttribute name="header" /></td> </tr> <tr> <td bgcolor="cyan" width="75%"><tiles:insertAttribute name="body" /></td> </tr> <tr height="20%"> <td colspan="2" align="center" bgcolor="skyblue"><tiles:insertAttribute name="footer" /></td> </tr> </table> </body> </html>
- Create the HelloWorld Action class
package example; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport { public String execute() throws Exception { System.out.println("Hello World"); return SUCCESS; } }
- Configure the Web Deployment descriptor by adding a tiles listener to the web.xml file of your web application.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns: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>tilesTest</display-name> <listener> <listener-class> org.apache.struts2.tiles.StrutsTilesListener </listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
- Configure struts to work with tiles, this can be done by either
- Extending the sturts package from "tiles-default"
<package name="tilesTest" extends="tiles-default">
OR
- Declaring a new "result-type", tiles, that will map to "org.apache.struts2.views.tiles.TilesResult"
<result-types> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /> </result-types>
- Extending the sturts package from "tiles-default"
- Set the type of the results in the package to "tiles"
<result name="success" type="tiles">helloworld.home</result>
struts.xml<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="tilesTest" extends="struts-default"> <result-types> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /> </result-types> <action name="helloWorld" class="example.HelloWorld"> <result name="success" type="tiles">helloworld.home</result> </action> </package> </struts>
Note that the result "helloword.home" must match the definition name in tiles.xml file. - Create definitions for tiles in WEB-INF/tiles.xml file.
<!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="helloworld.home" template="/layouts/layout.jsp"> <put-attribute name="header" value="/layouts/header.jsp" /> <put-attribute name="body" value="/index.html" /> <put-attribute name="footer" value="/layouts/footer.jsp" /> </definition> </tiles-definitions>
- The following is a list of jar files used for this example (copied to the WEB-INF/lib directory)
- commons-beanutils.jar
- commons-digester-1.8.jar
- commons-logging-1.1.1.jar
- freemarker-2.3.8.jar
- ognl-2.6.11.jar
- struts2-core-2.0.11.1.jar
- struts2-tiles-plugin-2.0.11.1.jar
- tiles-api-2.0.5.jar
- tiles-core-2.0.5.jar
- tiles-jsp-2.0.5.jar
- xwork-2.0.4.jar
- This example was implemented on tomcat 6.0.16, with Java 5 update 11
{ 0 comments... Views All / Send Comment! }
Post a Comment