sunriver

Java, Spring, Osgi, ...
个人资料
  • 博客访问:
文章分类
归档
正文

spring 2.5 with tiles 2.0

(2008-05-21 21:06:41) 下一个

Spring 2.5.1 MVC + Tiles 2.0.5 基本配置

当我兴致高昂的从http://tiles.apache.org下载了tilesjar包,并且按照google上搜到的一篇教程http://esffor.javaeye.com/blog/96052进行部署,却怎么也测试不成功。
页面中的都无法正常输出,日志打印出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.4LibrariesJDK1.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>trainingservlet-name>
        
<servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        
servlet-class>
        
<load-on-startup>1load-on-startup>
    
servlet>
    
<servlet-mapping>
        
<servlet-name>trainingservlet-name>
        
<url-pattern>*.htmurl-pattern>
    
servlet-mapping>
    
<welcome-file-list>
        
<welcome-file>index.jspwelcome-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">

<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.xmlvalue>
            
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配置成功了。

[ 打印 ]
阅读 ()评论 (0)
评论
目前还没有任何评论
登录后才可评论.