JAKARTA STRUTS programming: Use Tiles, the second part
Author: Chuck Cavaness Chen Zhengyong (zhengyongchen@21cn.com) translation
This article is the second part of the series of articles extracted from "Programming Jakarta Struts", we will learn how to install and configure Tiles, and fully understand Tiles.
Install and configure Tiles
Before you can use the Tiles framework, you must make sure it is installed and configured in the web container. The TILES framework does not rely on a specific container, and you have to get the required file and place them in the correct position of the web application.
Download Tiles
The TILES frame is included in the Struts release. In previous versions, it is located in the Contrib folder, but now is part of the core. You can also find the latest source program, binary release versions, and other useful information at http://www.lifl.fr/~dumoulin/tiles/index.html.
Install the required JAR files and related files
The first step is to install the required file. For Tiles, the following files must be in the web-inf / lib directory:
u tiles.jar
u commons-digester.jar
u commons-beanutils.jar
u Commons-collections.jar
u commons-logging.jar
If you are using Struts, these files already exist, and you also need to install the TILES TLD file to the application's web-infers.
WARNING: Don't avoid putting the tiles.jar files to the web-inf / lib directory of each web application, and put it in the ClassPath path of the servlet container, if this will cause the ClassNotFoundException exception.
You should also put the tiles-config.dtd files in the web-inflicity. The DTD file is used to verify the TILES definition file (it will speak in the following section of this chapter).
Add Tiles Tag Library
Like other JSP tag libraries, you must add TILES tag libraries to the web application publishing description file before you can use them. Add the following taglib elements to the web.xml file:
taglib>
If you are using a standard Struts tag library, then taglib elements should already have. Each page that needs to use the Tiles tag must be included in the top:
<% @ Taglib Uri = "/ Web-INF / TILES.TLD" prefix = "tiles"%>
Configure Tiles and Struts to work together
The TILES framework can be used with Struts, or it can be used separately. Depending on how you use it, configure Tiles for the web application to have several options. Because this book speaks Struts, our focus is how to bundle it to the Struts application.
Tip: If you use the previous version of the Tiles framework, you must configure a special ActionServlet called ActionComponentServlet in a web.xml file, and a requestProcessor is also configured in the struts' Controller element. This is now not needed - now has a TILES plugin, which is to complete all initialization. If you are planning to use Tiles, the plugin is very needed. You can also use Tiles libraries and struts without configuring this plugin. However, there is no harm to configure it. If you decide to use TILES definition, it may also save your time.
Add a TILES plugin in the Struts application, add the following plug-in elements in the STRUTS configuration file:
plug-in>
In the plug-in element, you can join several Set-Property elements to pass to the PlugIN class into additional parameters. The definitions-config parameter specifies the XML file that contains Tiles defined. If multiple file names are used, they are separated between commas.
The definitions-debug parameter specifies the hierarchy of debug, the allowable value is:
0: Do not output debugging information.
1: Provide some debugging information.
2: Provide complete debugging information.
The default is 0.
The Definitions-Parser-Details parameter specifies the level of the debug information required when the definition file is parsed, the value is passed to the Commons Digester. The value allowed with the definitions-debug parameters, the default value is 0.
Definitions-Parser-Validate parameters indicate whether the parser verifies the Tiles configuration file, allowing values to true and false, default is true.
There is also a parameter called Definitions-Factory-Class is not displayed. You can create a custom plant and then give the class name here, the default value is org.Apache.struts.tiles.xmldefinition.i18nFactorySet.
Tiles Overview
The TILES framework provides a template mechanism that separates the layout and content of the content. The templates described earlier in this chapter allow you to create a template first, then insert the content to the layout at runtime. If you want to customize your site according to internationalization, users like to change the soup (this will happen early every website), you will find a strong mechanism. The TILES framework has the following features:
U template capabilities
u Dynamic build and load page
u video definition
u Support TILE and layout reuse
u Support internationalization
u Support multi-channel template tag library already exists in a Struts framework for a considerable time, which allows you to organize a JSP page in a web application using very basic template. Although these tags are very helpful to the layout and content of the Isolation web application, the Tiles framework is far more, it is actually a supercoming of the template tag library, with many other features.
Tip: The TILES frame was previously called Components, later because Component's term was abused. Some places in the Tiles document and source are still along the old name.
Cedric Dumoulin created a Tiles framework that extends the concept of templates, providing developers greater flexibility and freedom when using JSP technology to create web applications. The contents of the web application still use the JSP page and JavaBeans, but the layout is specified by a separate JSP page or XML file (later).
What is TILE?
Tile is a region of the web page, one page can be composed of only one area, or decomposed into several areas. Figure 14-1 lists an example of the StoreFront application.
Figure 14-1 Area of the StoreFront application
A JSP page is usually composed of several regions or tiles, and there is nothing special for the page, just in the TILES framework and uses the Tiles tag library.
Tile The most important aspect is that it is reused, which is correct for layout and content. Unlike most JSP pages, TILES components are repeatedly utilized in applications, and may even reuse across applications. Moreover, TILE is not complicated. In fact, most examples we have seen, including Examples 14-2 to Examples 14-4, can be classified as Tiles.
Use layout Tile
In the world of Tiles, the layout is the template we have already mentioned. The layout plays the same role in the template, that is, a set of Tiles to specify the format of the page. In fact, Example 14-2 is a TILE layout. Tiles and Struts included in the template library almost exactly the syntax.
Tip: The TILES framework is a superchard of the standard Struts template tag defined by David Geary, but it provides more features to further extend the concept of templates.
The layout is also a highly evaluated Tiles. JSP pages, even the entire application can reuse the layout. A layout library is usually created for many different projects. TILES framework comes with several pre-established layout Tiles, you can modify it or as needed. The layout containing the Tiles frame is:
u Classic layout: with a header, left list menu, main body, and footer
u Columns layout: with multiple columns, vertical stacks per column
u Center layout: with header, left frame, right frame, main body and footer
u Menu layout: has a link menu
u Tabs layout:
u Vertical Box layout:
Since one of Tiles is a reusability, you can reuse these layouts only in the application. Of course, you can customize the layout as needed.
Layout
The early planning layout is very important. After the establishment of the website, I decide the appearance and absolutely I don't know a good way. This decision is usually made by the humanized team, product management or web developers themselves. Anyway, you should plan a layout before the development.
The default layout of the StoreFront application is introduced in Example 14-2, which will be used throughout this chapter as an example.
Maximum parameter to the layout
The layout Tile shown in Example 14-2 is general, it does not know the contents of itemDetail.jsp or other pages. This is a purposeful, it allows us to rehabilitate this layout in many pages. Unlike hard codes in the layout page, the content is passed to the layout page as a parameter at runtime. Let's take a look at the Signin.jsp Tile of the Storefront app, as shown in Example 14-5: Signin.jsp Tile for the StoreFront application
<% @ Taglib Uri = "/ Web-INF / TILES.TLD" prefix = "tiles"%>
tiles: insert>
The role of the PUT tag in Example 14-5 is to provide layout Tile, and the layout is specified by the included INSERT tag. The value of the Name attribute in Example 14-5 must match the desired layout tile.
Tip: INSERT tag has an optional Ignore property, which allows you to do anything when you find the files referenced by the property. When default, if the file specified by the property is not provided, the runtime exception will be thrown.