手记_Apache Tiles

Apache Tiles手记

目录

Apache Tiles手记

Composite View Pattern

View Helper

Configuring Tiles

Required libraries

Starting Tiles engine

Tiles concepts

Template

Attribute

Definition

View Preparer

Building Tiles-enabled pages

Create a template

Create the composing pages

Create a definition

Render the definition

TilesDispatchServlet

TilesDecorationFilter

Tiles Advanced Topics


 

http://tiles.apache.org/framework/index.html

Composite View Pattern

The Composite View pattern formalizes this typical use, by allowing to create pages that have a similar structure, in which each section of the page vary in different situations.

The template organizes the page according to this layout, putting each "piece" in the needed place, so that the header goes up, the footer down, etc.

It can happen that, for example clicking on a link, it is needed to change only a part of the page, typically the body.

As you can see, the pages are different, but their difference is only in the body part. Note that, however, the pages are distinct, it is not like a refresh of a frame in a frameset!

View Helper

Each piece of the composed page can have a "view helper". This pattern allows the preparation of the data to be displayed in a consistent way for the page piece itself, for example to create a menu.

 

*****sitemesh:http://wiki.sitemesh.org/wiki/display/sitemesh/Home

 

Configuring Tiles

Required libraries

<groupId>org.apache.tiles</groupId>

<artifactId>tiles-extras</artifactId>

 

Starting Tiles engine

<listener>
    <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
</listener>
 
<servlet>
    <servlet-name>Tiles Dispatch Servlet</servlet-name>
    <servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Tiles Dispatch Servlet</servlet-name>
    <url-pattern>*.tiles</url-pattern>
</servlet-mapping>

This means that any request to an URL ending in ".tiles" will be dispatched directly to the matching Tiles Definition.

 

Tiles concepts

Tiles is an implementation of the Composite View pattern. Tiles adds to this pattern its own concepts to make the pattern concrete. The implementation of Tiles around the Composite View pattern consists of the TemplateAttribute and Definition concepts. The View Helper pattern is implemented by the View Preparer concept.

Template

In Tiles, a template is the layout part of a page. You can see as a page structure with some gaps, called attributes, to be filled.

For instance, consider the "classic layout" page structure.

You can replicate this structure by creating a JSP page, as you can see below.

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<table>
  <tr>
    <td colspan="2">
      <tiles:insertAttribute name="header" />
    </td>
  </tr>
  <tr>
    <td>
      <tiles:insertAttribute name="menu" />
    </td>
    <td>
      <tiles:insertAttribute name="body" />
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <tiles:insertAttribute name="footer" />
    </td>
  </tr>
</table>

Notice that a template can have no attributes: in this case it can be used directly.

 

Attribute

An attribute is a gap in a template that needs to be filled in your application. An attribute can be of three types:

  • string: it is a string to be directly rendered as it is.
  • template: it is a template, with or without attributes. If it has attributes, you have to fill them too to render a page.
  • definition: it is a reusable composed page, with all (or some) attributes filled (see below).

Definition

definition is a composition to be rendered to the end user; essentially a definition is composed of a template and completely or partially filled attributes.

  • If all of its attributes are filled, it can be rendered to the end user.
  • If not all of its attributes are filled, it is called an abstract definition, and it can be used as a base definition for extended definitions, or their missing attributes can be filled at runtime.

For example, you can create a page using the classic layout as seen before, by modifying the Tiles configuration file.

<definition name="myapp.homepage" template="/layouts/classic.jsp">
  <put-attribute name="header" value="/tiles/banner.jsp" />
  <put-attribute name="menu" value="/tiles/common_menu.jsp" />
  <put-attribute name="body" value="/tiles/home_body.jsp" />
  <put-attribute name="footer" value="/tiles/credits.jsp" />
</definition>

View Preparer

Sometimes a definition, before it is rendered, needs to be "prepared". For example when showing a menu, the menu structure must be created and stored in the request scope.

For this reason, a View Preparer can be used: it is called before the definition is rendered, so all the things needed to render correctly the definition can be prepared.

See the Tiles View Preparer configuration for more information.

Following:

View Preparers

Sometimes a definition, before it is rendered, needs to be "prepared". For example when showing a menu, the menu structure must be created and stored in the request scope.

For this reason, a View Preparer can be used: it is called before the definition is rendered, so all the things needed to render correctly the definition can be prepared.

Creating a view preparer

A View Preparer is simply a class that implement the ViewPreparer interface. The execute method allows to execute code before a definition is rendered. You can extend the ViewPreparerSupport class to avoid compiling problems in the case the ViewPreparer interface changes.

package my.package;
 
import org.apache.tiles.preparer.PreparerException;
import org.apache.tiles.preparer.ViewPreparer;
import org.apache.tiles.request.Request;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.Attribute;
 
public class TestViewPreparer implements ViewPreparer {
 
    public void execute(Request tilesRequest, AttributeContext attributeContext)
    throws PreparerException {
        attributeContext.putAttribute(
            "body",
            new Attribute("This is the value added by the ViewPreparer"));
    }
}

Associating a preparer

To associate a preparer to a definition, put its complete path name to the preparer attribute of the <definition> element:

<definition name="preparer.definition" preparer="org.apache.tiles.test.preparer.TestViewPreparer">
  <put-attribute name="foo" value="/bar/foo.jsp" />
</definition>

 

Building Tiles-enabled pages

Create a template

Let's take the classic layout page structure:

Create a JSP page that acts as this layout and place it under /layouts/classic.jsp file.

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>
  <head>
    <title><tiles:getAsString name="title"/></title>
  </head>
  <body>
        <table>
      <tr>
        <td colspan="2">
          <tiles:insertAttribute name="header" />
        </td>
      </tr>
      <tr>
        <td>
          <tiles:insertAttribute name="menu" />
        </td>
        <td>
          <tiles:insertAttribute name="body" />
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <tiles:insertAttribute name="footer" />
        </td>
      </tr>
    </table>
  </body>
</html>

This template has five attributes: title (of string type), headermenubody and footer.

Create the composing pages

In this phase, you have to create four JSP pages, that will take place of headermenubody and footer attributes in the previously created template.

You can put everything you want in this pages, they are just a test.

Create a definition

By default, the definition file is /WEB-INF/tiles.xml. If you're using CompleteAutoloadTilesListener, tiles will use any file in the webapp that matches /WEB-INF/tiles*.xml or any file in the classpath that matches /META-INF/tiles*.xml; if several are found, it will merge them together.

But for now, let's stick to the default and create the /WEB-INF/tiles.xml file, with a definition as described in concepts:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
  <definition name="myapp.homepage" template="/layouts/classic.jsp">
    <put-attribute name="title" value="Tiles tutorial homepage" />
    <put-attribute name="header" value="/tiles/banner.jsp" />
    <put-attribute name="menu" value="/tiles/common_menu.jsp" />
    <put-attribute name="body" value="/tiles/home_body.jsp" />
    <put-attribute name="footer" value="/tiles/credits.jsp" />
  </definition>
</tiles-definitions>

Render the definition

After creating the definition, you can render it:

  • by using the <tiles:insertDefinition /> tag, inserting it in a JSP page:
  • <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="myapp.homepage" />
  • in other cases, you can render directly in the response, by using the Tiles container:
  • TilesContainer container = TilesAccess.getContainer(
  •         request.getSession().getServletContext());
container.render("myapp.homepage", request, response);
  • by using Rendering Utilities provided by Tiles. For instance, if you've configured TilesDispatchServlet, you can render the definition above by requesting http://example.com/webapp/myapp.homepage.tiles.
  • by using a supporting framework. See Integrations for a list of supporting frameworks.

Tiles Rendering Utilities

The core package of Tiles contains utilities to render Tiles definitions without the need of a JSP page or a supporting framework.

TilesDispatchServlet

The TilesDispatchServlet is a servlet that intercepts the URLs ending with ".tiles" and render the definition whose name is the path name before the ".tiles" part.

For example, if you call the testdispatchservlet.tiles path, the rendered definition will be testdispatchservlet.

You can configure the TilesDispatchServlet this way:

<servlet>
    <servlet-name>Tiles Dispatch Servlet</servlet-name>
    <servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class>
</servlet>
...
<servlet-mapping>
    <servlet-name>Tiles Dispatch Servlet</servlet-name>
    <url-pattern>*.tiles</url-pattern>
</servlet-mapping>

By using the org.apache.tiles.web.util.TilesDispatchServlet.CONTAINER_KEY you can use a different container. The value of this parameter will be used as the key under which the container is stored.

TilesDecorationFilter

You can use the TilesDecorationFilter to use Tiles as a decorator framework. All the requests intercepted by the filter will be put inside the configured attribute of the configured definition, and then that definition is rendered.

You can configure the filter this way:

<filter>
    <filter-name>Tiles Decoration Filter</filter-name>
    <filter-class>org.apache.tiles.web.util.TilesDecorationFilter</filter-class>
    <init-param>
        <param-name>definition</param-name>
        <param-value>test.definition</param-value>
    </init-param>
    <init-param>
        <param-name>attribute-name</param-name>
        <param-value>body</param-value>
    </init-param>
</filter>

By using the org.apache.tiles.web.util.TilesDecorationFilter.CONTAINER_KEY you can use a different container. The value of this parameter will be used as the key under which the container is stored.

Integration in Spring MVC: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/view.html#view-tiles

 

Tiles Advanced Topics

http://tiles.apache.org/framework/tutorial/advanced/index.html

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值