Have you added a TilesConfigurer to your web application context

spring mvc集成tiles的时候Tiles container is not initialized. Have you added a TilesConfigurer to your web application context

1.spring-MVC.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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-lazy-init="true">
<!-- 自动扫描的包名 -->
<context:annotation-config />
<context:component-scan base-package="com.aliks.test.mvc.*" />

<bean id="filenameController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />

<!--UrlBasedViewResolver类 通过配置文件,把一个视图名交给到一个View来处理 -->
<bean id="jstlViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="2" />
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>

<bean id="tilesResolver"
class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
<property name="order" value="1" />
<property name="viewNames">
<list>
<value>*</value>
</list>
</property>
</bean>

<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>

</beans>
2.tiles.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="index" template="/WEB-INF/layout/layout.jsp" >
<put-attribute name="title" value="hello tiles"></put-attribute>
<put-attribute name="head" value="head"></put-attribute>
<put-attribute name="menu" value="menu"></put-attribute>
<put-attribute name="content" value="/hello.html"></put-attribute>
<put-attribute name="footer" value="footer"></put-attribute>
</definition>
</tiles-definitions>

3.layout.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<title><tiles:getAsString name="title" /></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table
style="margin: auto; border: 1px; border-style: solid; border-color: #ccc; width: 900px;">
<tr style="height: 100px; background-color: #555;">
<td colspan="2">
<%-- <tiles:insertAttribute name="head"></tiles:insertAttribute> --%>
<tiles:getAsString name="head" />
</td>
</tr>
<tr>
<td style="width: 200px; height: 500px; background-color: #666;">
<%-- <tiles:insertAttribute name="menu"></tiles:insertAttribute> --%>
<tiles:getAsString name="menu" />
</td>
<td><tiles:insertAttribute name="content"></tiles:insertAttribute>
</td>
</tr>
<tr style="height: 50px; background-color: #333;">
<td colspan="2">
<%-- <tiles:insertAttribute name="footer"></tiles:insertAttribute> --%>
<tiles:getAsString name="footer" />
</td>
</tr>
</table>
</body>
</html>

4.indexController的代码
package com.aliks.test.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

@RequestMapping("/index")
public String IndexPage(Model model)
{
System.out.println("Tile index test");
return "index";
}
}

以上代码我个根据SpringMVC网络上的教材学着写的,过程中老是报异常,异常如下,找了很多资料都没有解决
六月 27, 2014 5:04:17 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet springMVC threw exception
javax.servlet.ServletException: Tiles container is not initialized. Have you added a TilesConfigurer to your web application context?
at org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:102)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)
at java.lang.Thread.run(Thread.java:744)
解决方法:主要是springMVC-servlet.xml配置问题
default-lazy-init="true"
导致了初始化的时候把Tiles加载到web application context中然到后没使用Tiles实力对象的时候找不到所以报异常了,去除这一行就OK了

你可能感兴趣的:(spring)