Master Tiles Framework (i)---tiles introduction and tiles framework and Architecture __ Framework

Source: Internet
Author: User
Tags extend html tags tld apache tomcat

Getting Started

What this tutorial tells you

This tutorial describes how to use the Tiles framework to create reusable presentation components. (when it was first created, the Tiles framework was named components.) The name was later changed because the "components" represented too many different things, but the original name was still preserved. However, in addition to the site layout, there are many other things you can do with tile. For example, you can divide the presentation layer to better reuse layout, HTML, and other visual components.

This tutorial tries to uncover the basics of using the Tiles framework and then let your knowledge go to the next level. As you complete this tutorial, you will be able to use more advanced Tiles features to create reusable components.

Note: Throughout this tutorial, we use the term tile and pages interchangeably, because any WEB resource can be tile. A tile layout represents a special type of tile that can be used to place other tile inside it. A tile layout can be used as a tile within another tile layout.

Specifically, this tutorial: Define the Tiles framework and architecture. Describes the Tiles architecture and how it is integrated with Struts. Clarify some key Tiles concepts. Shows how to generate tile and use tile as a site template. Shows how to use the tile definition in XML and JavaServer Pages (JSP). Defines tile scopes (scope) and how to move objects into and out of the tile range. Use the attribute list. Demonstrates how to nest tile. Shows how to generate tile and use tile layout as a small visual component. Shows how to subdivide the definition. Create a controller for tile. Shows how to use tile as a actionforward. who should study this tutorial.

If you find yourself writing three lines of the same JSP code on each page, or you want to easily define a complex template layout, you'll benefit from this tutorial.

This tutorial assumes that you fully understand Java programming, MVC (Model-view-controller, model-View-Controller), models 2, and JSP technologies. While a good Struts background will give you the most benefit from this tutorial, you should be able to understand most of the content of this tutorial as long as you are proficient in JSP programming.

Software requirements and code installation

To complete this tutorial, you will need: servlet/jsp containers that conform to the JSP 1.1, 1.2, or 2.0 version. Apache Tomcat 3.x or later is an excellent choice. Note: The example of this tutorial is written using a container that conforms to JSP 1.2. Tiles Framework. This framework can be obtained as part of the Struts 1.1 download package or as a separate component from the tiles Web site. Source. I have provided two versions: a version with jar files and a version with no JAR files for narrowband users. Struts comes with an empty war file Struts-blank.war (in the WebApps directory) that describes which configuration files and jar files you need, and where you usually put them. You will use the same structure for the example code in this tutorial.

See Resources for information on these materials and additional resources.

Tiles Framework and Architecture

Tiles Framework

The Tiles framework completely uncovers the concepts within jsp:includes-thus allowing you to more flexibly create reusable pages. Using the Tiles framework, developers can build pages by combining reusable tile. You should think of tile as a visual component.

The Tile layout is a special JSP page that allows other Tile to be placed on it. The Tile layout controls where the Tile is placed on the page. In many ways, the tile layout is similar to the template layout. In fact, if you've used Struts before, you'll notice that the Tile framework is compatible with the template custom tag library.

Terminology Interpretation

The terminology that appears in this tutorial may seem a bit overwhelming at first, so before we discuss the Tiles framework in more detail, let's first define some important terms.

Glossary of Terms   Tiles Struts is used to create a template framework that represents a component. page Tile The WEB resource that is included in the layout. Tile the same page. A range of other tile is inserted in the tile layout of the zone . A zone has logical names such as headers and footers. The Tile layout describes the JSP pages where other pages should be positioned. The Tile layout acts as a template, defining areas to insert into other Tile. A tile layout can be a tile of another tile layout. defines the parameters that are used to invoke a tile layout.

Tile Layout

In some ways, the tile layout works like a display function. To use a tile layout, you can use the Tiles:insert tag to invoke it. When you call the tile layout, you pass parameters to it. These parameters will become the properties of the tile layout, for example, the parameters will be placed in the tile range.

Parameters passed when calling tile can be other JSP pages or WEB resources that you can insert into a predefined location in the layout, called a zone. The parameter also contains a string that can be inserted into the tile layout. In fact, many types of objects can be passed as parameters to the tile. These parameters become tile-scoped properties that are available only for the tile.

The tile range is similar to a page range because the tile range is more specialized than the request scope. The tile range allows tile users to pass parameters (called attributes) to tile. The tile range allows you to pass variables (called attributes) that are available only for the tile layout or tile. Special custom labels allow you to copy attributes from the tile range to a page, request, session, or application scope, or to display properties as contained Web resources.

Default Parameters

Some programming languages, such as C + +, Visual Basic, and Python, allow you to pass default parameters to functions and methods. To further extend this display function, the Tiles framework also allows you to pass default parameters to the tile layout. To do this, you must define a tile definition. The Tile definition allows you to define default parameters for Tile. Tile definitions (definition) can be defined in JSP code or XML.

As with classes extending other classes, definitions can extend other definitions. By using a definition and tile layout, you can create reusable display components.

You can use Tiles with struts, or you can use Tiles without struts. To use Tiles with struts, you will use the Tiles tag library that is included with struts. In addition, the Tiles framework includes its own requestprocessor, which is used to handle the tile layout as a actionforward-allowing you to go to the tile definition instead of to the JSP page. Tile is achieved by rewriting Processactionforward in its requestprocessor.

A typical tile layout

A typical tile layout might define a rectangular area for headers, footers, menus, and bodies, as shown in Figure 1.

The area shown in Figure 1 can be mapped to a Web site similar to the one shown in Figure 2.

Note that the reusable part of the application can easily be redefined simply by passing the correct parameters. For example, an employee manifest might use the same headers and footers, but with different menus and bodies, you can still use all of the common layout areas defined by the tile layout. This allows the same tile layout to be reused for different content. Unlike the inclusion of HTML tags, you will include content in the markup.

Master Tiles Framework (ii)--tiles layout and definition

1. Tile Layout

building the first tile layout

Wouldn't it be nice if the site could reuse the same layout (implemented using HTML tables) and images without having to repeat the same HTML code?

Tile is especially good at creating a common look for your site. That said, many developers do not realize that Tiles is also excellent at creating reusable components implemented in JSP.

If you find yourself repeating the same HTML code on multiple pages, consider using a tile layout for those pages. Similarly, if you use the same HTML or JSP tags in different places on different pages, this is also a good place to use tile to create small visual components.

As an example of the Tiles framework, the following will refactor a simple stock quote application to take advantage of the tile layout, as shown in Figure 3.

Sample Application

This simple sample application consists primarily of a stock quote page that has a form (index.jsp) that accepts a single parameter (that is, the stock code). Another page displays the value of the stock quote (QUOTE.JSP).

Look at the following two code listings. You will refactor them to use a variety of tile layouts.

index.jsp View Code

<%@ taglib uri= "/web-inf/struts-html.tld" prefix= "html"%> <%@ taglib uri= "/web-inf/struts-bean.tld"
     Bean "%>  

quote.jsp View Code

 <%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%>  

To learn how to use the Tiles framework, you first have to write a tile layout, and then refactor the two examples above so that they don't have to repeat so much HTML code.

To create a tile layout incrementally

To create a tile layout, you need to do the following: Find similarities between two pages. Create a new layout page. Create two new content pages that contain only the differences between and. EMPLOYEELISTING.JSPDEPTLISTING.JSP inserts the tile layout into the page-that is, to let and insert the tile layout in their pages, passing the content as a parameter and passing other necessary arguments (such as headings). EmployeeListing.jspDeptListing.jsp

Since finding similarities between two pages requires skills in HTML layout and Web site applicability, it turns out that the job is more like an art than a science. For some reason, having purple hair and tattoos is helpful. If you don't think so, you can ask my friend Boo.

This tutorial focuses on Struts, not the necessary skills for HTML layout and Web site applicability. So you don't know anything about tattoos and purple hair. In fact, the HTML layout in the example is deliberately simplistic to avoid distracting us from the Tiles framework.

Create a tile layout

Once you've identified the similarities between pages (this is a difficult part), you'll be able to create new layout pages (this is the easy part). To create a tile layout, you must do the following: Use the Tag Library directive to import the tile tag library into the JSP and import any other tag libraries that you want. Use string parameters to display content that uses labels like a page. Tiles:getasstring uses labels to insert tile into the appropriate area of the layout. Tiles:insert uses the tiles:put tag to pass any required parameters to the internal tile-the label is a child tag of the Tiles:insert tag.

Import the tile tag library into the JSP and import any other tag libraries you want, as shown below (sitelayout.jsp): View Code

<%@ taglib uri= "/web-inf/struts-html.tld

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: 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.