Introduction to tiles labels in struts

Source: Internet
Author: User
Tags custom name

Tiles Introduction XML: namespace prefix = o ns = "urn: Schemas-Microsoft-com: Office: office"/>

I found that in the documents about tiles, the introduction to this aspect in struts is the best for beginners (I personally think), So I translated it, for those who want to know this and do not want to read e, refer. Translation Vulnerabilities are inevitable. I hope you can refer to the original article and correct the errors. Thank you.


1. Introduction tiles framework allows us to combine tiles that can be reused to build Web pages. For example, a page in is constructed by combining a header, a footer, a menu, and a body. Aspectratio = "T" V: ext = "edit"> every tiles (header, menu, body ,...) they are all JSP pages. They can also be built by combining other tiles. The application tiles is like the application Java Method : You need to define tiles (method body) before you can "call" The method body wherever you need it and pass some parameters. In tiles Parameters Obfuscated, parameters are called attributes ). The tiles method body (or content-Translator's note) can be a simple JSP page or a struts action ), or any URI pointing to an existing webpage resource. You can insert or call tiles by placing the following tag in any position in JSP: <tiles: insert...>. Insert can also be completed using the following method: specify a custom name as the struts forward path; or specify a custom name as the struts action input, forward, or include attribute. The tiles method is used to create layout, reusable parts, and so on, while the tiles insert operation is used to insert tiles. The same tiles can be reused multiple times on the same site or even the same page. Tiles insertion can be associated with a logical name called "Definition" in tiles. The definition of a tiles contains a logical name, a subject page, and some attribute values. The definition declaration does not insert the tiles method body associated with it. It only associates tiles with its logical name. You can use the defined logical name in any place where tiles can be inserted, so that the corresponding tiles and its attributes are inserted. The definition of tiles can be defined on the JSP page or in one or more central control files. Definition can inherit from another tiles definition, overload some attributes, and add some new attributes. This allows the definition of the "parent class" to define some general layout, header, menu, and footer. all other definition inherited from the parent class can simply change the entire page by changing its definition. 2. A simple example (1) is inserted into the JSP page:
<tiles:insert page="/layouts/commonLayout.jsp" flush="true" />
 
In this example, the page specified in the tag is inserted into the whole page. The page attribute can be directed to any existing Network A valid resource URL. (2) Insert a tiles and pass the attribute value simultaneously:
<tiles:insert page="/layouts/classicLayout.jsp" flush="true">
       <tiles:put name="title" value="Page Title" />
       <tiles:put name="header" value="/common/header.jsp" />
       <tiles:put name="footer" value="/common/footer.jsp" />
        <tiles:put name="menu"   value="/common/menu.jsp" />
       <tiles:put name="body"   value="/tiles/mainBody.jsp" />
</tiles:insert>
In this example, the specified page is inserted and its attribute value is passed. Attribute values are stored in the context of tiles. This context is also passed to the inserted tag and can be accessed by their names. (3) Use string to obtain the attribute <tiles: getasstring name = "title"/>. In this example, the value of the "tilte" attribute is obtained, and print it as a string in the current output stream. The tostring () method is applied to this attribute value and can be used as a value to pass any object. (4) Insert tiles through reference attributes
<tiles:insert attribute='menu' />
In this example, tiles referenced by the "menu" attribute value is inserted. The current tiles context first obtains the attribute value and then inserts it as the page target. (5) The typical page layout example is a page layout composed of a typical header-footer-menu-body.
<tr>
 <td colspan="2"><tiles:insert attribute="header" /></td>
</tr>
<tr>
 <td width="140" valign="top">
    <tiles:insert attribute='menu' />
 </td>
 <td valign="top" align="left">
    <tiles:insert attribute='body' />
 </td>
</tr>
<tr>
 <td colspan="2">
    <tiles:insert attribute="footer" />
 </td>
</tr>
</table>
</body>
The layout can be declared on a JSP page (for example,/layouts/classiclayout. jsp ). It can be used together with the tag described in example (2. 3. The definition of tiles (definitions) the definition of tiles contains a logical name with a tiles URL and some attribute values. Defines whether tiles is inserted. Insert is completed later with the definition name. The logic name of a definition can be used multiple times in the site for easy reuse of tiles. Tiles's definition can inherit from another definition, and can reload its attributes or add attributes. This makes it easier to construct different definitions through some attributes. For example, you can define a "parent class" definition and define the main header, menu, footer, and a default title. Then, make the definitions of each page inherit the definition of the "parent class" and reload the title and body attributes. The definition of tiles can be defined on the JSP page or in one or more central control files. To make the definitions defined in the central control file valid, You need to initialize the "Definitions factory". This definition pool can interpret the definitions in the file and provide them to the tiles framework. 4. Make the definition pool effective. To make the tiles definitions defined in the file take effect, you need to write down these files and initialize the definition pool. Initialization varies depending on whether struts is applied or the struts version used (only the configuration of struts1.1 is described here ). Struts1.1 apply the tiles plug-in (plug-in) to make tiles definition take effect. This plug-in creates a definition pool and passes it to the configuration object with parameters. Parameters can be configured in the web. xml file or as parameters of the plug-in. The plug-in first reads parameters from web. XML, and then reloads the parameters that are also configured in the plug-in. All parameters are optional and can be ignored. The plug-in should declare in every struts-config.xml file:
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
    <set-property property="definitions-config" 
                          value="/WEB-INF/tiles-defs.xml,
                            /WEB-INF/tiles-tests-defs.xml,/WEB-INF/tiles-tutorial-defs.xml,
                            /WEB-INF/tiles-examples-defs.xml" />
    <set-property property="moduleAware" value="true" />
    <set-property property="definitions-parser-validate" value="true" />
 </plug-in>
L definitions-config: (optional) Name of the configuration file. Multiple File names can be separated by commas. L definitions-parser-validate: (optional) specify whether the XML interpreter verifies the tiles configuration file. True: verification. DTD should be specified at the beginning of the file (default) False: not verified. L moduleaware: (optional) specifies whether the tiles definition pool has been implemented by modules. If it is true (default), each struts module has a definition pool. If it is false, there will be a definition pool shared by all modules. For the latter, each module still needs to define a plug-in. The definition pool will be initialized with the parameter values found in the first plug-in Plug-in (usually the plug-in associated with the default module. True: the tiles framework corresponds to a module. False: the tiles framework has only one shared definition pool (default) among all modules ). L tilesutilimplclassname :( optional -- Advanced Used by the user) specifies the name of the class implemented by tilesutil. The specified class should be a subclass of the tilesutilstrutsimpl class. This option invalidates the moduleaware option. If "tilesutilstrutsimpl" is specified, it is equivalent to moduleaware = false. If "tilesutilstrutsmoduleimpl" is specified, it is equivalent to moduleaware = true. When this item is first encountered, it is executed and only once. To avoid problems, we recommend that you use the same value in all tilesplugin statements. The tilesplugin class creates a definition pool for each struts module. If moduleaware is false, a shared definition pool is created for all modules. For the latter, the definition pool is initialized with parameters found in the first plugin. The corresponding plugin should be declared in each module, and in the whole application Program Moduleaware must all be in the same status. The paths in tiles are relative to main context. You do not need to specify the specific tilesrequestprocessor. Plug-in will automatically process the process. However, if you want to specify your own requestprocessor, it should inherit from tilesrequestprocessor. Plug-in checks this constraint. 5. syntax of the definition file the syntax of the definition file can be found in the tiles-config_1_1.dtd file. The following is a simple example:
 
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
 
<!-- Definitions for Tiles documentation   -->
<tiles-definitions>
 
 <!-- ========================================================== -->
 <!-- Master definition                                          -->
  <!-- ========================================================== -->
 <!-- Main page layout used as a root for other page definitions -->
 
 <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" />
          <put name="footer" value="/tiles/common/footer.jsp" />
          <put name="body"   value="/tiles/body.jsp" />
 </definition>
 
 <!-- ========================================================== -->
 <!-- Index page definition                                      -->
 <!-- ========================================================== -->
    <!-- This definition inherits from the main definition.
          It overloads the page title and the body used.
          Use the same mechanism to define new pages sharing common 
           properties (here header, menu, footer, layout)
         -->
 
 <definition name="site.index.page" extends="site.mainLayout" >
          <put name="title" value="Tiles Blank Site Index" />
          <put name="body"   value="/tiles/body.jsp" />
 </definition>
 
</tiles-definition>
6. debugging If You Want To debug a page created by tiles, follow the following suggestions: l check each tiles independently. Direct Access The nested tiles Test Are they running normally.

Tiles logs take effect. For more information, see the Help file of the commons-logging package.

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.