Struts1.x Series Tutorials: Introduction to the tiles framework

Source: Internet
Author: User
Tags definition config

The tiles framework is a plug-in for struts. We can use the Tiles framework for page layout design. The tiles framework provides a template mechanism that separates Web page content from layout and allows you to create templates before inserting specific content into the page.

I. Installation of tiles framework

In order to use tiles, we must first declare the tiles in the Struts-config.xml file with the following configuration code:

<plug-in className="org.apache.struts.tiles.TilesPlugin">
    <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
</plug-in>

Where the Org.apache.struts.tiles.TilesPlugin class only needs to set a Definitions-config property. This property value points to a definition file, Tiles-defs.xml. The basic format for this definition file is as follows:

<?xml version="1.0" encoding="GBK" ?>
   <!DOCTYPE tiles-definitions PUBLIC
          "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
          "http://struts.apache.org/dtds/tiles-config_1_1.dtd">
   <tiles-definitions>
       <definition name="def1" path="/tiles/layout1.jsp">
           <put name="title" value="我的标题" />
           <put name="header" value="header.jsp" />
           ……
       </definition>
       <definition name="def2" path="/tiles/layout1.jsp" >
           ……
       </definition>
   </tiles-definitions>

Where the <tiles-definitions> element can contain multiple <definition> child elements. We can also split a definition file into multiple definition files, such as A-defs.xml, B-defs.xml, and C-defs.xml. When you assign a value to the Definitions-config property, if you have more than one definition file, the middle is separated by a comma (,). As shown in the following code:

<set-property property= "Definitions-config" value= "/web-inf/a-defs.xml,/web-inf/b-defs.xml,/WEB-INF/ C-defs.xml "/>

The tiles framework also has a custom tag library. We can use the following TAGLIB directive to declare this tag library:

<%@ taglib uri= "http://struts.apache.org/tags-tiles" prefix= "tiles"%>

Second, <jsp:include> and <tiles:insert> labels

Because in the process of writing a Web program, many pages will appear the same content, such as all pages use the same page header and footer. An include tag is provided in the JSP tag library to include other pages in the current page, such as the following code that includes the header and footer.

……
   <jsp:include page = "header.jsp" />
   ……
   <jsp:include page = "footer.jsp" />
   ……

An insert label is also available in the tag library of the tiles framework. This tag can also be completed and include the same work. The above code can also be written in the following form:

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
   ……
   <tiles:insert page = "header.jsp" />
   ……
   <tiles:insert page = "footer.jsp" />
   ……

Although include and insert labels can solve code reuse problems Well, it does not completely avoid the problem of code duplication. Many pages that use include or insert labels also have many similar or identical code. such as the use of CSS layout, table, div and other HTML elements for position control. If you want to avoid duplication of these codes. It is not possible to use the Include or insert label for light. To do this, you will need to use the tiles template that is described in the next article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.