Basic usage of tiles-defs.xml files in struts1.2

Source: Internet
Author: User
Tags documentation xml parser
One: Description
Struts1.1 later added tiles pack so struts has a choice in the processing of the page. and easier to implement
Reuse of code.
The Division of the page in tiles is a bit like TDK in the Jakarta of another project turbine. Added the concept of layout.
In fact, a page is divided into several pieces. Generally speaking, a page can be divided into the following blocks:
Head Page Header: Store A public information used: logo, if it is the site may be the top piece.
Menu Page Menus: Place a menu to be used in an application, or a connection that is used on every page.
Footer Page Tail: such as copyright information.
The Body page topic content: Each page relatively independent content.
If you press the above division that for each page we just write the contents of the body, others can share reuse.
If most of the pages are basically the same layout, we can even use a JSP file to invoke different body types based on different parameters.

II: Introduction to Tiles Configuration and basic configuration files
Tiles has a configuration file: Tiles-defs.xml
Tiles-defs.xml defines the elements and forms of each page.
Below I will explain a tiles-defs.xml file as shown below
Tiles-defs.xml
-----------------------------------------------
<tiles-definitions>
<!--the constituent name of the definition/layouts/classiclayout.jsp is site.mainlayout-->
<!--will be appended with/layouts/classiclayout.jsp content-->
<definition name= "Site.mainlayout" path= "/layouts/classiclayout.jsp" >
<put name= "title" value= "Tiles Blank Site"/>
<put name= "header" value= "/tiles/common/header.jsp"/>
<put name= "Menu" value= "Site.menu.bar"/>
<!--menu is composed of Site.menu.bar corresponding page-->
<put name= "Footer" value= "/tiles/common/footer.jsp"/>
<put name= "Body" value= "/tiles/body.jsp"/>
</definition>
<!--define Site.index.page, inherit site.mainlayout-->
<definition name= "Site.index.page" extends= "Site.mainlayout" >
<put name= "title" value= "Tiles Blank Site Index"/>
<put name= "Body" value= "/tiles/body.jsp"/>
<!--the above two elements will replace the elements in Site.mainlayout-->
</definition>

<definition name= "Site.menu.bar" path= "/layouts/vboxlayout.jsp" >
<putlist name= "List" >
<add value= "Site.menu.links"/>
<add value= "Site.menu.documentation"/>
</putList>
</definition>
</tiles-definitions>

Attached:/layouts/classiclayout.jsp
--------------------------------
<title><tiles:getasstring name= "title"/>
</title>

<body bgcolor= "#ffffff" text= "#000000" link= "#023264" alink= "#023264" vlink= "#023264" >
<table border= "0" width= "100%" cellspacing= "5" >
<tr>
&LT;TD colspan= "2" ><tiles:insert attribute= "header"/></td>
</tr>
<tr>
&LT;TD width= "140" valign= "Top" >
<tiles:insert attribute= ' Menu '/>
</td>
&LT;TD valign= "Top" align= "left" >
<tiles:insert attribute= ' body '/>
</td>
</tr>
<tr>
&LT;TD colspan= "2" >
<tiles:insert attribute= "Footer"/>
</td>
</tr>
</table>
</body>

Configure the tiles inside the web.xml and configure the Struts action servlet as follows:
Xml
-----------------
<!--Action Servlet Configuration-->
<servlet>
<servlet-name>action</servlet-name>
<!--specify servlet class to use:
-Struts1.0.x:actioncomponentservlet
-Struts1.1:actionservlet
-No Struts:tilesservlet
-->
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<!--Tiles Servlet parameter
Specify configuration file names. There can be several comma
Separated file names
-->
<init-param>
<param-name>definitions-config</param-name>
<param-value>/WEB-INF/tiles-defs.xml</param-value>
</init-param>

<!--Tiles Servlet parameter
Specify Tiles debug level.
O:no Debug Information
1:debug Information
2:more Debug Information
-->
<init-param>
<param-name>definitions-debug</param-name>
<param-value>1</param-value>
</init-param>

<!--Tiles Servlet parameter
Specify Digester debug level. This value is passed to Digester
O:no Debug Information
1:debug Information
2:more Debug Information
-->
<init-param>
<param-name>definitions-parser-details</param-name>
<param-value>0</param-value>
</init-param>

<!--Tiles Servlet parameter
Specify if XML parser should validate the Tiles file.
True:validate. DTD should is specified in file header.
False:no validation
-->
<init-param>
<param-name>definitions-parser-validate</param-name>
<param-value>true</param-value>
</init-param>

<!--struts configuration, if struts is used-->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>

<load-on-startup>2</load-on-startup>
</servlet>

Third: Using tiles
If you have configured Tiels-defs.xml, you can then use these definitions in your JSP files.
Use tiles in the same way
3.1:
<tiles:insert definition= "Site.mainlayout" flush= "true"/>
Inserts a page of the site.mainlayout tag

3.2:
<tiles:insert template= "/tutorial/basic/myframesetlayout.jsp" >
<tiles:put name= "title" content= "My frameset page" direct= "true"/>
<tiles:put name= "header" content= "/tutorial/common/header.jsp" direct= "true"/>
<tiles:put name= "Footer" content= "/tutorial/common/footer.jsp" direct= "true"/>
<tiles:put name= "Menu" content= "/tutorial/basic/menu.jsp" direct= "true"/>
<tiles:put name= "Body" content= "/tutorial/basic/hellobody.jsp" direct= "true"/>
</tiles:insert>

/tutorial/basic/myframesetlayout.jsp
---------------------------------
<title><tiles:get name= "title"/></title>

<frameset rows=, * >
<frame src= "<%=request.getcontextpath ()%><tiles:get name=" header "/>" name= "header" >
<frame src= "<%=request.getcontextpath ()%><tiles:get name=" Body "/>" name= "Body" >
<frame src= "<%=request.getcontextpath ()%><tiles:get name=" Footer "/>" name= "Footer" >
</frameset>


Insert/tutorial/basic/myframesetlayout.jsp
and set the value of title to: my frameset page
Header set to/tutorial/common/header.jsp


Four: PostScript
The use of tiles in his documents is more detailed. These are some simple and basic uses. The specific documentation can be seen
A Tiles-documentation.war bag inside the struts. But even this bag is not very full. Can be passed on the
Connect to the author's homepage to find it.
Personally feel that the use of tiles in doing business use may not be as good as the site to reflect the advantages. But at the beginning of the system
Design to consider and plan the entire UI, which will save a lot of effort when modifying and maintaining, because the usual UI determines
At the end of code writing and completion, all as much as possible using multiple subpages to form a page, behind the beautification and maintenance than directly
It's easy to maintain a large page.
Because I also used a tiles, if the above document has errors, thank you very much for correcting me.

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.