We Recommend These ResourcesGetting Started with FUSE Mediation RouterApache Lucene 3.0: Download the LucidWorks Certified DistributionWhy Load Testing From The Cloud Doesn't WorkNOSQL for the EnterpriseImplementing Enterprise Integration Patterns Welcome to Part-4 of the 7-part series where we will go through different aspects for Struts2 Framework with some useful examples. In previous part we went through Struts2 Validation Framework. We saw how easy it is to integrate validation in your struts2 application. In this part we will discuss about Tiles Framework and its Integration with Struts2. We will add Tiles support to our HelloWorld Struts application that we created in previous parts. I strongly recommend you to go through previous articles and download the source code of our sample application. Struts 2 Tutorial ListRelated: Struts Tiles Plugin Tutorial with Example Introduction to Tiles 2 Nowadays, website are generally divided into pieces of reusable template that are being rendered among different web pages. For example a site containing header, footer, menu etc. This items remains same through out the website and give it a common look and feel. It is very difficult to hard code this in each and every webpage and if later a change is needed than all the pages needs to be modified. Hence we use templatization mechanism. We create a common Header, Footer, Menu page and include this in each page. Tiles Plugin allow both templating and componentization. In fact, both mechanisms are similar: you A common layout of website is defined in a central configuration file and this layout can be extended across all the webpages of the web application. Our Application LayoutOur goal is to add Header, Footer and Menu to our StrutsHelloWorld application. Following will be the layout of the same.
In order to add Tiles support to our Struts2 application, we will need few jar files. Following is the list of JARs in our example. Add these JARs in WEB-INF/lib folder.
To configure Tiles, an entry for listener has to be made in web.xml. Open the web.xml from WEB-INF folder and add following code into it. <listener><listener-class> org.apache.struts2.tiles.StrutsTilesListener </listener-class> </listener> <context-param> <param-name>tilesDefinitions</param-name> <param-value>/WEB-INF/tiles.xml</param-value> </context-param> The above code configure Tiles listener in web.xml. An input configuration file /WEB-INF/tiles.xml is passed as argument. This file contains the Tiles definition for our web application. Create a file tiles.xml in WEB-INF folder and copy following code into it.
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" ""> <tiles-definitions> <definition name="baseLayout" template="/BaseLayout.jsp"> <put-attribute name="title" value="" /> <put-attribute name="header" value="/Header.jsp" /> <put-attribute name="menu" value="/Menu.jsp" /> <put-attribute name="body" value="" /> <put-attribute name="footer" value="/Footer.jsp" /> </definition> <definition name="/welcome.tiles" extends="baseLayout"> <put-attribute name="title" value="Welcome" /> <put-attribute name="body" value="/Welcome.jsp" /> </definition> <definition name="/customer.tiles" extends="baseLayout"> <put-attribute name="title" value="Customer Form" /> <put-attribute name="body" value="/Customer.jsp" /> </definition> <definition name="/customer.success.tiles" extends="baseLayout"> <put-attribute name="title" value="Customer Added" /> <put-attribute name="body" value="/SuccessCustomer.jsp" /> </definition> </tiles-definitions> (责任编辑:JavaVideo) |



