<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tile_exercise</groupId>
<artifactId>tile_exercise</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<jdk.version>1.8</jdk.version>
<spring.version>4.3.4.RELEASE</spring.version>
<freemarker.version>2.3.23</freemarker.version>
<servletapi.version>3.1.0</servletapi.version>
<org.slf4j.version>1.7.21</org.slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- compile only, deployed container will provide this -->
<!-- Need this for config annotation -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servletapi.version}</version>
<!-- scope>provided</scope -->
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-freemarker</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<!--scope>runtime</scope-->
</dependency>
</dependencies>
</project>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-servlet-jsp.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.exercise.controller"/>
<bean id="tileViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles3.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/jsp/tiles-defs.xml</value><!--这里用来加载配置文件,可以有多个-->
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
banner
common_menu.jsp
credits.jsp
home_body.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<tiles:insertDefinition name="myapp.index.homepage" /><!--这里的名称要与 tiles-defs.xml中使用布局文件的definition的name的值一致-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<table>
<tr>
<td colspan="2">
<!--这里的name的值与tiles-difis.xml文件中的值put-attribute的name的值相同-->
<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>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="myapp.homepage" >
<put-attribute name="title" value="Tiles tutorial homepage" />
<!--这里个人理解为用一个名称来对应需要加载的页面-->
<put-attribute name="header" value="/jsp-tiles/banner.jsp" />
<put-attribute name="menu" value="/jsp-tiles/common_menu.jsp" />
<put-attribute name="footer" value="/jsp-tiles/credits.jsp" />
</definition>
<!--这里使用了一个继承关系,并且在这里使用了布局文件,extends的值为需要继承definition的name的值-->
<definition name="myapp.index.homepage" extends="myapp.homepage" template="/WEB-INF/jsp/layout.jsp">
<put-attribute name="body" value="/jsp-tiles/home_body.jsp" />
</definition>
</tiles-definitions>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-servlet-jsp-flt.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- step 1 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.exercise.controller"/>
<bean id="freemarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="WEB-INF/jsp-ftl/" />
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="template_update_delay">0</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
</props>
</property>
</bean>
<!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 -->
<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="requestContextAttribute" value="request" />
</bean>
<bean id="tileViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles3.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<!--注意这里的配置,在方法二没有-->
<property name="tilesInitializer">
<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer.SpringCompleteAutoloadTilesInitializer"/>
</property>
<property name="definitions">
<list>
<value>/WEB-INF/jsp-ftl/tiles-defs1.xml</value>
</list>
</property>
</bean>
</beans>
jsp-ftl-banner.ftl
jsp-ftl-common_menu.ftl
jsp-ftl-home_body.ftl
jsp-ftl-credits.ftl
left.jsp
<#assign tiles=JspTaglibs["http://tiles.apache.org/tags-tiles"]>
<@tiles.insertDefinition name="myapp.jsp.ftl.homepage" />
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<table>
<tr>
<td colspan="2">
<tiles:insertAttribute name="header1" />
</td>
</tr>
<tr>
<td>
<tiles:insertAttribute name="menu1" />
</td>
<td>
<tiles:insertAttribute name="body1" />
</td>
</tr>
<tr>
<td colspan="2">
<tiles:insertAttribute name="footer1" />
</td>
</tr>
<tr>
<td colspan="2">
<tiles:insertAttribute name="left" />
</td>
</tr>
</table>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<!--注意这里没有继承-->
<definition name="myapp.jsp.ftl.homepage" template="/WEB-INF/jsp-ftl/layout2.jsp" >
<put-attribute name="header1" value="/jsp-ftl-tiles/banner1.ftl" />
<put-attribute name="menu1" value="/jsp-ftl-tiles/common_menu1.ftl" />
<put-attribute name="footer1" value="/jsp-ftl-tiles/credits1.ftl" />
<put-attribute name="body1" value="/jsp-ftl-tiles/home_body1.ftl" />
<put-attribute name="left" value="/jsp-ftl-tiles/left.jsp" />
</definition>
</tiles-definitions>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<!-- FreemarkerServlet settings: -->
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
<property name="tilesInitializer">
<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer.SpringCompleteAutoloadTilesInitializer"/>
</property>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-servlet-ftl.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--layout为ftl时的解决方案,添加以下配置-->
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<!-- FreemarkerServlet settings: -->
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- step 1 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.exercise.controller"/>
<bean id="tileViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles3.TilesView</value>
</property>
<property name="order" value="1"/>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/ftl/tiles-defs4.xml</value>
</list>
</property>
</bean>
<bean id="freemarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="WEB-INF/ftl/" />
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="template_update_delay">0</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
</props>
</property>
</bean>
<!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 -->
<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="requestContextAttribute" value="request" />
<property name="order" value="2"/>
</bean>
</beans>
ftl-banner.ftl
ftl-common_menu.ftl
ftl-home_body.ftl
ftl-credits.ftl
<#assign tiles=JspTaglibs["http://tiles.apache.org/tags-tiles"]>
<@tiles.insertDefinition name="myapp.ftl.homepage" />
<#assign tiles=JspTaglibs["http://tiles.apache.org/tags-tiles"]>
<table>
<tr>
<td colspan="2">
<@tiles.insertAttribute name="header4" />
</td>
</tr>
<tr>
<td>
<@tiles.insertAttribute name="menu4" />
</td>
<td>
<@tiles.insertAttribute name="body4" />
</td>
</tr>
<tr>
<td colspan="2">
<@tiles.insertAttribute name="footer4" />
</td>
</tr>
</table>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="myapp.ftl.homepage" template="/WEB-INF/ftl/layout4.ftl" >
<put-attribute name="header4" value="/ftl-tiles/banner4.ftl" />
<put-attribute name="menu4" value="/ftl-tiles/common_menu4.ftl" />
<put-attribute name="footer4" value="/ftl-tiles/credits4.ftl" />
<put-attribute name="body4" value="/ftl-tiles/home_body4.ftl" />
</definition>
</tiles-definitions>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<!-- FreemarkerServlet settings: -->
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
home_body5.ftl内容为
ftl-home_body5.ftl
<#assign tiles=JspTaglibs["http://tiles.apache.org/tags-tiles"]>
<@tiles.insertDefinition name="myapp.index4.page" />
<#assign tiles=JspTaglibs["http://tiles.apache.org/tags-tiles"]>
<@tiles.insertDefinition name="myapp.index5.page" />
<tiles-definitions>
<definition name="myapp.ftl.homepage" >
<put-attribute name="header4" value="/ftl-tiles/banner4.ftl" />
<put-attribute name="menu4" value="/ftl-tiles/common_menu4.ftl" />
<put-attribute name="footer4" value="/ftl-tiles/credits4.ftl" />
</definition>
<definition name="myapp.index4.page" extends="myapp.ftl.homepage" template="/WEB-INF/ftl/layout4.ftl">
<put-attribute name="body4" value="/ftl-tiles/home_body4.ftl" />
</definition>
<definition name="myapp.index5.page" extends="myapp.ftl.homepage" template="/WEB-INF/ftl/layout4.ftl">
<put-attribute name="body4" value="/ftl-tiles/home_body5.ftl" />
</definition>
</tiles-definitions>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="myapp.ftl.homepage" >
<put-attribute name="header4" value="/ftl-tiles/banner4.ftl" />
<put-attribute name="menu4" value="/ftl-tiles/common_menu4.ftl" />
<put-attribute name="footer4" value="/ftl-tiles/credits4.ftl" />
</definition>
<!--<definition name="myapp.index4.page" extends="myapp.ftl.homepage" template="/WEB-INF/ftl/layout4.ftl">-->
<!--<put-attribute name="body4" value="/ftl-tiles/home_body4.ftl" />-->
<!--</definition>-->
<!--<definition name="myapp.index5.page" extends="myapp.ftl.homepage" template="/WEB-INF/ftl/layout4.ftl">-->
<!--<put-attribute name="body4" value="/ftl-tiles/home_body5.ftl" />-->
<!--</definition>-->
<!--使用下边的配置来代替上边注释掉的配置,注意不同之处-->
<definition name="myapp.index*.page" extends="myapp.ftl.homepage" template="/WEB-INF/ftl/layout4.ftl">
<put-attribute name="body4" value="/ftl-tiles/home_body{1}.ftl" />
</definition>
</tiles-definitions>
@RequestMapping("/index/{num}")
public Object getIndexByNum(@PathVariable("num")String num, ModelMap map){
map.put("num",num);
return "index";
}
<#assign tiles=JspTaglibs["http://tiles.apache.org/tags-tiles"]>
<@tiles.insertDefinition name="myapp.index${num!''}.page" />
通过同一个入口完成页面的切换,要求具有一个统一的命名规范。
web.xml中配置
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<!-- FreemarkerServlet settings: -->
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>DefaultEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>