美文网首页
Spring + Tiles

Spring + Tiles

作者: Draper | 来源:发表于2018-11-01 13:41 被阅读0次

    Introduction

    a free open-sourced templating framework for modern Java applications.
    Base upon the Composite pattern it is built to simplify the development of user of user interfaces.

    For complex websites, it remains the easiest and most elegant way to work alongside any MVC technology.

    一项免费开源的用于现代 Java 应用模板框架。基于可重复使用的组件,用来简化开发用户界面。
    对于复杂的网站,它仍然是较为容易优雅的方式与 MVC 技术相结合

    Tiles allows authors to define page fragments which can be assembled into a complete pages at runtime. These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates. These templates streamline the development of a consistent look and feel across an entire application.

    Tiles 允许用户在运行时定义可被组合成完整的页面fragment(碎片),或 tiles 可以被用来当做简单的组件而减少重复性的公共页面元素嵌入到其他碎片从而开发出一系列可重用的组件,这些模板简化了应用外观一致性的开发。

    Tiles grew in popularity as a component of the popular Struts framework. It was extracted from Struts and is now integrated with various frameworks, such as Spring and Struts 2.

    Tiles 作为流行框架 Struts 框架的一个组件而得到流行,并在其他各种各样的框架中得到拓展。如 Spring, Struts2

    Configure Tiles

    Plain JSP

    <html …> <body>
     <div style="padding-top: 50px;">
       <jsp:include page="../menu.jspx"/>
       <c:choose>
         <c:when test="${empty users}">
           Table is empty.
         </c:when>
         <c:otherwise>
          <table>
           <thead>
             <tr>
              <th> First Name </th>
              <th> Last name </th>
             </tr>
            </thead>
            <tbody>
            <c:forEach var="user" items="${users}">
            <tr>
              <td> <c:out value="${user.firstName}"/> </td>
              <td> <c:out value="${user.lastName}"/> </td>
            </tr>
            </c:forEach>
           </tbody>
         </table>
        </c:otherwise>
       </c:choose>
       <jsp:include page="../footer.jspx"/>
      </div>
     </body>
    </html
    
    1. The layout
      我们可以使用 <jsp:include/> 来导入 JSP fragment 如(header/footer)

    2. Verbosity
      我们的用户界面哦通常是简单的显示一个 list 元素。
      我们准备了 50 行代码,你可以想想一想多么大的,我们是否会显示这些内容。

    3. HTML/CSS compliance
      这些页面并不合乎 HTML/CSS 规范。想要 Web designer 来遵守它,那你将会重写大量代码来使 JSP 达到一致。

    Externalizing your JSPs’ layout using Apache Tiles

    <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
     <property name="definitions">
     <list>
     <value>/WEB-INF/view/jsp/tiles.xml</value>
     </list>
     </property>
    </bean>
    

    Spring configure file

    因为使用 Spring MVC + Tiles,所以不可避免要在 Spring 配置文件中配置

    • tilesConfigurer Tiles 的布局文件,以及涉及到新的页面的设置
    • tilesViewResolver Spring dispatcher 和 tiles 的连接
        <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
            <property name="order" value="1"/>
        </bean>
    
        <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>WEB-INF/layout.xml</value>
                </list>
            </property>
        </bean>
    

    template file

    模板文件就是指在一个模板中使用 tiles 做标记,给未来在这些标记的点插入组件使用的

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
    <html>
    <head>
        <tiles:insertAttribute name="head"/>
    </head>
    
    <body>
        <tiles:insertAttribute name="header"/>
        <tiles:insertAttribute name="body"/>
        <tiles:insertAttribute name="footer"/>
    </body>
    
        <tiles:insertAttribute name="script"/>
    </html>
    

    layout file

    提前准备好各种各样的 fragment 到时候插入模板中组装成一个新的页面 mainLayout,而 Spring 的 Tiles Resolver 将会自动查找解析

    <?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="mainLayout" template="/WEB-INF/jsp/mainLayout.jsp">
            <put-attribute name="head" value="/WEB-INF/jsp/component/head.jsp"/>
            <put-attribute name="header" value="/WEB-INF/jsp/component/header.jsp"/>
            <put-attribute name="body" value="/WEB-INF/jsp/indexBody.jsp"/>
            <put-attribute name="footer" value="/WEB-INF/jsp/component/footer.jsp"/>
            <put-attribute name="script" value="/WEB-INF/jsp/component/script.jsp"/>
        </definition>
    </tiles-definitions>
    

    相关文章

      网友评论

          本文标题:Spring + Tiles

          本文链接:https://www.haomeiwen.com/subject/fzitxqtx.html