1.Spring-mvc.xml 添加扫面路径和视图解析器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 11 12 <!-- 注解扫描包 --> 13 <context:component-scan base-package="com" /> 14 15 <!-- 开启注解 --> 16 <mvc:annotation-driven /> 17 18 <!-- 19 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd 20 --> 21 <mvc:resources mapping="/img/**" location="/img/" /> 22 <mvc:resources mapping="/js/**" location="/js/" /> 23 <mvc:resources mapping="/css/**" location="/css/" /> 24 <mvc:resources mapping="/html/**" location="/html/" /> 25 26 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 27 <property name="viewClass"> 28 <value> 29 org.springframework.web.servlet.view.tiles2.TilesView 30 </value> 31 </property> 32 </bean> 33 <bean id="tilesConfigurer" 34 class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 35 <property name="definitions"> 36 <list> 37 <value>/WEB-INF/tiles.xml</value> 38 </list> 39 </property> 40 </bean> 41 </beans>
可以定义视图解析器顺序,通过order属性
1 <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> 2 <property name="definitions"> 3 <list> 4 <value>/WEB-INF/tiles/general.xml</value> 5 </list> 6 </property> 7 </bean> 8 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 9 <property name="order" value="1" /> 10 <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"></property> 11 </bean> 12 <bean 13 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/WEB-INF/views/" /> 15 <property name="suffix" value=".jsp" /> 16 <property name="order" value="2" /> 17 </bean>
2.定义tiles.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE tiles-definitions PUBLIC 3 "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" 4 "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> 5 6 <tiles-definitions> 7 <definition name="index" 8 template="/WEB-INF/jsp/index.jsp"> 9 <put-attribute name="title" value="房产资产后台管理" /> 10 <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" /> 11 <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" /> 12 <put-attribute name="content" value="/WEB-INF/jsp/content.jsp" /> 13 </definition> 14 </tiles-definitions>
3.index.jsp
调用tiles模板
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 <title><tiles:insertAttribute name="title"/></title> 13 <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" /> 14 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.2.min.js"></script> 15 </head> 16 <body> 17 18 <tiles:insertAttribute name="header" /> 19 <div style="position: absolute;margin-top: 88px"> 20 <tiles:insertAttribute name="menu" /> 21 </div> 22 <div style="position: absolute;margin-left:187px; margin-top: 88px"> 23 <tiles:insertAttribute name="content" /> 24 </div> 25 26 </body> 27 </html>