zoukankan      html  css  js  c++  java
  • Spring 实战-第六章-渲染Web视图-6.3使用Apache Tiles视图定义布局

    Apache Tiles是页面框架工具,可以格式化页面结构。

    首选需要在配置中增加Tiles配置,并且声明ViewResolver。

    这里需要使用TilesViewResolver才能解析tiles相关配置。

     TilesConfigurer中的setDefinitions指明了布局文件的路径。

        @Bean
        public ViewResolver viewResolver(){
            TilesViewResolver resolver =new TilesViewResolver();
            return resolver;
        }
    
        @Bean
        public TilesConfigurer tilesConfigurer(){
            TilesConfigurer tiles=new TilesConfigurer();
            tiles.setDefinitions(new String[]{
                    "/WEB-INF/**/tiles.xml"
            });
            tiles.setCheckRefresh(true);
            return tiles;
        }

    tiles.xml标注了要用来渲染页面结构的视图,base节点是基础配置,put-attribute增加了属性并且标注了使用什么视图进行渲染,其他的节点可以扩展base,并新增属性。

    <?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" template="/WEB-INF/layout/page.jsp">
            <put-attribute name="header" value="/WEB-INF/layout/header.jsp"/>
            <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp"/>
        </definition>
    
        <definition name="home" extends="base">
            <put-attribute name="body" value="/WEB-INF/views/home.jsp"/>
        </definition>
    ...
    </tiles-definitions>

    运行的时候,会使用page.jsp作为基础模板,根据tiles.xml配置,使用对应的jsp进行渲染

    <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
    <%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" session="false" %>
    
    <html>
    <head>
        <title>Spittr</title>
        <link rel="stylesheet" type="text/css" href="<s:url value="/resources/style.css"/>"/>
    </head>
    <body>
        <div id="header">
            <t:insertAttribute name="header"/><%--插入头部--%>
        </div>
        <div id="content">
            <t:insertAttribute name="body"/><%--插入body--%>
        </div>
        <div id="footer">
            <t:insertAttribute name="footer"/><%--footer--%>
        </div>
    </body>
    </html>

    当controller收到请求的时候,返回的视图名称,不再是之前对应的WEB-INF/view目录下的视图,而是tiles.xml中对应的definition的名字。

    遗留问题:如何找到对应的布局?

    SourceCode:https://github.com/ljw8947/SpringInAction/tree/master/Capter6/tiles/Spittr

  • 相关阅读:
    我的.emacs配置文件
    C语言夜未眠5——变量前缀代表的含义
    c语言夜未眠4——对某一位或几位进行反转
    指针也可这么玩:返回局部指针变量,局部噢
    lua学习之table类型
    10个最“优秀”的代码注释
    为什么我希望用C而不是C++来实现ZeroMQ
    c语言夜未眠2——实现撤销和重做
    emacs学习笔记(基本概念)
    flutter json_serializable
  • 原文地址:https://www.cnblogs.com/lvjianwei/p/7853149.html
Copyright © 2011-2022 走看看