Apache Tiles Learning (iii), the basic use of Apache Tiles _apache

Source: Internet
Author: User
Tags abstract definition
Absrtact: This article introduces the basic concepts and related components of Apache tiles, and has a good understanding of tiles. 1. Overview

For a new technology, understanding its basic concepts and principles is the basis for learning the technology. 2, the concept of tiles

Tiles is an implementation of the composite view mode (composite view pattern). Tiles adds the pattern to its own concept is that the pattern is materialized. The realization of tiles is based on the theory of compound mode, which includes the following concepts: Template,attribute and definition. The implementation of view helper mode is the view preparer concept.

Next, we'll explain the concepts: template,attribute,definition and view preparer. 2.1, Template: Template

In tiles, a template (Template) is the layout part of a page. You can consider a page structure to be made up of different needs to fill gaps.

For example, consider this "typical layout" page structure.

You can copy the structure to a new JSP page, as shown below

template.jsp

<%@ taglib uri= "http://tiles.apache.org/tags-tiles" prefix= "tiles"%>
<table>
  <tr>
    <TD colspan= "2" >
      <tiles:insertattribute name= "header"/>
    </td>
  </tr>
  < tr>
    <td>
      <tiles:insertattribute name= "menu"/>
    </td>
    <td>
      < Tiles:insertattribute name= "Body"/>
    </td>
  </tr>
  <tr>
    <td colspan= "2" >
      <tiles:insertattribute name= "Footer"/>
    </td>
  </tr>
</table>

Note: a template (Template) can have no attributes (attributes), in which case the template can be used directly.  If you have attributes, you must first define the attributes to use, otherwise you will be able to parse the JSP page when the error. 2.2, Attributes: Attribute

A property is a blank in a template that is filled into a template in your application. Properties can be of the following three types: string: If the property is string, the string is rendered directly on the page. Template: Property is a template (template), with or without attributes. If you have attributes, you also need to populate them and then render the page.  Definition: It is a reusable page that contains all the attributes to populate to render the page. 2.3, Definitions: definition

A definition is a composition that is presented to the end user; Essentially, a definition consists of a template and a fully or partially populated attribute. To be blunt: a definition is made up of a template and a property. If all of the properties are populated, it can be rendered to the end user. If not all of the attributes are populated, this definition, called an "abstract definition" (abastract definition), can be used as a "parent definition", so that other "definitions" are inherited, and lost "properties" can be populated at run time.

For example, you can create a page by following the typical template you've seen before, and modify the tiles profile as follows:

<definition name= "Myapp.homepage" template= "/layouts/classic.jsp" >
  <put-attribute "header" value = "/tiles/banner.jsp"/>
  <put-attribute name= "menu" value= "/tiles/common_menu.jsp"/>
  < Put-attribute name= "Body" value= "/tiles/home_body.jsp"/> <put-attribute name= "footer" value= "/tiles/"
  credits.jsp "/>
</definition>

2.4, view Assistant: Views preparer

Sometimes a definition requires "preprocessing" before rendering. For example, when a menu is displayed, the structure of the menu must be created and saved in the request scope.

To achieve "preprocessing," the View assistant will be used, and the View assistant will be invoked before rendering the definition, so that what is required to render the definition is properly preprocessed. 3. Create and use tiles pages

After installing tiles and learning some of the concepts of tiles, it's time to create some pages. Below you will learn the steps to create reusable pages and complete pages. 3.1. Create a template

We use the typical layout page structure:

Create a JSP page as the layout and save it in the/layouts/classic.jsp file

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


There are 5 attributes in this template: Title (string-type properties), Header,menu,body and footer.
3.2, create the composition of the page

At this stage, you will need to create 4 JSP pages to replace the Header,menu,body and footer attributes in the template created before.

You can place anything you want to put on this page and they are just a test.
3.3. Create a definition

By default, the definition file is/web-inf/tiles.xml. If you are using the Completeautoloadtileslistener,tiles will use the WebApp directory to press/web-inf/tiles*.xml match or classpath down to press/meta-inf/tiles*. XML matches any file as a "definition" file, and if more than one is found, tiles will merge the files together.

But now, we use the default and create a/wen-inf/tiles.xml file that contains a definition.

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE tiles-definitions public
       '-//apache Software foundation//dtd tiles Configuration 3.0//en '
       "http:// Tiles.apache.org/dtds/tiles-config_3_0.dtd ">
<tiles-definitions>
  <definition name=" Myapp.homepage "template="/layouts/classic.jsp ">
    <put-attribute name=" title "value=" Tiles tutorial Homepage "/>
    <put-attribute name= Header" value= "/tiles/banner.jsp"/> <put-attribute "name="
    Menu "value="/tiles/common_menu.jsp "/>
    <put-attribute name=" Body "value="/tiles/home_body.jsp "/>
    <put-attribute name= "Footer" value= "/tiles/credits.jsp"/>
  </definition>
</ Tiles-definitions>

3.4. Rendering definition

Once you've created the set, you can render it. Insert the definition into a JSP page by using the <tiles:insertdefinition/> tag.

<%@ taglib uri= "http://tiles.apache.org/tags-tiles" prefix= "tiles"%> <tiles:insertdefinition "Name="
Myapp.homepage "/>


In other cases, you can directly use response to render the definition by using the tiles container
Tilescontainer container = Tilesaccess.getcontainer (
        request.getsession (). Getservletcontext ());
Container.render ("Myapp.homepage", request, response);


Rendering Utilities provided through the use of tiles. For example, if you have configured Tilesdispatchservlet, you can render "definition" by requesting: Http://example.com/webapp/myapp.homepage.tiles. Render "Definition" by using a supported framework (struts,spring, etc.).

Reprint: https://my.oschina.net/jast90/blog/284254

Reference:

Official documents

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.