Sealyu

--- 博客已迁移至: http://www.sealyu.com/blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 Trackbacks
从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配置成功了。


posted on 2009-10-20 17:02 seal 阅读(660) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: