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了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章