Java学习从这里JavaVideo开始

JavaVideo java学习从这里开始

当前位置: 主页 > 热门框架 > Struts >

Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Examp

时间:2012-02-23 05:08来源: 作者:admin 点击:
Welcome to Part-4 of the 7-part series where we will go through different aspects for Struts2 Framework with some useful examples. In previous part we

  • Tweet
  • We Recommend These ResourcesGetting Started with FUSE Mediation RouterApache Lucene 3.0: Download the LucidWorks Certified DistributionWhy Load Testing From The Cloud Doesn't WorkNOSQL for the EnterpriseImplementing Enterprise Integration Patterns

    Welcome to Part-4 of the 7-part series where we will go through different aspects for Struts2 Framework with some useful examples. In previous part we went through Struts2 Validation Framework. We saw how easy it is to integrate validation in your struts2 application.

    In this part we will discuss about Tiles Framework and its Integration with Struts2. We will add Tiles support to our HelloWorld Struts application that we created in previous parts. I strongly recommend you to go through previous articles and download the source code of our sample application.

    Struts 2 Tutorial List 
     Related: Struts Tiles Plugin Tutorial with Example
    Introduction to Tiles 2

    Nowadays, website are generally divided into pieces of reusable template that are being rendered among different web pages. For example a site containing header, footer, menu etc. This items remains same through out the website and give it a common look and feel. It is very difficult to hard code this in each and every webpage and if later a change is needed than all the pages needs to be modified. Hence we use templatization mechanism. We create a common Header, Footer, Menu page and include this in each page.

    Tiles Plugin allow both templating and componentization. In fact, both mechanisms are similar: you
    define parts of page (a “Tile”) that you assemble to build another part or a full page. A part can
    take parameters, allowing dynamic content, and can be seen as a method in JAVA language. Tiles is a templating system used to maintain a consistent look and feel across all the web pages of a web application. It increase the reusability of template and reduce code duplication.

    A common layout of website is defined in a central configuration file and this layout can be extended across all the webpages of the web application.

    Our Application Layout

    Our goal is to add Header, Footer and Menu to our StrutsHelloWorld application. Following will be the layout of the same.

    struts2-tiles-layout

    Required JAR files

    In order to add Tiles support to our Struts2 application, we will need few jar files. Following is the list of JARs in our example. Add these JARs in WEB-INF/lib folder.

    struts2-tiles-jar-files

    Configuring Tiles in web.xml

    To configure Tiles, an entry for listener has to be made in web.xml. Open the web.xml from WEB-INF folder and add following code into it.

    <listener>
    <listener-class>

    org.apache.struts2.tiles.StrutsTilesListener
    </listener-class>
    </listener>
    <context-param>
    <param-name>tilesDefinitions</param-name>
    <param-value>/WEB-INF/tiles.xml</param-value>

    </context-param>

    The above code configure Tiles listener in web.xml. An input configuration file /WEB-INF/tiles.xml is passed as argument. This file contains the Tiles definition for our web application.

    Create a file tiles.xml in WEB-INF folder and copy following code into it.

    struts2-tiles-xml

    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE tiles-definitions PUBLIC
    "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
    "">
    <tiles-definitions>
    <definition name="baseLayout" template="/BaseLayout.jsp">
    <put-attribute name="title" value="" />

    <put-attribute name="header" value="/Header.jsp" />
    <put-attribute name="menu" value="/Menu.jsp" />

    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/Footer.jsp" />

    </definition>
    <definition name="/welcome.tiles" extends="baseLayout">
    <put-attribute name="title" value="Welcome" />

    <put-attribute name="body" value="/Welcome.jsp" />
    </definition>
    <definition name="/customer.tiles" extends="baseLayout">

    <put-attribute name="title" value="Customer Form" />
    <put-attribute name="body" value="/Customer.jsp" />

    </definition>
    <definition name="/customer.success.tiles" extends="baseLayout">
    <put-attribute name="title" value="Customer Added" />

    <put-attribute name="body" value="/SuccessCustomer.jsp" />
    </definition>
    </tiles-definitions>
    (责任编辑:JavaVideo)
    顶一下
    (0)
    0%
    踩一下
    (0)
    0%
    ------分隔线----------------------------
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    评价:
    表情:
    用户名: 验证码:点击我更换图片
    栏目列表
    推荐内容