最近因工作需要,了解了一下Apache Tiles,习惯性的看了一下官方文档,还是感觉有点晕。哈,其实就是看着一大堆 E文,头痛罢了,这时就想偷个懒,看看有没有相关的文章,起码让我先把环境搭起来,写个hello world,然后再看其他特性。杯具的是,在网上找了好久,这类的资源非常少,仅有的那俩资源也没能引导我把这个hello world给弄出来,没法子,只有硬着头皮看官方文档了。。。
好了,废话不多说了,直接上代码:
配置方法一:web.xml文件添加如下语句(加载一切)
<listener> <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class> </listener>在web-inf文件夹下加入tiles-defs.xml,内容如下:
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.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/header.jsp" /> <put-attribute name="menu" value="/tiles/menu.jsp" /> <put-attribute name="body" value="/tiles/body.jsp" /> <put-attribute name="footer" value="/tiles/footer.jsp" /> </definition> </tiles-definitions>配置方法二:web.xml文件添加如下语句(适用于ssh框架或SpringStrutsMyBatis框架或单独的spring)
<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>spring配置文件中加入以下语句:(spring 必须有 org.springframework.web.servlet-3.1.0.M2.jar)
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/defs/tiles-defs.xml</value> </list> </property> <!-- resolving preparer names as Spring bean definition names --> <property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/> </bean>在web-inf文件夹下建defs文件夹,放入tiles-defs.xml,内容和配置方法一的那个相同
好了,配置基本完成,剩余就是写页面了
其中index页面如下:
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="myapp.homepage" />
classic.jsp如下:
<%@ 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>所需jar如下(哈,偷个懒,把所有tiles开头的jar全都加进去了,事实也就这样,基本上都需要的,其余的jar均可以在tiles-2.2.2-bin下的lib或optional中找到):
commons-beanutils-1.8.0.jar
commons-collections-3.2.jar
commons-digester-2.0.jar
commons-lang-2.4.jar
jcl-over-slf4j-1.5.8.jar
mvel2-2.0.11.jar
slf4j-api-1.5.8.jar
slf4j-jdk14-1.5.8.jar
tiles-api-2.2.2.jar
tiles-compat-2.2.2.jar
tiles-core-2.2.2.jar
tiles-el-2.2.2.jar
tiles-extras-2.2.2.jar
tiles-freemarker-2.2.2.jar
tiles-jsp-2.2.2.jar
tiles-mvel-2.2.2.jar
tiles-ognl-2.2.2.jar
tiles-portlet-2.2.2.jar
tiles-portlet-wildcard-2.2.2.jar
tiles-servlet-2.2.2.jar
tiles-servlet-wildcard-2.2.2.jar
tiles-template-2.2.2.jar
tiles-velocity-2.2.2.jar