Tiles framework

xiaoxiao2021-03-06  95

Chapter 16 TILES Framework ... 2

16.1 Create a composite web page with basic JSP statement ... 2

16.2 Creating a composite web page with JSP's Include instructions ... 6

16.3 Create a composite web page in the tag ... 10

16.4 Creating a composite web page with TILES template ... 12

16.5 Create a composite web page with TILES template and TILES components ... 14

16.5.1 Basic Usage of TILES Components ... 15

16.5.2 call TILES components through struts action ... 17

16.5.3 Combination of TILES components ... 17

16.5.4 Extension of TILES components ... 19

16.6 section ... 20

Chapter 16 TILES Framework

When developing a Web site, you often require all Web pages of the same site to maintain a consistent look, such as with the same layout, page, page, and menus. Figure 16-1 shows a typical webline layout.

Menu

HEADER

Content

Footer

Figure 16-1 A typical webline layout

In Figure 16-1, the web page is divided into four parts: Header, Menu, Footer and Content. For all web pages of the same site, the header, menu and the Footer section are the same, and only the contents of the Content portion are different. If you use basic JSP statements to write all Web pages, it is clear that a large number of duplication codes are used, increase development and maintenance costs.

The TILES framework provides a template mechanism for the creation web page, which separates the layout and content of the web page. It allows the template to be created first, and then insert the contents to the template at runtime. The TILES framework is based on the JSP's Include instruction, but it provides a more powerful feature than the JSP's Include instruction. The TILES framework has the following features:

l Create reusable templates

l Dynamic build and load page

l Define reusable TILES components

l Support internationalization

The TILES framework contains the following:

l tiles tag library

l TILES component configuration file

l tilesplugin plugin

This chapter steps from a gradual introduction of several programs that construct a composite web page displayed in Figure 16-1, each of which is based on the previous program. The sample programs of this chapter provide a separate version for Tilestaglibs applications.

16.1 Create a composite web page using basic JSP statement

The most basic way to create a dynamic web page is to create a separate JSP file for each page. Figure 16-2 and Figure 16-3 are the main page index.jsp and product page for Tilestaglibs applications, respectively.

This section introduces the source program of the Tilestaglibs application in the SourceCode / TileStaglibs / Version1 / Tilestaglibs directory of the supporting CD. If you want to post this app on Tomcat, just copy the entire TileStaglibs subdirector of the version1 directory to the / webapps directory.

Figure 16-2 Main page of Tilestaglibs Applications INDEX.JSP

Figure 16-3 Product page product page for TileStaglibs Application Product.jsp

Routines 16-1 and routines 16-2 are source code for index.jsp and product.jsp, respectively.

Routines 16-1 INDEX.JSP

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

Tilestaglibs Sample </ TISTLE></p> <p></ hEAD></p> <p><body></p> <p><% - One Table Lays Out of The Content for this Page -%></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - sidebar -%></p> <p><td width = "150" valign = "top" align = "left" bgcolor = "# ccffcc"></p> <p><table></p> <p><tr></p> <p><% - Sidebar Top -%></p> <p><td width = "150" height = "65" valign = "top" align = "left"></p> <p><a href=""></p> <p><img src = "chinese.gif" border = "0" /> </A></p> <p><a href=""></p> <p><img src = "usa.gif" border = "0" /> </A></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Sidebar Bottom -%></p> <p><TD></p> <p><font size = "5"> links </ font> <p></p> <p><a href="index.jsp"> Home </a> <br></p> <p><a href="product.jsp"> Products </A> <br></p> <p><a href=""> Hot link1 </a> <br></p> <p><a href=""> Hot link2 </a> <br></p> <p><a href=""> Hot link3 </a> <br></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p><% - main content -%></p> <p><TD Valign = "TOP" Height = "100%" width = "*"></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - header -%></p> <p><TD Valign = "TOP" Height = "15%"></p> <p><font size = "6"> Welcome to abc inc. </ font></p> <p><hr></p> <p></ td></p> <p><tr></p> <p><tr></p> <p><% - Content -%></p> <p><TD Valign = "TOP" Height = "*"></p> <p><font size = "4"> Page-Specific Content Goes Here </ font></p> <p></ td></p> <p></ TR> <TR></p> <p><% - Footer -%></p> <p><TD Valign = "Bottom" Height = "15%"></p> <p><hr></p> <p>Thanks for Stopping By!</p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>Routines 16-2 Product.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><html></p> <p><HEAD></p> <p><title> Tilestaglibs Sample </ Title></p> <p></ hEAD></p> <p><body></p> <p><% - One Table Lays Out of The Content for this Page -%></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - sidebar -%></p> <p><td width = "150" valign = "top" align = "left" bgcolor = "# ccffcc"></p> <p><table></p> <p><tr></p> <p><% - Sidebar Top -%></p> <p><td width = "150" height = "65" valign = "top" align = "left"></p> <p><a href=""></p> <p><img src = "chinese.gif" border = "0" /> </A></p> <p><a href=""></p> <p><img src = "usa.gif" border = "0" /> </A></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Sidebar Bottom -%></p> <p><TD></p> <p><font size = "5"> links </ font> <p></p> <p><a href="index.jsp"> Home </a> <br></p> <p><a href="product.jsp"> Products </A> <br></p> <p><a href=""> Hot link1 </a> <br></p> <p><a href=""> Hot link2 </a> <br></p> <p><a href=""> Hot link3 </a> <br></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p><% - main content -%></p> <p><TD Valign = "TOP" Height = "100%" width = "*"> <Table Width = "100%" height = "100%"></p> <p><tr></p> <p><% - header -%></p> <p><TD Valign = "TOP" Height = "15%"></p> <p><font size = "6"> Welcome to abc inc. </ font></p> <p><hr></p> <p></ td></p> <p><tr></p> <p><tr></p> <p><% - Content -%></p> <p><TD Valign = "TOP" Height = "*"></p> <p><font size = "4"> Products </ font> <p></p> <p><Li> Product1 </ li> <br></p> <p><li> product2 </ li> <br></p> <p><li> product3 </ li> <br></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Footer -%></p> <p><TD Valign = "Bottom" Height = "15%"></p> <p><hr></p> <p>Thanks for Stopping By!</p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>As can be seen from routine 16-1 and routine 16-2, in index.jsp and product.jsp files, only the code blocks of only the crude word identifier are not repeated, and the rest are repeated code. If the same part of the web page needs to change, you must manually modify all JSP files. It can be seen that the basic JSP statement is used to write the above web page, which will cause a large amount of redundancy of JSP code, increase development and maintenance costs.</p> <p>16.2 Creating a compound web page with JSP's Include instruction</p> <p>In order to reduce the redundancy of the code, you can put the same part in Index.jsp and Product.jsp, and then include other JSP files through the JSP Include instruction in the index.jsp and product.jsp files. Figure 16-4 and Figure 16-5 show other JSP files included in the index.jsp and product.jsp files, respectively.</p> <p>The source program of the Tilestaglibs application described in this section is located in the Sourcecode / TileStaglibs / Version2 / TileStaglibs directory of the supporting CD. If you want to post this app on Tomcat, just copy the entire TileStaglibs subdirectory in the Version2 directory to the <catalina_home> / webapps directory.</p> <p>Sidebar.jsp</p> <p>HEADER.JSP</p> <p>IndexContent.jsp</p> <p>FOOTER.JSP</p> <p>Figure 16-4 INDEX.JSP contains other JSP files</p> <p>Sidebar.jsp</p> <p>HEADER.JSP</p> <p>ProductContent.jsp</p> <p>FOOTER.JSP</p> <p>Figure 16-5 Other JSP files included in Product.jsp</p> <p>As can be seen from Figures 16-4 and 16-5, including header.jsp, sidebar.jsp, and footer.jsp, SideBar.jsp, and Footer.jsp, only the JSP files included in the web main body part. Routines 16-3, 16-4, 16-5, 16-6, 16-7, 16-8 and 16-9 are header.jsp, footer.jsp, sidebar.jsp, indexcontent.jsp, productcontent.jsp , INDEX.JSP and PRODUCT.JSP source code. Routines 16-3 Header.jsp</p> <p><font size = "6"> Welcome to abc inc. </ font></p> <p><hr></p> <p>Routines 16-4 FOOTER.JSP</p> <p><hr></p> <p>Thanks for Stopping By!</p> <p>Routines 16-5 SideBar.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><table></p> <p><tr></p> <p><% - Sidebar Top Component -%></p> <p><td width = "150" height = "65" valign = "top" align = "left"></p> <p><a href=""> <img src = "chinese.gif" border = "0" /> </A></p> <p><a href=""> <img src = "usa.gif" border = "0" /> </A></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Sidebar Bottom Component -%></p> <p><TD></p> <p><table></p> <p><tr></p> <p><TD></p> <p><font size = "5"> links </ font> <p></p> <p><a href="index.jsp"> Home </a> <br></p> <p><a href="product.jsp"> Products </A> <br></p> <p><a href=""> Hot link1 </a> <br></p> <p><a href=""> Hot link2 </a> <br></p> <p><a href=""> Hot link3 </a> <br></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p>Routines 16-6 indexContent.jsp</p> <p><font size = "4"> Page-Specific Content Goes Here </ font></p> <p>Routines 16-7 ProductContent.jsp</p> <p><font size = "4"> Products </ font> <p></p> <p><Li> Product1 </ li> <br></p> <p><Li> Product2 </ li> <br> <li> Product3 </ li> <br></p> <p>Routines 16-8 Index.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><html></p> <p><HEAD></p> <p><title> Tilestaglibs Sample </ TISTLE></p> <p></ hEAD></p> <p><body></p> <p><% - One Table Lays Out of The Content for this Page -%></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - Sidebar Section -%></p> <p><td width = "150" valign = "top" align = "left" bgcolor = "# ccffcc"></p> <p><jsp: include page = "sidebar.jsp" /></p> <p></ td></p> <p><% - main content section -%></p> <p><TD Height = "100%" width = "*"></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - header section -%></p> <p><TD Valign = "TOP" Height = "15%"></p> <p><jsp: include page = "header.jsp" /></p> <p></ td></p> <p><tr></p> <p><tr></p> <p><% - Content Section -%></p> <p><TD Valign = "TOP" Height = "*"></p> <p><jsp: include page = "indexcontent.jsp" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Footer Section -%></p> <p><TD Valign = "Bottom" Height = "15%"></p> <p><jsp: include page = "footer.jsp" /></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>Routines 16-9 Product.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><html></p> <p><HEAD></p> <p><title> Tilestaglibs Sample </ TISTLE></p> <p></ hEAD></p> <p><body></p> <p><% - One Table Lays Out of The Content for this Page -%></p> <p><table width = "100%" height = "100%"> <TR></p> <p><% - Sidebar Section -%></p> <p><td width = "150" valign = "top" align = "left" bgcolor = "# ccffcc"></p> <p><jsp: include page = "sidebar.jsp" /></p> <p></ td></p> <p><% - main content section -%></p> <p><TD Height = "100%" width = "*"></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - header section -%></p> <p><TD Valign = "TOP" Height = "15%"></p> <p><jsp: include page = "header.jsp" /></p> <p></ td></p> <p><tr></p> <p><tr></p> <p><% - Content Section -%></p> <p><TD Valign = "TOP" Height = "*"></p> <p><jsp: include page = "productcontent.jsp" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Footer Section -%></p> <p><TD Valign = "Bottom" Height = "15%"></p> <p><jsp: include page = "footer.jsp" /></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>The JSP Include instruction is used to create a composite page, which has taken the right step in improving the code reusability. The same content contained in Index.jsp and Product.jsp is placed in a separate JSP page. INDEX.JSP and Product.JSP simply include these same contents through the JSP Include instruction, which improves the reusability of the code. However, the JSP Include instruction does not completely avoid code redundancy, such as from routine 16-8 and routine 16-9, there is still a lot of repetition code in Index.jsp and Product.jsp, only a crude word identity code block Not a duplication code.</p> <p>In addition, the number of JSP files increases, although the second solution is reduced, the number of JSP files increases, and the complexity of the software has also increased.</p> <p>16.3 Create a composite page with <Tiles: Insert> tab</p> <p>The <tiles: Insert> tag and JSP Include instruction of the TILES tag library have the same function, and other JSP pages can be inserted into the current page. For example, the effect of the following two statements is the same:</p> <p><jsp: include page = "indexcontent.jsp" /></p> <p><tiles: insert page = "indexcontent.jsp" Flush = "true" /> <tiles: Insert> Tag Page Property Specifies the inserted JSP file, the optional value of the Flush property includes True and false, when the Flush property is TRUE , Indicating the FLUSH () method of the output stream of the current page before performing the insertion operation.</p> <p>The source program of the Tilestaglibs application described in this section is located in the Sourcecode / TileStaglibs / Version3 / Tilestaglibs directory of the supporting CD. If you want to publish this app on Tomcat, just copy the entire TileStaglibs subdirectory under the Version3 directory to the <catalina_home> / webappus directory.</p> <p>The following is the step of using the <Tiles: Insert> tag in Tilestaglibs applications.</p> <p>(1) Install the file required for the Tiles tag library</p> <p>The files required to run the Tiles tag library are included in the Struts download software. If the Tiles tag library is used in web applications, the following files must be in the web-inf / lib directory:</p> <p>l struts.jar</p> <p>l Commons-Digester.jar</p> <p>l Commons-beanutils.jar</p> <p>l Commons-Collects.jar</p> <p>l commons-logging.jar</p> <p>In addition, the definition files for the Tiles tag library should be copied to the web-infers.</p> <p>(2) Configure the <taglib> element in the web.xml file:</p> <p><taglib></p> <p><taglib-uri> /web-inf/struts-tiles.tld </ taglib-uri></p> <p><taglib-location> /web-inf/struts-tiles.tld </ taglib-location></p> <p></ taglib></p> <p>(3) Create index.jsp and product.jsp files</p> <p>Modify the 16.2 routine 16-8 (Index.jsp) and routine 16-9 (Product.jsp), at the beginning of index.jsp and product.jsp file, introduce Tiles tag libraries through <% @ taglib> instruction, Then change the JSP INCLUDE instruction in the source code to the <Tiles: Insert> tag. Routines 16-10 and routines 16-11 are modified index.jsp and product.jsp files, respectively.</p> <p>Routine 16-10 ndex.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><html></p> <p><HEAD></p> <p><title> Tilestaglibs Sample </ TISTLE></p> <p></ hEAD></p> <p><body></p> <p><% - One Table Lays Out of The Content for this Page -%></p> <p><Table Width = "100%" Height = "100%"></p> <p><TR> <% - Sidebar Section -%></p> <p><td width = "150" valign = "top" align = "left" bgcolor = "# ccffcc"></p> <p><Tiles: Insert Page = "SideBar.jsp" Flush = "true" /></p> <p></ td></p> <p><% - main content section -%></p> <p><TD Height = "100%" width = "*"></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - header section -%></p> <p><TD Valign = "TOP" Height = "15%"></p> <p><tiles: INSERT Page = "Header.jsp" flush = "true" /></p> <p></ td></p> <p><tr></p> <p><tr></p> <p><% - Content Section -%></p> <p><TD Valign = "TOP" Height = "*"></p> <p><Tiles: Insert Page = "IndexContent.jsp" Flush = "true" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Footer Section -%></p> <p><TD Valign = "Bottom" Height = "15%"></p> <p><Tiles: Insert Page = "Footer.jsp" Flush = "true" /></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>Routines 16-11 Product.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><html></p> <p><HEAD></p> <p><title> Tilestaglibs Sample </ TISTLE></p> <p></ hEAD></p> <p><body></p> <p><% - One Table Lays Out of The Content for this Page -%></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - Sidebar Section -%></p> <p><td width = "150" valign = "top" align = "left" bgcolor = "# ccffcc"></p> <p><Tiles: Insert Page = "SideBar.jsp" Flush = "true" /> </ td></p> <p><% - main content section -%></p> <p><TD Height = "100%" width = "*"></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - header section -%></p> <p><TD Valign = "TOP" Height = "15%"></p> <p><tiles: INSERT Page = "Header.jsp" flush = "true" /></p> <p></ td></p> <p><tr></p> <p><tr></p> <p><% - Content Section -%></p> <p><TD Valign = "TOP" Height = "*"></p> <p><Tiles: Insert Page = "ProductContent.jsp" Flush = "true" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Footer Section -%></p> <p><TD Valign = "Bottom" Height = "15%"></p> <p><Tiles: Insert Page = "Footer.jsp" Flush = "true" /></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>As can be seen from routines 16-10 and routines 16-11, replacing the JSP INCLUDE directive with the <Tiles: INSERT> tag to create a composite page, the code is only a slight difference, and the advantages and disadvantages of the two are also very similar. Use the <Tiles: Insert> tag to create a composite page, and have not fully utilized the advantage of the TILES framework.</p> <p>16.4 Creating a compound web page with TILES template</p> <p>In Section 16.3, although <Tiles: Insert> tags, index.jsp, and product.jsp files still have a lot of repetition code. In order to improve the reusability and maintainability of the web page, Tiles template mechanism can be introduced.</p> <p>The source program of the Tilestaglibs application described in this section is located in the SourceCode / TileStaglibs / Version4 / Tilestaglibs directory of the supporting CD. If you want to post this app on Tomcat, just copy the entire TileStaglibs subdirectory in the Version4 directory to the <catalina_home> / webapps directory.</p> <p>Popular speaking, Tiles template is a JSP page that describes page layout. Tiles templates only define the style of the web page, not to specify content. Insert a specific content in the template page when the web application is running. The same template can be shared by multiple web pages.</p> <p>Using templates, you can easily implement all of the page of the web application to remain the same appearance and layout without having to hard coding each page. In an app, most pages use the same template, some pages may require different appearances, using other templates, so an application may have more than one template.</p> <p>The following is the step of using the Tiles template in the Tilestaglibs application. (1) Install the file required for the TILES tag library, step 1 of 16.3.</p> <p>(2) Configure the <taglib> element in the web.xml file, step 2 of 16.3.</p> <p>(3) Define the template file, see routines 16-12.</p> <p>Routines 16-12 Layout.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><html></p> <p><HEAD></p> <p><title> Tilestaglibs Sample </ TISTLE></p> <p></ hEAD></p> <p><body></p> <p><% - One Table Lays Out All of the Content -%></p> <p><Table Width = "100%" Height = "100%"></p> <p><% - Sidebar Section -%></p> <p><tr></p> <p><td width = "150" valign = "top" align = "left" bgcolor = "# ccffcc"></p> <p><tiles: insert attribute = "sidebar" /></p> <p></ td></p> <p><% - main content section -%></p> <p><TD Valign = "TOP" Height = "100%" width = "*"></p> <p><Table Width = "100%" Height = "100%"></p> <p><tr></p> <p><% - header section -%></p> <p><TD Height = "15%"></p> <p><tiles: insert attribute = "header" /></p> <p></ td></p> <p><tr></p> <p><tr></p> <p><% - Content Section -%></p> <p><TD Valign = "TOP" Height = "*"></p> <p><Tiles: Insert Attribute = "Content" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><% - Footer Section -%></p> <p><TD Valign = "Bottom" Height = "15%"></p> <p><tiles: insert attribute = "footer" /></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>The layout of the web page is defined in the template file layout.jsp, but does not specify the specific content of each part. Layout.jsp contains multiple <tiles: INSERT> tags, and its Attribute property specifies the logical name of the content to be inserted without specifying a file that is really inserted. (4) Use the TILES template in Index.jsp and Product.jsp, see routines 16-13 and routines 16-14.</p> <p>Routines 16-13 Index.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><tiles: INSERT Page = "Layout.jsp" Flush = "True"></p> <p><tiles: put name = "sidebar" value = "sidebar.jsp" /></p> <p><tiles: put name = "header" value = "Header.jsp" /></p> <p><Tiles: Put name = "content" value = "indexcontent.jsp" /></p> <p><Tiles: Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ tiles: insert></p> <p>Routines 16-14 Product.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><tiles: INSERT Page = "Layout.jsp" Flush = "True"></p> <p><tiles: put name = "sidebar" value = "sidebar.jsp" /></p> <p><tiles: put name = "header" value = "Header.jsp" /></p> <p><tiles: put name = "content" value = "productcontent.jsp" /></p> <p><Tiles: Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ tiles: insert></p> <p>In Index.jsp and Product.jsp, the <tiles: Insert> tab specifies the inserted template file, index.jsp, and product.jsp use the same template file Layout.jsp. The <tiles: Insert> tag contains several <Tiles: PUT> sub-tags, which specifies the specific content inserted into the template. <Tiles: PUT> Name Properties and Template Files in the Template File, the attribute property matching, the value of the <tiles: Put> tag specifies the specific JSP file inserted into the template.</p> <p>The TILES template mechanism greatly improves the reusability and maintainability of the code, and the template contains web-common layout. If the layout changes, you only need to modify the template file without modifying the specific web file. However, from routine 16-13 and 16-14, it can be seen, although index.jsp and product.jsp files are shortened, but both still have duplicate code. 16.5 Creating a composite page with Tiles template and Tiles components</p> <p>In order to maximize the reusability and flexibility of the code, the TILES framework introduces the concept of Tiles components. The TILES component can represent a complete web page or a part of the web page. Simple TILES components can be combined into complex Tiles components, or are extended to complex TILES components.</p> <p>16.5.1</p> <p>Basic use of TILES components</p> <p>The TILES framework allows TILES components to be configured in a dedicated XML file. For example, the following code defines a TILES component called "index-definition", which describes the entire index.jsp page:</p> <p><Tiles-Definitions></p> <p><definition name = "index-definition" Path = "/ layout.jsp"></p> <p><put name = "sidebar" value = "sidebar.jsp" /></p> <p><put name = "header" value = "Header.jsp" /></p> <p><Put Name = "Content" value = "indexcontent.jsp" /></p> <p><Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ definition></p> <p></ tiles-definitions></p> <p><Definition> Element Name Properties Specifies the name of the Tiles component, the Path property specifies the template used by the Tiles component, <PUT> sub-elements of the <definition> element are used to insert specific web content into the template.</p> <p>This section describes the source program of the Tilestaglibs application in the SourceCode / TileStaglibs / Version5 / Tilestaglibs directory of the supporting CD. If you want to post this app on Tomcat, just copy the entire TileStaglibs subdirectory under the Version5 directory to the <catalina_home> / webapps directory.</p> <p>The following is the step of using Tiles components in Tilestaglibs applications.</p> <p>(1) Install the files required for the Tiles tag library, step 1 in 16.3</p> <p>(2) Configuring <taglib> elements in the web.xml file, step 2 in 16.3.</p> <p>(3) Configure TILES components in a special XML file, named this profile to Tiles-Defs.xml in this example, this file is located in a web-inflicity. Routines 16-15 is the code for the Tiles-Defs.xml file.</p> <p>Routines 16-15 TILES-Defs.xml</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p>DOCTYPE TILES-Definitions PUBLIC</p> <p>"- // Apache Software Foundation // DTD TILES Configuration 1.1 // en"</p> <p>"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd"> <Tiles-Definitions></p> <p><definition name = "index-definition" Path = "/ layout.jsp"></p> <p><put name = "sidebar" value = "sidebar.jsp" /></p> <p><put name = "header" value = "Header.jsp" /></p> <p><Put Name = "Content" value = "indexcontent.jsp" /></p> <p><Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ definition></p> <p><definition name = "product-definition" Path = "/ layout.jsp"></p> <p><put name = "sidebar" value = "sidebar.jsp" /></p> <p><put name = "header" value = "Header.jsp" /></p> <p><put name = "content" value = "productcontent.jsp" /></p> <p><Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ definition></p> <p></ tiles-definitions></p> <p>The above code defines two TILES components that represent the complete index.jsp and Product.jsp page, respectively.</p> <p>(4) Configure the TILESPLUGIN plugin in the strut configuration file, the code is as follows:</p> <p><plug-in classname = "org.apache.struts.tiles.tilesplugin"></p> <p><set-protety proty = "definitions-config" value = "/ web-inf / tiles-defs.xml" /></p> <p><set-protety property = "definitions-parser-validate" value = "true" /></p> <p></ plug-in></p> <p>The TILESPLUGIN plug-in is used to load the TILES component configuration file. In the <plug-in> element contains several <set-print> 子 子, it is used to pass to the TILESPLUGIN plug-in to add additional parameters:</p> <p>l Definitions-config parameters: Specify the configuration file of the Tiles component, if there are multiple configuration files, then divide it with a comma.</p> <p>l Definitions-Parser-Validate Parameters: Specify whether the XML parser verifies Tiles profiles, optional values ​​include True and false, the default value is TRUE.</p> <p>(5) Configuring an ActionServlet in a web.xml file</p> <p>To ensure that the TILESPLUGIN plug-in is loaded while the web application is started, you should join the ActionServlet controller, and the ActionServlet controller can load all plugins in initialization. The following is the code for configuring an ActionServlet in a web.xml file: <servlet></p> <p><servlet-name> Action </ servlet-name></p> <p><servlet-class> org.apache.struts.Action.ActionServlet </ servlet-class></p> <p><init-param></p> <p><param-name> config </ param-name></p> <p><param-value> /web-inf/struts-config.xml </ param-value></p> <p></ init-param></p> <p><load-on-startup> 3 </ load-on-startup></p> <p></ servlet></p> <p><servlet-mapping></p> <p><servlet-name> Action </ servlet-name></p> <p><url-pattern> *. do </ url-pattern></p> <p></ servlet-maping></p> <p>(6) Insert TILES components in Index.JSP and Product.jsp, see routines 16-16 and routines 16-17:</p> <p>Routine 16-16 index.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><Tiles: Insert Definition = "Index-Definition" /></p> <p>Routines 16-17 Product.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><tiles: insert definition = "product-definition" /></p> <p>16.5.2</p> <p>Call TILES components via struts action</p> <p>If the TILES component represents a complete web page, you can call TILES components directly via Struts Action. For example, if you want to call via Struts Action</p> <p>16.5.1</p> <p>The TILES component defined as "index-definition" can be configured in the Struts profile:</p> <p><action-mappings></p> <p><action path = "/ index"</p> <p>Type = "org.apache.struts.Actions.forwardAction"</p> <p>Parameter = "INDEX-Definition"></p> <p></ action></p> <p></ action-mappings></p> <p>Next, access http: // localhost: 8080 / tilestaglibs / index.do by the browser, which is first forwarded to forwardaction, forwardaction forwards the request to the Tiles component called "index-definition", and finally in the browser The user will see the same page as INDEX.JSP. To call the TILES component through Struts Action, you can give full play to the function of the Struts framework responsible for process control. In addition, the number of JSP files can be reduced. For example, if you call Tiles components called "index-definition" directly via Struts Action, you don't have to create an index.jsp file.</p> <p>16.5.3</p> <p>Combination of TILES components</p> <p>TILES components are a reusable component. Like a wooden, the simple TILES component is assembled into a complex Tiles component, for example, split the left part of the TILES component named "index-definition" into a separate TILES component, named "Sidebar-Definition" As shown in Figure 16-6.</p> <p>Index-Definition</p> <p>Template file: Layout.jsp</p> <p>Sidebar-definition</p> <p>Template file: sidebar-layout.jsp</p> <p>Flags.jsp</p> <p>Sidebar-links.jsp</p> <p>HEADER.JSP</p> <p>IndexContent.jsp</p> <p>FOOTER.JSP</p> <p>Figure 16-6 Split the left part of the Tiles component called "index-definition" into a separate TILES component</p> <p>The source program of the Tilestaglibs application described in this section is located in the Sourcecode / TileStaglibs / Version6 / Tilestaglibs directory of the supporting CD. If you want to publish this app on Tomcat, just copy the entire TileStaglibs subdirectory under the Version6 directory to the <catalina_home> / webapps directory.</p> <p>The following is the step of using a combined TILES component in a Tilestaglibs application.</p> <p>(1) Three TILES components "Sidebar-Definition", "Index-Definition", and "Product-Definition" are redefined in Tiles-Def.xml files. In a TILES component, the syntax of another TILES component is:</p> <p><definition name = "index-definition" Path = "/ layout.jsp"></p> <p><put name = "sidebar" value = "sidebar-definition" type = "definition" /></p> <p>......</p> <p></ definition></p> <p>The value attribute of the <PUT> child element specifies the name of the TILES component, the Type property is set to "definition", indicating that the value attribute is a TILES component, not a JSP file. Routines 16-18 is the code for the Tiles-Def.xml file.</p> <p>Routines 16-18 TILES-DEF.XML</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p>DOCTYPE TILES-Definitions PUBLIC</p> <p>"- // Apache Software Foundation // DTD TILES Configuration // En"</p> <p>"http://jakarta.apache.org/struts/dtds/tiles-config.dtd"> <Tiles-Definitions></p> <p><definition name = "sidebar-definition" path = "/ sidebar-layout.jsp"></p> <p><put name = "TOP" value = "flags.jsp" /></p> <p><put name = "bottom" value = "sidebar-links.jsp" /></p> <p></ definition></p> <p><definition name = "index-definition" Path = "/ layout.jsp"></p> <p><put name = "sidebar" value = "sidebar-definition" type = "definition" /></p> <p><put name = "header" value = "Header.jsp" /></p> <p><Put Name = "Content" value = "indexcontent.jsp" /></p> <p><Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ definition></p> <p><definition name = "product-definition" Path = "/ layout.jsp"></p> <p><put name = "sidebar" value = "sidebar-definition" type = "definition" /></p> <p><put name = "header" value = "Header.jsp" /></p> <p><put name = "content" value = "productcontent.jsp" /></p> <p><Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ definition></p> <p></ tiles-definitions></p> <p>(2) Create a related JSP file for Tiles components named "Sidebar-Definition".</p> <p>Template files for Tiles components called "Sidebar-Definition" are Sidebar-layout.jsp, which is inserted into this template for: flags.jsp and sidebar-links.jsp. Routines 16-19, 16-20 and 16-21 are the source code for these JSP files, respectively.</p> <p>Roller 16-19 Sidebar-layout.jsp</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%></p> <p><table></p> <p><tr></p> <p><% - Sidebar Top Component -%> <Tiles: Insert Attribute = "TOP" /></p> <p></ TR></p> <p><tr></p> <p><% - Sidebar Bottom Component -%></p> <p><Tiles: Insert Attribute = "Bottom" /></p> <p></ TR></p> <p></ TABLE></p> <p>Routines 16-20 Sidebar-Links.jsp</p> <p><% - Sidebar Bottom Component -%></p> <p><TD></p> <p><table></p> <p><tr></p> <p><TD></p> <p><font size = "5"> links </ font> <p></p> <p><a href="index.jsp"> Home </a> <br></p> <p><a href="product.jsp"> Products </A> <br></p> <p><a href=""> Hot link1 </a> <br></p> <p><a href=""> Hot link2 </a> <br></p> <p><a href=""> Hot link3 </a> <br></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ td></p> <p>Routines 16-21 Flags.jsp</p> <p><% - Sidebar Top Component -%></p> <p><td width = "150" height = "65" valign = "top" align = "left"></p> <p><a href=""> <img src = "chinese.gif" border = "0" /> </A></p> <p><a href=""> <img src = "usa.gif" border = "0" /> </A></p> <p></ td></p> <p>16.5.4</p> <p>TILES components extension</p> <p>in</p> <p>16.5.3</p> <p>In the Tiles-Def.xml file, "index-definition" and "product-definition" two TILES components still have a duplication code. The redundant code can be further eliminated using the scalable characteristics of TILES components. Solution To define a parent class tiles component that contains the common content of these two TILES components, named "Base-Definition", then let "index-definition" and "product-definition" inherit this Parent class components. Figure 16-7 shows the relationship between the improved TILES components.</p> <p>Sidebar-definition</p> <p>Base-definition</p> <p>contain</p> <p>Index-Defintion</p> <p>Product-Definition</p> <p>inherit</p> <p>Figure 16-7 Relationship between the improved TILES components</p> <p>The source program of the Tilestaglibs application described in this section is located in the Sourcecode / TileStaglibs / Version7 / TileStaglibs directory of the supporting CD. If you want to publish this app on Tomcat, just copy the entire TileStaglibs subdirectory under the Version7 directory to the <catalina_home> / webapps directory. A TILES component inherits the syntax of another TILES component as follows, where the extends attribute of the <definition> element specifies the extended parent class tiles component:</p> <p><definition name = "index-definition" extends = "base-definition"></p> <p>Routines 16-22 are code for improved Tiles-Def.xml.</p> <p>RRR 16-22 TILES-DEF.XML</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p>DOCTYPE TILES-Definitions PUBLIC</p> <p>"- // Apache Software Foundation // DTD TILES Configuration 1.1 // en"</p> <p>"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd"></p> <p><Tiles-Definitions></p> <p><definition name = "sidebar-definition" path = "/ sidebar-layout.jsp"></p> <p><put name = "TOP" value = "flags.jsp" /></p> <p><put name = "bottom" value = "sidebar-links.jsp" /></p> <p></ definition></p> <p><definition name = "base-definition" Path = "/ layout.jsp"></p> <p><put name = "sidebar" value = "sidebar-definition" type = "definition" /></p> <p><put name = "header" value = "Header.jsp" /></p> <p><Put Name = "Content" Value = "" /></p> <p><Put Name = "Footer" Value = "Footer.jsp" /></p> <p></ definition></p> <p><definition name = "index-definition" extends = "base-definition"></p> <p><Put Name = "Content" value = "indexcontent.jsp" /></p> <p></ definition></p> <p><definition name = "product-definition" extends = "base-definition"></p> <p><put name = "content" value = "ProductContent.jsp" /> </ definition></p> <p></ tiles-definitions></p> <p>16.6 Section</p> <p>Traditional GUI kits, such as Java AWT and Java Swing, provide some powerful layout managers that specify the distribution location of each view component in the window. The layout manager helps create a composite complex interface, and a compound interface consists of some simple basic interfaces. Use the layout manager to create the GUI interface has the following advantages:</p> <p>l Reusability: The basic interface can be reused, combined into a variety of different composite interfaces.</p> <p>l Sclectability: It can be easily extended to create a more complex interface.</p> <p>l Maintainability: Each basic interface is independent, and when the local area in the composite interface changes, it does not affect other areas</p> <p>Unfortunately, JSP technology itself does not provide a layout or layout manager. To simplify the development of the web page, improve reusability and scalability, the Struts Tiles framework provides a template mechanism, the template defines the layout of the web page, and the same template can be shared by multiple web pages. In addition, the TILES framework also allows a reusable TILES component that can describe a complete web page or part of the web page. Simple TILES components can be combined or extended to become more complex TILES components.</p> <p>This chapter is shallower to introduce several options for creating a composite web page. The TILES framework greatly improves the reusability, scalability, and maintainability of the view layer program code compared to the basic JSP language to create a web page. However, using the TILES framework also increases the difficulty and complexity of development views. If the web application size is small, the interface is very simple, you may wish to use the basic JSP language to write a web page. For large-scale complex web applications, you can use the advantages of TILES framework, and improve the efficiency of web development from the whole.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-123887.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="123887" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.054</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'GqF36Ry_2BSJ_2F56gkvJa23QkmwmqUUBip_2FuztdiuoBrPY8pFeJQzf1iNiAy9Ljg9wAodwgMWFL6hmLeMc2BAobNg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>