2015-06-26 21 views
1

我试图用apache瓦片建立一个web应用程序。 我使用apache瓦片V3与通配符支持。Apache瓦片V3包含在子页面

我tiles.xml看起来就象这样:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!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="*" template="/resources/tiles/main-layout.jsp"> 
     <put-attribute name="main" value="/resources/templates/{1}.jsp" /> 
     <put-attribute name="head-content" value="" /> 
    </definition> 
    <definition name="*/*" template="/resources/tiles/main-layout.jsp"> 
     <put-attribute name="main" value="/resources/templates/{1}/{2}.jsp" /> 
     <put-attribute name="head-content" value="" /> 
    </definition> 
    <definition name="*/*/*" template="/resources/tiles/main-layout.jsp"> 
     <put-attribute name="main" value="/resources/templates/{1}/{2}/{3}.jsp" /> 
     <put-attribute name="head-content" value="" /> 
    </definition> 
</tiles-definitions> 

我有一个主页(主layout.jsp)与头部分和内容部分。

头部分:

<head> 
    <link rel="stylesheet" type="text/css" href="${baseCss}" /> 
    <link rel="stylesheet" type="text/css" href="${messages}" /> 
    <script src="${jquery}"></script> 
    <script src="${jqueryui}"></script> 
    <script src="${jquerycookie}"></script> 
    <script src="${languagetoggle}"></script> 
    <script src="${menuJs}"></script> 
    <tiles:insertAttribute name="head-content" /> 
</head> 

主要部分:

<section class="main-content"> 
    <tiles:insertAttribute name="main" /> 
</section> 

我的主要部分呈现正确。我可以使用Spring MVC从/hello/world加载文件world.jsp并从de hello文件夹加载。

不,我想添加一些额外的CSS文件头。 我的问题是:我怎么能从de world.jsp文件做到这一点?

我已经尝试从world.jsp文件中添加砖属性,并加载它:

<tiles:putAttribute name="head-content"> 
    <spring:url value="/resources/base-theme/css/tables.css" var="tableCss" /> 
    <link rel="stylesheet" type="text/css" href="${tableCss}"> 
</tiles:putAttribute> 

但它不工作。当我谷歌我总是在相同的页面上,在每个页面得到指定tiles.xml,但从瓦片V3通配符支持它不再需要。有人能给我一个提示如何完成它吗?

回答

1

请阅读下面的代码以了解您的理解。 我已经创建了一个全局的cssjs文件的默认瓷砖定义。 yourpage.jsp扩展了这个定义,两个文件添加到它:yourpage.jsyourpage.css

tiles.xml

<tiles-definitions> 
    <definition name="app.base" template="/path/to/your/layout.jsp"> 
     <put-attribute name="title" value="Not Found" /> 
     <put-attribute name="header" value="/WEB-INF/tiles/header.jsp" /> 
     <put-attribute name="body" value="/WEB-INF/tiles/body.jsp" /> 
     <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp" /> 
     <put-list-attribute name="stylesheets"> 
      <add-attribute value="/static/resources/css/bootstrap.min.css" />   
      <add-attribute value="/static/resources/css/global.css" /> 
     </put-list-attribute> 
     <put-list-attribute name="javascripts"> 
      <add-attribute value="/static/resources/js/jquery-2.1.4.min.js" /> 
      <add-attribute value="/static/resources/js/global.js" /> 
     </put-list-attribute> 
    </definition> 
    <definition name="yourpage" extends="app.base"> 
     <put-attribute name="title" value="Your Page" /> 
     <put-attribute name="body" value="/path/to/your/yourpage.jsp" /> 
     <put-list-attribute name="stylesheets" inherit="true"> 
      <add-attribute value="/static/resources/css/yourpage.css" /> 
     </put-list-attribute> 
     <put-list-attribute name="javascripts" inherit="true"> 
      <add-attribute value="/static/resources/js/yourpage.js" /> 
     </put-list-attribute> 
    </definition> 
</tiles-definitions> 

tiles.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%> 
<tiles:importAttribute name="stylesheets"/> 
<tiles:importAttribute name="javascripts"/> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title> 
     <tiles:insertAttribute name="title"> 
     </tiles:insertAttribute> 
    </title> 

    <!-- stylesheets--> 
    <c:forEach var="css" items="${stylesheets}"> 
     <link rel="stylesheet" type="text/css" href="<c:url value="${css}"/>"> 
    </c:forEach> 
</head> 

<body> 
    <header> 
     <tiles:insertAttribute name="header" /> 
    </header> 
    <div class="body"> 
     <tiles:insertAttribute name="body" /> 
    </div> 
    <footer> 
     <tiles:insertAttribute name="footer" /> 
    </footer> 

    <!-- scripts--> 
    <c:forEach var="script" items="${javascripts}"> 
     <script src="<c:url value="${script}"/>"></script> 
    </c:forEach> 
</body> 
</html> 

希望这可能是帮助