1.創建項目
在eclipse裏選擇新建Maven Project,然後選擇mave-archetype-webapp的Archetype,這樣就會創建一個Eclipse WTP能識別的Web項目,能夠Run on server。
2.修改POM
爲項目加入相關的dependency,包括Spring,tiles等。
2.1添加Spring的依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<scope>compile</scope>
</dependency>
這裏只添加spring-webmvc的依賴,Maven會根據spring-webmvc包裏的pom找到他需要的其他相關jar包的依賴,包括spring-core.jar,spring-bean.jar等。
2.2添加Tiles的依賴
在Apache Tiles的官方網站上的Download(http://tiles.apache.org/download.html)頁面裏有如何在Maven里加Tiles的依賴。
<!--[if !supportLists]-->· <!--[endif]--> The complete Tiles dependency with all supported technologies:
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>2.2.2</version>
</dependency>
<!--[if !supportLists]-->· <!--[endif]-->The basic Tiles dependency with only servlet support:
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>2.2.2</version>
</dependency>
<!--[if !supportLists]-->· <!--[endif]-->Tiles JSP support:
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.2.2</version>
</dependency>
2.3添加jstl依賴
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
2.4添加Servlet相關的Provided依賴
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
3.修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>tiles</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
這裏servlet-name的值是tiles,並且沒有給DispatcherServlet配置contextConfigLocation(可以通過這個值指定spring的配置文件),Spring MVC會從WEB-INF裏去找tiles-servlet.xml的spring的配置文件。
4.tiles-servlet.xml
相關注視都在文檔裏
<?xml version="1.0" encoding="utf-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd "
default-autowire="byName"
default-lazy-init="false">
<!-- 當用戶訪問在webapp文件夾下的靜態資源的時候,通過下面的配置直接訪問,而不去找Controller -->
<mvc:resources
mapping="/resources/**"
location="/resources/" />
<!-- 爲Spring MVC添加annotation-driven,該配置包括了對JSR-303 Validation,message conversion和filed
formatting -->
<mvc:annotation-driven />
<!-- 配置Spring掃描Controller存放的包路徑 -->
<context:component-scan
base-package="com.rever.tiles.mvc" />
<!-- 配置Spring MVC的視圖,從WEB-INF/views文件夾下去找對應的視圖 -->
<!-- p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="1" -->
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 爲了使用JSTL Tag修改默認的viewClass屬性 -->
<property
name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property
name="prefix"
value="/WEB-INF/views/" />
<property
name="suffix"
value=".jsp"></property>
<property
name="order"
value="1"></property>
</bean>
<!-- Convenience subclass of UrlBasedViewResolver that supports TilesView
(i.e. Tiles definitions) and custom subclasses of it. -->
<!-- Don't forget to set the order if you declared other ViewResolvers -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/view/tiles2/TilesViewResolver.html -->
<bean
id="tilesviewResolver"
class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
<property
name="order"
value="0"></property>
</bean>
<!-- Helper class to configure Tiles 2.x for the Spring Framework -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/view/tiles2/TilesConfigurer.html -->
<!-- The actual tiles templates are in the tiles-definitions.xml -->
<bean
id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property
name="definitions">
<list>
<value>/WEB-INF/tiles-definitions.xml</value>
</list>
</property>
</bean>
</beans>
5. tiles-definitions.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>
<!-- We declare a new template named template-main. This template is used
for displaying the main page. It has 4 attributes. These attributes are placeholder
for our contents For each attribute, we have assigned a corresponding JSP -->
<definition
name="template-main"
template="/WEB-INF/views/layouts/main.jsp">
<put-attribute
name="banner-content"
value="/WEB-INF/views/sections/banner.jsp" />
<put-attribute
name="title-content"
value="Pet Type" />
<put-attribute
name="primary-content"
value="" />
<put-attribute
name="footer-content"
value="/WEB-INF/views/sections/footer.jsp" />
</definition>
<!-- We declare a new template named template-detail. This template is used
for displaying details of an item. It has 5 attributes. These attributes
are placeholder for our contents For each attribute, we have assigned a corresponding
JSP -->
<definition
name="template-detail"
template="/WEB-INF/views/layouts/details.jsp">
<put-attribute
name="banner-content"
value="/WEB-INF/views/sections/banner.jsp" />
<put-attribute
name="title-content"
value="Pet Type" />
<put-attribute
name="subtitle-content"
value="" />
<put-attribute
name="primary-content"
value="" />
<put-attribute
name="footer-content"
value="/WEB-INF/views/sections/footer.jsp" />
</definition>
<!-- Concrete page. To use this page, your controller must return the name
"pet-tiles" -->
<definition
name="pet-tiles"
extends="template-main">
<put-attribute
name="primary-content"
value="/WEB-INF/views/contents/pets.jsp" />
</definition>
<!-- Concrete page. To use this page, your controller must return the name
"dog-tiles" -->
<definition
name="dog-tiles"
extends="template-detail">
<put-attribute
name="subtitle-content"
value="Canines" />
<put-attribute
name="primary-content"
value="/WEB-INF/views/contents/dogs.jsp" />
</definition>
<!-- Concrete page. To use this page, your controller must return the name
"cat-tiles" -->
<definition
name="cat-tiles"
extends="template-detail">
<put-attribute
name="subtitle-content"
value="Felines" />
<put-attribute
name="primary-content"
value="/WEB-INF/views/contents/cats.jsp" />
</definition>
</tiles-definitions>
在tiles的配置文件裏定義了兩個模板(template-main和template-detail),template-main模板的模板jsp文件是layouts/main.jsp,其內容如下:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body {
background: #555;
width: 400px;
border: 1px solid #fff;
padding: 0px;
}
div {
padding: 5px;
margin: 0px;
}
h1,h2,p {
padding: 0px;
margin: 0px;
}
#banner-style {
background: #3B3E37;
}
#title-style {
background: #665845;
}
#subtitle-style {
background: #9F8158;
}
#primary-style {
background: #EBC785;
}
#footer-style {
background: #733027;
}
</style>
<title>Insert title here</title>
</head>
<body style="background: #555;">
<div id="banner-style">
<tiles:insertAttribute name="banner-content" />
</div>
<div id="title-style">
<h2>
<tiles:insertAttribute name="title-content" />
</h2>
</div>
<div id="primary-style">
<tiles:insertAttribute name="primary-content" />
</div>
<div id="footer-style">
<tiles:insertAttribute name="footer-content" />
</div>
</body>
</html>
這裏定義了banner-content,title-content,primary-content和footer-content幾個大塊內容,這些和tiles配置文件裏的template-main模板裏的元素對應。
<put-attribute
name="banner-content"
value="/WEB-INF/views/sections/banner.jsp" />
<put-attribute
name="title-content"
value="Pet Type" />
<put-attribute
name="primary-content"
value="" />
<put-attribute
name="footer-content"
value="/WEB-INF/views/sections/footer.jsp" />
從上面的代碼中可以看出main.jsp中的banner-content使用sections/banner.jsp文件(應該是使用include)。title-content在這裏就寫死了,是Pet Type,當然子模板也可以覆蓋這個屬性。Primary-content這裏沒有設置內容,需要子模板提供相應的數據。Footer-content使用sections/footer.jsp文件。
子模板pet-tiles就繼承了上面template-main模板,其內容如下:
<definition
name="pet-tiles"
extends="template-main">
<put-attribute
name="primary-content"
value="/WEB-INF/views/contents/pets.jsp" />
</definition>
可以看出,子模板中沒有指定banner-content,title-content和footer-content,只是指定了primary-content,使用contents/pets.jsp文件。
6.Controller
Controller裏沒有什麼好說的,唯一需要說明的是,在controller的方法裏返回的視圖名不是真正對應的jsp名,要對應tiles配置文件裏的名字。比如上面的pet-tiles,而不能返回pets。
Tiles+Spring MVC相關的內容從http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html裏看的,可以去這裏查找原文章。