我只要想起来就去看看Turbine,但真的很令人失望,自2.3.2以后,好多年没有更新了,与之相关的项目比如maven都发展的很快,甚至不支持Turbine了。
Tiles不是另一种渲染技术,而是一种视图布局技术,这个跟Turbine的布局很相似,甚至更强大。
我使用MyEclipse6.0的Add Spring Capabilities功能加入Spring MVC等所依赖的库,发现已经支持Spring 2.0了。很高兴哦!
当我兴致高昂的从http://tiles.apache.org下载了tiles的jar包,并且按照google上搜到的一篇教程http://esffor.javaeye.com/blog/96052进行部署,却怎么也测试不成功。
页面中的<tiles:getAsString name="title" />和<tiles:insertAttribute name="header" />都无法正常输出,日志打印出TilesContainer not initialized的异常信息。
我已经注意到:
<definition name="template" page="/WEB-INF/tiles/mainTemplate.jsp">
<put name="title" value="default title"/>
<put name="header" value="/WEB-INF/tiles/header.jsp"/>
<put name="content" value="/WEB-INF/tiles/content.jsp"/>
<put name="footer" value="/WEB-INF/tiles/footer.jsp"/>
</definition>
在tiles2中必须改为:
<definition name="template"
template="/WEB-INF/tiles/mainTemplate.jsp">
<put-attribute name="title" type="string" value="default title" />
<put-attribute name="header" type="template"
value="/WEB-INF/tiles/header.jsp" />
<put-attribute name="content" type="template"
value="/WEB-INF/tiles/content.jsp" />
<put-attribute name="footer" type="template"
value="/WEB-INF/tiles/footer.jsp" />
</definition>
网络上使用Spring mvc+tiles的案例和文章极少,我怀疑是Spring 2.0不支持tiles2,但是在tiles的官网上已经找不到更早的版本了。
幸运的是,Spring官网已经发布了2.5.1,在官网2.5.1文档http://static.springframework.org/spring/docs/2.5.x/reference/view.html#view-tiles中,我看到了
14.3. Tiles
It is possible to integrate Tiles - just as any other view technology - in web applications using Spring. The following describes in a broad way how to do this.
NOTE: This section focuses on Spring''s support for Tiles 2 (the standalone version of Tiles, requiring Java 5+) in the org.springframework.web.servlet.view.tiles2 package. Spring also continues to support Tiles 1.x (a.k.a. "Struts Tiles", as shipped with Struts 1.1+; compatible with Java 1.4) in the original org.springframework.web.servlet.view.tiles package.
14.3.1. Dependencies
To be able to use Tiles you have to have a couple of additional dependencies included in your project. The following is the list of dependencies you need.
•
Tiles version 2.0.4 or higher
•
Commons BeanUtils
•
Commons Digester
•
Commons Logging
These dependencies are all available in the Spring distribution.
看来我的怀疑是正确的。
以下是Spring 2.5.1 MVC + Tiles 2.0.5的配置:
--------------------------------------------------------------------------------
从sourceforge下载spring-framework-2.5.1-with-dependencies.zip,并从中拉出以下jar文件(最小化依赖)
#spring 2.5.1
commons-logging.jar
log4j-1.2.14.jar
spring-beans.jar
spring-core.jar
spring-context.jar
spring-web.jar
spring-webmvc.jar
#with tiles 2.0.5
commons-beanutils-1.7.0.jar
commons-digester-1.8.jar
tiles-core-2.0.5.jar
tiles-api-2.0.5.jar
tiles-jsp-2.0.5.jar
jstl.jar
#当然,除此之外还需要为web工程加入j2ee 1.4的Libraries和JDK1.5或以上的Libraries。
步骤:
1、最好配置好log4j,便于在配置过程中排错。
2、修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>training</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>training</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、在WEB-INF目录下增加training-servlet.xml文件,修改其内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Application context definition for "example" DispatcherServlet. -->
<beans>
<bean name="/home.htm" class="Action.TilesHomeController"></bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/tiles-def.xml</value>
</list>
</property>
</bean>
</beans>
4、在WEB-INF目录下增加defs/tiles-def.xml,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="template"
template="/WEB-INF/tiles/mainTemplate.jsp">
<put-attribute name="title" type="string" value="default title" />
<put-attribute name="header" type="template"
value="/WEB-INF/tiles/header.jsp" />
<put-attribute name="content" type="template"
value="/WEB-INF/tiles/content.jsp" />
<put-attribute name="footer" type="template"
value="/WEB-INF/tiles/footer.jsp" />
</definition>
<definition name="courseDetail" extends="template">
<put-attribute name="title" value="courseDetail title"
type="string" />
<put-attribute name="content" type="template"
value="/WEB-INF/tiles/courseContent.jsp" />
</definition>
</tiles-definitions>
5、在WEB-INF目录下增加tiles目录,并在tiles目录下增加以下文件:
mainTemplate.jsp
<%@ page session="false"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<title><tiles:getAsString name="title" />
</title>
</head>
<body>
<table border="2" width="300" bordercolor="Gray">
<tr>
<td bgcolor="Blue">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td>
<tiles:insertAttribute name="content" />
</td>
</tr>
<tr>
<td>
<tiles:insertAttribute name="footer" />
</td>
</tr>
</table>
</body>
</html>
header.jsp
this is from header
content.jsp
<h1>
this is from content
</h1>
footer.jsp
this is from footer
courseContent.jsp
<%@page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
this is courseContent
6、在src下增加一个java class文件:Action.TilesHomeController.java:
package Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
public class TilesHomeController extends AbstractCommandController {
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
return new ModelAndView("courseDetail");
}
@Override
protected ModelAndView handle(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, BindException arg3)
throws Exception {
return null;
}
}
7、部署到tomcat 5.x,启动Tomcat,访问http://localhost:8080/exercise-2.5.1/home.htm,可看到如下网页:
this is from header
this is courseContent
this is from footer
这就说明Spring 2.5 MVC + tiles 2配置成功了。