Struts tiles framework

Source: Internet
Author: User
Tags tld

Features and content of the tiles framework

The tiles framework provides a template mechanism for creating web pages, which can separate the layout and content of webpages. It allows you to create a template first, and then dynamically insert content into the template at runtime. The tiles framework is based on JSP include commands, but it provides more powerful functions than JSP include commands. The tiles framework has the following features:

◆ Create reusable templates

◆ Dynamic building and loading page

◆ Define reusable tiles Components

◆ Supports Internationalization

The tiles framework includes the following:

◆ Tiles tag Library

◆ Tiles component configuration file

◆ Tilesplugin plug-in

When developing a web site, it is often required that all web pages of the same site have the same appearance, such as the same layout, page header, page end, and menu.

Use Basic JSP statements to create compound web pages

The most basic way to create a dynamic web page is to create an independent JSP file for each page. If the same part of the web page needs to be changed, you must manually modify all JSP files. It can be seen that using the basic JSP statement to compile the above web page will cause the JSPCodeA large number of redundancy, increasing development and maintenance costs.

Use the JSP include command to create a compound webpage

To reduce code redundancy, you can set index. JSP and product. put the same part in JSP in a separate JSP file, and then in index. JSP and product. the JSP file contains other JSP files through the JSP include command. This improves code reusability. However, the JSP include command cannot completely avoid code redundancy. Although this solution reduces repeated code, the number of JSP files has increased, from the original two files to seven files, so the complexity of the software has also increased.

Use the tiles: insert tag to create a compound webpage

The tiles: insert tag of the tiles tag Library has the same function as the JSP include command, and other JSP pages can also be inserted into the current page. Use the tiles: insert label instead of the JSP include command to create a composite page. The code is slightly different, and the advantages and disadvantages of the two are similar. Using the tiles: insert Label alone to create a composite page does not give full play to the advantages of the tiles framework.

The following two statements serve the same purpose:

<? XML: namespace prefix = jsp/> <JSP: Include page = "indexcontent. jsp"> </jsp: Include>
<? XML: namespace prefix = tiles/> <tiles: insert page = "indexcontent. jsp"> </tiles: Insert>

Use tiles templates to create compound web pages

Although the tiles: insert tag is used, there are still a lot of repeated code in the index. jsp and product. jsp files. To improve the reusability and maintainability of web pages, the tiles template mechanism can be introduced. In general, the tiles template is a JSP page that describes the page layout. The tiles template only defines the web page style, but does not specify the content. When a web application is running, the specific content is inserted into the template page. The same template can be shared by multiple web pages. By using templates, you can easily maintain the same appearance and layout of all pages of a Web application without the need to hard code each page. In an application, most pages use the same template. Some pages may have different appearances and use other templates. Therefore, an application may have more than one template.

<% @ taglib uri = "/WEB-INF/struts-tiles.tld" prefix = "tiles" %>




<% @ page contenttype = "text/html; charset = UTF-8 "%>
<% @ taglib uri ="/WEB-INF/struts-tiles.tld "prefix =" tiles "%>





The tiles template mechanism greatly improves code reusability and maintainability. The template contains the common layout of webpages. If the layout changes, you only need to modify the template file without modifying the specific webpage file. However, we can see from the routines 16-13 and 16-14 that although the length of the index. jsp and product. jsp files is shortened, there are still repeated codes.

Basic usage of tiles Components

To maximize code reusability and flexibility, the tiles framework introduces the concept of tiles components. The tiles component can represent a complete webpage or a part of the webpage. Simple tiles components can be combined into complex tiles components or extended into complex tiles components.

The tiles framework allows you to configure the tiles component in a specialized XML file. For example, the following code defines a tiles component named "index-definition", which describes the entire index. jsp webpage:

<Tiles-Definitions>
<Definition name = "index-definition" Path = "/layout. jsp">
<Put value = "sidebar. jsp" name = "sidebar"> </put>
<Put value = "header. jsp" name = "Header"> </put>
<Put value = "indexcontent. jsp" name = "content"> </put>
<Put value = "footer. jsp" name = "footer"> </put>
</Definition>
</Tiles-Definitions>

The name attribute of the definition element specifies the name of the tiles component, and the path attribute specifies the template used by the tiles component. The put sub-element of the definition element is used to insert specific webpage content to the template.

Routines 16-15 tiles-defs.xml


http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd ">


<: put value =" sidebar. JSP "name =" sidebar ">
<: put value =" header. JSP "name =" Header ">
<: put value =" indexcontent. JSP "name =" content ">
<: put value =" footer. JSP "name =" footer ">







The above Code defines two tiles components, which represent the complete index. jsp and product. JSP pages respectively.

(4) configure the tilesplugin plug-in the strut configuration file. The Code is as follows:

<Plug-in classname = "org. Apache. Struts. Tiles. tilesplugin">
<Set-property value = "/WEB-INF/tiles-defs.xml" property = "definitions-config"/>
<Set-property value = "true" property = "definitions-parser-validate"/>
</Plug-in>

The tilesplugin plug-in is used to load the configuration file of the tiles component. The plug-in element contains several set-property sub-elements for passing additional parameters to the tilesplugin plug-in:

◆ Definitions-config parameter: Specifies the configuration file of the tiles component. If multiple configuration files exist, they are separated by commas.

◆ Definitions-parser-validate parameter: Specifies whether the XML Parser verifies the tiles configuration file. Optional values include true and false. The default value is true.

(5) Configure actionservlet in the web. xml file

To ensure that the tilesplugin plug-in is loaded when the web application starts, the actionservlet controller should be added. The actionservlet controller can load all the plug-ins during initialization. The following code configures actionservlet in the web. xml file:

<Servlet>
<SERVLET-NAME> action </SERVLET-NAME>
<SERVLET-CLASS> org. Apache. Struts. Action. actionservlet </SERVLET-CLASS>
<INIT-PARAM>
<PARAM-NAME> config </PARAM-NAME>
<PARAM-VALUE>/WEB-INF/struts-config.xml </PARAM-VALUE>
INIT-PARAM>
<LOAD-ON-STARTUP> 3 </LOAD-ON-STARTUP>
</Servlet>
<SERVLET-MAPPING>
<SERVLET-NAME> action </SERVLET-NAME>
<URL-PATTERN> *. DO </URL-PATTERN>
SERVLET-MAPPING>

(6) Insert the tiles component in index. jsp and product. jsp. For details, see routines 16-16 and routines 16-17:

Example 16-16 index. jsp

<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Taglib uri = "/WEB-INF/struts-tiles.tld" prefix = "tiles" %>
<Tiles: insert definition = "index-definition"> </tiles: Insert>

Routine 16-17 product. jsp

<Ccid_nobr>

Bordercolorlight = "black" bordercolordark = "# ffffff" align = "center">

 

<Ccid_code> <% @ page contenttype = "text/html; charset = UTF-8" %>
<% @ Taglib uri = "/WEB-INF/struts-tiles.tld" prefix = "tiles" %>
<Tiles: insert definition = "product-definition"> </tiles: Insert>

 

Use struts action to call the tiles component

If the tiles component represents a complete web page, you can directly call the tiles component through struts action. For example, if you want to use struts action to call the tiles component named "index-definition" defined in section 16.5.1, you can configure the following action ing in the struts configuration file:

<ACTION-MAPPINGS>
<Action Path = "/Index" P <>
Type = "org. Apache. Struts. Actions. forwardaction"
Parameter = "index-definition">
</Action>
ACTION-MAPPINGS>

Next, access http: // localhost: 8080/tilestaglibs/index through the browser. do: the request is first forwarded to forwardaction, and forwardaction then forwards the request to the tiles component named "index-definition". Finally, on the browser side, the user will see and index. the same JSP page.

The struts action is used to call the tiles component. The Struts framework is responsible for process control. In addition, the number of JSP files can be reduced. For example, if you use struts action to call the tiles component named "index-definition", you do not have to create the index. jsp file.

Resolve the combination and extension of tiles Components

Tiles components are reusable components. A simple tiles component can be assembled into a complex tiles component like a building block, for example, you can split the left part of the tiles component named "index-definition" into an independent tiles component named "sidebar-definition ".

<Definition name = "index-definition" Path = "/layout. jsp">
<Put value = "sidebar-definition" name = "sidebar" type = "Definition"> </put>
......
</Definition>

The Value Attribute of the put sub-element specifies the name of the contained tiles component. The type attribute is set to "Definition", indicating that the value attribute specifies the tiles component rather than the JSP file.

<Definition name = "index-definition" extends = "base-definition">
This article is from: it bubble! (Http://www.itpob.cn) detailed source: http://www.itpob.cn/thread-594-1-1.html

 

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.