手记_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 Template, Attribute 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
A 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), header, menu, body and footer.
Create the composing pages
In this phase, you have to create four JSP pages, that will take place of header, menu, body 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 usingCompleteAutoloadTilesListener, tiles will use any file in the webapp that matches/WEB-INF/tiles*.xmlor 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
手记_Apache Tiles相关推荐
- SpringMVC集成Tiles布局引擎框架
Tiles布局框架, http://tiles.apache.org/ Spring已经对Tiles进行了集成.页头页尾公共模板页要靠这个,不然重复代码太多. <dependency>&l ...
- Apache Tiles 学习(四)、Tiles实战
为什么80%的码农都做不了架构师?>>> 1.创建maven项目 New-->Maven Project--> 勾选上Create a simple project ...
- AM335X的汇编语言与c语言,X86汇编语言学习手记 -- 汇编和C协同
X86汇编语言学习手记(3) 2004年12月 在X86汇编语言学习手记(1)(2)中,可以看到栈(Stack)作为进程执行过程中数据的临时存储区域,通常包含如下几类数据: 局部变量 函数调用的返回地 ...
- 结巴分词和自然语言处理HanLP处理手记
手记实用系列文章: 1 结巴分词和自然语言处理HanLP处理手记 2 Python中文语料批量预处理手记 3 自然语言处理手记 4 Python中调用自然语言处理工具HanLP手记 5 Python中 ...
- 大叔手记(17):大叔2011年读过的书及2012年即将要读的书
前言 2011年是大叔最累的一年(基本上都是晚上12点以后睡觉,早上6点30分起),读得书也是异常的多,一方面要保持自己的技术在最前列,另外方面技术管理和项目管理方面的东西也要进一步进阶,2011年读 ...
- webpack入门学习手记(一)
本人微信公众号:前端修炼之路,欢迎关注. 之前用过gulp.grunt,但是一直没有学习过webpack.这两天刚好有时间,学习了下webpack.webpack要想深入研究,配置的东西比较多,网上的 ...
- 掌握Tiles框架 (二)-- Tiles布局和定义
1. Tile 布局 构建第一个 tile 布局 如果站点能够重用相同的布局(使用 HTML 表格来实现)和图像,而不必重复相同的 HTML 代码,这样不是很好吗? Tile 在为站点创建共同的外观方 ...
- 【手记】解决启动SQL Server Management Studio 17时报Cannot find one or more components...的问题
[手记]解决启动SQL Server Management Studio 17时报Cannot find one or more components...的问题 参考文章: (1)[手记]解决启动S ...
- ELK菜鸟手记 (三) - X-Pack权限控制之给Kibana加上登录控制以及index_not_found_exception问题解决
ELK菜鸟手记 (三) - X-Pack权限控制之给Kibana加上登录控制以及index_not_found_exception问题解决 参考文章: (1)ELK菜鸟手记 (三) - X-Pack权 ...
最新文章
- 【Android 逆向】函数拦截实例 ( ③ 刷新 CPU 高速缓存 | ④ 处理拦截函数 | ⑤ 返回特定结果 )
- 浅谈linux命令大全
- JVM_07 Class文件结构
- 外设驱动库开发笔记12:TSEV01CL55红外温度传感器驱动
- js基础教程学习笔记
- python中控制代码块逻辑关系_一、Python基础知识
- 从底部上滑失灵_iPad 上这 20 个快捷操作,真正帮你提高生产力!
- IDEA中Maven项目中界面右边的Maven Projects中子项目出现灰色
- linux(ubuntu) 查看系统设备信息
- 评委对计算机知识竞赛的提问,评委评分知识竞赛答题软件
- 第四章 结构化程序设计
- redit mysql_样式
- Maya---物体跟随曲线动画
- 关于陌陌和微信表情页与输入法之间切换的问题
- 形态学操作——开运算与闭运算
- 阿里云服务器 远程桌面连接 卡顿
- Java BMI计算程序
- Linux--解决Windows和Ubuntu之间的复制粘贴问题
- 从键盘分别输入年、月、日判断这一天是当年的第几天
- drools-insert与update