Vous êtes sur la page 1sur 5

Struts Tiles Tutorial

Tiles is used to create reusable presentation components. With Tiles, we first define a base layout with different sections after that we define which jsp page should fill in the corresponding regions in an exteranl configuration file. The same layout can be reused any number of times by specifying different jsp pages.

What is Struts Tiles?


Tiles is a framework for the development user interface. Tiles is enables the developers to develop the web applications by assembling the reusable tiles (jsp, html, etc..). Tiles uses the concept of reuse and enables the developers to define a template for the web site and then use this layout to populate the content of the web site. For example, if you have to develop a web site having more that 500 page of static content and many dynamically generated pages. The layout of the web site often changes according to the business requirement. In this case you can use the Tiles framework to design the template for the web site and use this template to populate the contents. In future if there is any requirement of site layout change then you have to change the layout in one page. This will change the layout of you whole web site. Introduction We use Struts Tiles to design reusable user-interface components (headers, footers, logos, left navigation and right navigation) and assemble then into Web pages Consider a example library application whose web page layout has a header, body and footer. On this example i will explain what is tiles and when you will be use it.

The developer have two alternative ways to create the layout. He add the header and footer to each page of the site or he use the command <jsp:include>, to include the header and footer into the pages. In the first way all pages contains the header and footer source code. When the header or footer will changed, the developer have to change all pages. In the second way the header and footer are placed in seperated files, so the files can be included into the pages where they will be needed. When he change the layout and add a menu to the library application, shown by the picture below, the developer have to change all pages, because he have to add the inlude command of the menu in each page. On this situation tiles is the best way to develope the page layout, thus the developer don`t have to change each page.

Tiles use a seperate layout file, that contains the container of the layout. When the layout will be changed only the layout file and the tiles configuration files have to change by the developer. That will save many time on large applications.

1st Way : How to use

jsp:include action:

The <jsp:include> element allows you to include either static and dynamic files in a JSP file. The results of including static and dynamic files are quite different. If the file is static, its content is included in the calling JSP file. If the file is dynamic, it acts on a request and sends back a result that is included in the JSP page. When the include action is finished, the JSP container continues processing the remainder of the JSP file. <jsp:include> element handles both types of files, so it is convenient to use when you dont know whether the file is static or dynamic.

Example:

<html> <head> <title>demo of jsp include</title> </head> <body bgcolor="#551a8b"> <center>What is new Jsp. </center> <p> These are include pages<ol> <li><jsp:include page="step1.html" <li><jsp:include page="step2.html" <li><jsp:include page="step3.html" <li><jsp:include page="step4.html" </ol> </body> </html>

flush="true"> flush="true"> flush="true"> flush="true">

Tiles vs. JSP Include


Advantage of Tiles over JSP include 1. Code Repetition is reduced Use of Tiles reduce the code repetition to a great extent. Code repetition is bad but repetition of layout logic could be worst. Tiles also handle this issue. As you have layout templates based on which all the pages are combined, you don't need to repeat the code for layout. Other view components are also reusable and can be used in the same application at other places reducing the code repetition. 2. Low coupling between pages Coupling is the degree of interactivity between two entities. It is always suggested to minimize coupling between unrelated classes, packages, and so on. Same principle is applied to view components. Tiles reduce the coupling between unrelated view components. 3. High layout control Tiles provide great layout control by providing layout templates. 4. I18N support for locale-specific loading 5. Dynamic Page building Pages are built dynamically in tiles. You can control the page view by configuring it through

xml. 6. Elimination of duplicate and redundant information Tiles eliminate the duplicate and redundant information in the configuration file by providing Tiles inheritance. 7. Central location for view components Tiles save definition of all the components at one place (in tilesDef.xml) and hence can be modified easily when required.

Disadvantages of Tiles 1. Increases the number of pages 2. Increases complexity Tiles increase complexity by introducing another layout page. Understanding and implementing templating can also be difficult at initial stages. 3. Big API Tiles have a bigger API set which may be difficult to understand for getting full benefit of tiles. 4. You have to specify a name to any component you create.

Steps To Create Tiles Application


Tiles is very useful framework for the development of web applications. Here are the steps necessary for adding Tiles to your Struts application: Add the Tiles Tag Library Descriptor (TLD) file to the web.xml. Create layout JSPs. Develop the web pages using layouts. Repackage, run and test application.

Steps involved in using Tiles concept:


1. Configure TilesPlugin plug-in in struts-config.xml.

Add following entry of plug-in tag to your struts-config.xml in order to configure Tiles. <plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="moduleAware" value="true" /> <set-property property="definitions-parser-validate" value="true" /> </plug-in>

Vous aimerez peut-être aussi