org.apache.tiles.template.NoSuchAttributeException: Attribute 'header' not found. ...
對所學知識內容的興趣可能成為學習動機。
在Spring中使用Java配置Tiles 3時出錯:
org.apache.tiles.template.NoSuchAttributeException: Attribute 'header' not found. at org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:50) at org.apache.tiles.template.InsertAttributeModel.resolveAttribute(InsertAttributeModel.java:165) at org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:121) at org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299) at org.apache.jsp.WEB_002dINF.views.page_jsp._jspx_meth_t_005finsertAttribute_005f0(page_jsp.java:142) at org.apache.jsp.WEB_002dINF.views.page_jsp._jspService(page_jsp.java:91) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:743) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:485) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:410) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:337) at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:750) at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:720) at org.apache.jsp.index_jsp._jspService(index_jsp.java:96) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
pom配置如下:
<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.cn.spring.study</groupId> <artifactId>spring-tiles</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.11.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.11.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.11.RELEASE</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-extras --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-extras</artifactId> <version>3.0.8</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-servlet --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-servlet</artifactId> <version>3.0.8</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-api --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-api</artifactId> <version>3.0.8</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>3.0.8</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.1.RELEASE</version> </dependency> </dependencies> </project>
WebConfig類的配置如下:
package com.spring.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.tiles3.TilesConfigurer; import org.springframework.web.servlet.view.tiles3.TilesViewResolver; @Configuration @EnableWebMvc @ComponentScan("com.spring") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public TilesConfigurer tilesConfigurer() { TilesConfigurer tilesConfigurer = new TilesConfigurer(); tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles.xml" }); tilesConfigurer.setCheckRefresh(true); return tilesConfigurer; } @Bean public ViewResolver viewResolver() { return new TilesViewResolver(); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { // TODO Auto-generated method stub configurer.enable(); } }
tiles.xml配置如下:
<?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> <!-- 定義base tile --> <definition name="base" template="/WEB-INF/views/page.jsp"> </definition> <definition name="index" extends="base"> <put-attribute name="header" value="/WEB-INF/views/header.jsp"> </put-attribute> <put-attribute name="body" value="/WEB-INF/views/home.jsp"> </put-attribute> <put-attribute name="footer" value="/WEB-INF/views/footer.jsp"> </put-attribute> </definition> </tiles-definitions>
page.jsp配置如下:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %> <%@ page session="false" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Apache Tiles Template</title> </head> <body> <div id="header"> <t:insertAttribute name="header" /> </div> <div id="content"> <t:insertAttribute name="body" /> </div> <div id="footer"> <t:insertAttribute name="footer" /> </div> </body> </html>
header.jsp配置如下:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> <h5>header</h5>
home.jsp配置如下:
<%@ taglib uri="http://www.springframework.org/tags" prefix="c" %> <%@ page session="false" %> <h1>Hello tiles!</h1>
footer.jsp配置如下:
Copyright ©
目錄結構如下:
部署代碼至Tomcat,啟動時並無任何錯誤,啟動日誌如下:
五月 22, 2018 10:08:21 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Java\jdk1.8.0_161\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Java/jdk1.8.0_161/bin/../jre/bin/server;C:/Java/jdk1.8.0_161/bin/../jre/bin;C:/Java/jdk1.8.0_161/bin/../jre/lib/amd64;.;C:\Java\jdk1.8.0_161\bin;C:\Java\jdk1.8.0_161\jre\bin;D:\apache-maven-3.5.3\bin;;C:\Users\Administrator\Desktop;;. 五月 22, 2018 10:08:21 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-8080"] 五月 22, 2018 10:08:21 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-bio-8009"] 五月 22, 2018 10:08:21 下午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 973 ms 五月 22, 2018 10:08:21 下午 org.apache.catalina.core.StandardService startInternal 信息: Starting service Catalina 五月 22, 2018 10:08:21 下午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/7.0.81 五月 22, 2018 10:08:21 下午 org.apache.catalina.startup.HostConfig deployDescriptor 信息: Deploying configuration descriptor F:\apache-tomcat-7.0.81\conf\Catalina\localhost\spring-tiles.xml 五月 22, 2018 10:08:21 下午 org.apache.catalina.startup.SetContextPropertiesRule begin 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:spring-tiles' did not find a matching property. 五月 22, 2018 10:08:28 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 五月 22, 2018 10:08:28 下午 org.apache.catalina.core.ApplicationContext log 信息: Spring WebApplicationInitializers detected on classpath: [com.spring.config.SpringTilesWebAppInitialzer@1b6aca02] 五月 22, 2018 10:08:28 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext 五月 22, 2018 10:08:30 下午 org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom 警告: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [151] milliseconds. 五月 22, 2018 10:08:30 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring FrameworkServlet 'dispatcher' 五月 22, 2018 10:08:30 下午 org.apache.catalina.startup.HostConfig deployDescriptor 信息: Deployment of configuration descriptor F:\apache-tomcat-7.0.81\conf\Catalina\localhost\spring-tiles.xml has finished in 8,730 ms 五月 22, 2018 10:08:30 下午 org.apache.catalina.startup.HostConfig deployDescriptor 信息: Deploying configuration descriptor F:\apache-tomcat-7.0.81\conf\Catalina\localhost\src#WEB-INF.xml 五月 22, 2018 10:08:30 下午 org.apache.catalina.startup.SetContextPropertiesRule begin 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:spring-tiles' did not find a matching property. 五月 22, 2018 10:08:33 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 五月 22, 2018 10:08:33 下午 org.apache.catalina.core.ApplicationContext log 信息: Spring WebApplicationInitializers detected on classpath: [com.spring.config.SpringTilesWebAppInitialzer@2b7f5883] 五月 22, 2018 10:08:33 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext 五月 22, 2018 10:08:34 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring FrameworkServlet 'dispatcher' 五月 22, 2018 10:08:34 下午 org.apache.catalina.startup.HostConfig deployDescriptor 信息: Deployment of configuration descriptor F:\apache-tomcat-7.0.81\conf\Catalina\localhost\src#WEB-INF.xml has finished in 3,683 ms 五月 22, 2018 10:08:34 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory F:\apache-tomcat-7.0.81\webapps\docs 五月 22, 2018 10:08:35 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 五月 22, 2018 10:08:35 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory F:\apache-tomcat-7.0.81\webapps\docs has finished in 1,210 ms 五月 22, 2018 10:08:35 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory F:\apache-tomcat-7.0.81\webapps\examples 五月 22, 2018 10:08:37 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 五月 22, 2018 10:08:37 下午 org.apache.catalina.core.ApplicationContext log 信息: ContextListener: contextInitialized() 五月 22, 2018 10:08:37 下午 org.apache.catalina.core.ApplicationContext log 信息: SessionListener: contextInitialized() 五月 22, 2018 10:08:37 下午 org.apache.catalina.core.ApplicationContext log 信息: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@31567986') 五月 22, 2018 10:08:37 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory F:\apache-tomcat-7.0.81\webapps\examples has finished in 2,395 ms 五月 22, 2018 10:08:37 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory F:\apache-tomcat-7.0.81\webapps\host-manager 五月 22, 2018 10:08:39 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 五月 22, 2018 10:08:39 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory F:\apache-tomcat-7.0.81\webapps\host-manager has finished in 1,192 ms 五月 22, 2018 10:08:39 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory F:\apache-tomcat-7.0.81\webapps\manager 五月 22, 2018 10:08:40 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 五月 22, 2018 10:08:40 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory F:\apache-tomcat-7.0.81\webapps\manager has finished in 976 ms 五月 22, 2018 10:08:40 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory F:\apache-tomcat-7.0.81\webapps\ROOT 五月 22, 2018 10:08:40 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 五月 22, 2018 10:08:40 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory F:\apache-tomcat-7.0.81\webapps\ROOT has finished in 876 ms 五月 22, 2018 10:08:40 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-bio-8080"] 五月 22, 2018 10:08:41 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-bio-8009"] 五月 22, 2018 10:08:41 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 19246 ms
本地訪問時home.jsp時,好像header和footer沒有任何作用,如下:
本次訪問page.jsp時,報錯:org.apache.tiles.template.NoSuchAttributeException: Attribute 'header' not found.