Spring Web MVC tiles属性错误

时间:2016-03-05 15:49:46

标签: java spring spring-mvc tiles apache-tiles

我在spring web mvc中使用了tile,我收到错误。

<?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="base.definition" template="/template/mainTemplate.jsp">
        <put-attribute name="title" value="Piranha"></put-attribute>
        <put-attribute name="header" value="/template/header.jsp"></put-attribute>
        <put-attribute name="content" value=""></put-attribute>
        <put-attribute name="footer" value="/template/footer.jsp"></put-attribute>
    </definition>


    <definition name="adminHome" extends="base.definition">
        <put-attribute name="content" value="/view/manage/adminHome.jsp"></put-attribute>
        <put-attribute name="maincontent" value=""></put-attribute>
    </definition>
    <definition name="insertProduct" extends="adminHome">
        <put-attribute name="maincontent" value="/view/manage/insertProduct.jsp"></put-attribute>
    </definition>
</tiles-definitions>

AdminHome.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<div align="center">Restricted Area - Administrator</div>
<table>
    <tr>
        <td>
            DATA    
        </td>
        <td>
            <tiles:insertAttribute name="maincontent" />
        </td>
    </tr>
</table>

错误

org.apache.tiles.template.NoSuchAttributeException: Attribute 'maincontent' not found.
    org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:50)
    org.apache.tiles.template.InsertAttributeModel.resolveAttribute(InsertAttributeModel.java:165)
    org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:121)
    org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
    org.apache.jsp.view.manage.adminHome_jsp._jspx_meth_tiles_005finsertAttribute_005f0(adminHome_jsp.java:116)
    org.apache.jsp.view.manage.adminHome_jsp._jspService(adminHome_jsp.java:87)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
    org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
    org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
    org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123)
    org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
    org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
    org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188)
    org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132)
    org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
    org.apache.jsp.template.mainTemplate_jsp._jspx_meth_tiles_005finsertAttribute_005f2(mainTemplate_jsp.java:340)
    org.apache.jsp.template.mainTemplate_jsp._jspService(mainTemplate_jsp.java:130)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265)
    org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228)
    org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57)
    org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:221)
    org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59)
    org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:132)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:96)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

1 个答案:

答案 0 :(得分:1)

您的配置存在一些问题

首先,adminHome正在扩展主要定义。 扩展定义时,必须只具有父定义的属性。您只能覆盖属性而不添加新属性。

所以你需要将maincontent添加到基本定义并相应地调整mainTemplate.jsp

如下所示

<definition name="base.definition" template="/template/mainTemplate.jsp">
   <put-attribute name="title" value="Piranha"></put-attribute>
   <put-attribute name="header" value="/template/header.jsp"></put-attribute>
   <put-attribute name="content" value=""></put-attribute>
   <put-attribute name="maincontent" value=""></put-attribute>
   <put-attribute name="footer" value="/template/footer.jsp"></put-attribute>
</definition>

<definition name="adminHome" extends="base.definition">
    <put-attribute name="content" value="/view/manage/adminHome.jsp"></put-attribute>
    <put-attribute name="maincontent" value="/page/of/your/content"></put-attribute>
</definition>

但是,我不明白你为什么要为一个定义提供两页内容......

使用tiles:insertAttribute在模板中添加maincontent也很重要 AdminHome.jsp不应该包含tile属性,只能包含页面内容。

请向官方documentation查询进一步的帮助