使用 Tiles,有没有办法将我的两个 JSP 减少为一个 JSP?

With Tiles, is there a way to reduce my two JSPs into one JSP?

我是第一次使用 Tiles。尝试在我的 Spring MVC 项目中布置我的 JSP 页面。

请看看我在做什么,让我知道这是否是 "correct" 的方法...我的具体问题在底部...

我的文件结构如下...文件夹 /src/main/webapp/WEB-INF/layouts/ 包含 ...

standard.jsp

// ...
<html>
    <title><tiles:insertAttribute name="title"/></title>
// ... LOTS OF HTML ...
    <tiles:insertAttribute name="header"/>
    <tiles:insertAttribute name="body"/>
    <tiles:insertAttribute name="footer"/>
// ... LOTS OF HTML
</html>
// ...

tiles.xml

<tiles-definitions>
    <definition name="standardLayout" template="/WEB-INF/layouts/standard.jsp">
        <put-attribute name="title" value="My Directory" />
        <put-attribute name="header" value="/WEB-INF/layouts/header.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="" />
    </definition>
</tiles-definitions>

我的可见 JSP 位于文件夹 /src/main/webapp/WEB-INF/views/ 中,其中包含...

tiles.xml

<tiles-definitions>
    <definition name="home" extends="standardLayout">
    </definition>
</tiles-definitions>

home.jsp

<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>

<tiles:insertDefinition name="home">
    <tiles:putAttribute name = "body" value="/WEB-INF/views/home-body.jsp"/>
</tiles:insertDefinition>

首页-body.jsp

<div>
// LOTS OF HTML FOR THE BODY OF THE HOME PAGE
</div>

问题:我只想要一个 home.jsp 但看起来我必须从一个单独的文件中提取正文内容(以及任何其他内容)。有没有一种适当的方法可以将 home.jsphome-body.jsp 简化为一个文件,或者我实际上做的是否正确?

您需要更新 tiles.xml、standard.jsp 和 home.jsp,如下所示,

tiles.xml

<tiles-definitions>
    <definition name="home" extends="standardLayout">
       <put-attribute name="body" value="/WEB-INF/views/home.jsp"></put-attribute>
    </definition>        
</tiles-definitions>

standard.jsp

 <html>
   <title><tiles:insertAttribute name="title"/></title>
   <header>
       <tiles:insertAttribute name="header"/>
   </header>
   <body>
       <tiles:insertAttribute name="body"/>
   </body>
   <footer>
       <tiles:insertAttribute name="footer"/>
   </footer>
 </html>

home.jsp

<!-- Actual body content goes here.. -->    
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>    
<p>The content of the home page.</p>