首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Apache-Tiles布局中指定唯一的<head>内容。

在Apache-Tiles布局中指定唯一的<head>内容。
EN

Stack Overflow用户
提问于 2013-09-20 18:21:52
回答 1查看 556关注 0票数 1

我刚接触过阿帕奇瓷砖。事实上,我使用它的最大原因是因为它是创建Spring应用程序时的默认呈现器。我知道它有很多追随者,所以我想我可以试一试。

然而,我很困惑如何完成一项简单的任务。我希望从我的布局中向页面的<head>部分添加一些声明。我知道/意识到我可以为头包含创建一个单独的jsp页面,在我的views.xml文件(正如这篇文章所指出的那样)中为主体包含并指定它们,但是这看起来非常笨拙,也不合适。

例如,在一个指定的视图中,我想包含一些额外的JS库。此外,我还需要添加一些我想在页面上运行的jQuery JS脚本。按照惯例,我喜欢让我的<body>远离任何这样的代码,并将所有的JS放在<head>部分。

但是,需要为每个视图创建2个文件(一个head.jsp和一个body.jsp)似乎是不正确的。毕竟,这意味着在编写JS jQuery代码时,我需要打开两个文件(头和体),以查看来自主体的哪些元素正在被操作,等等。

我知道,在Sitemesh,我可以很容易地在同一页内完成这一任务。我也知道Sitemesh和Tiles并不打算做同样的事情,但是我希望有一种比两个单独的文件更容易做到这一点的方法。

是否没有办法从同一个视图(如Sitemesh中)定义不同的区域/包含?唯一的选择是有两个不同的文件-一个为头和一个为身体?在index.jspx页面中,我没有什么可以做到这两件事和防御的事情吗?

目前的设置是:

Default.jspx:

代码语言:javascript
复制
<html xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:tiles="http://tiles.apache.org/tags-tiles" xmlns:spring="http://www.springframework.org/tags" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" >  

    <jsp:output doctype-root-element="HTML" doctype-system="about:legacy-compat" />

    <jsp:directive.page contentType="text/html;charset=UTF-8" />  
    <jsp:directive.page pageEncoding="UTF-8" /> 

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=8" />    

        <util:load-scripts />

        <spring:message code="application_name" var="app_name" htmlEscape="false"/>
        <title><spring:message code="welcome_h3" arguments="${app_name}" /></title>
    </head>

    <body class="tundra spring">
        <div id="wrapper">
            <tiles:insertAttribute name="header" ignore="true" />
            <tiles:insertAttribute name="menu" ignore="true" />   
            <div id="main">
                <tiles:insertAttribute name="body"/> 
                <tiles:insertAttribute name="footer" ignore="true"/>
            </div>
        </div>
    </body>
</html>

Views.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
    <definition extends="default" name="rates/index">
        <put-attribute name="body" value="/WEB-INF/views/rates/index.jspx" />
    </definition>
</tiles-definitions>

Index.jspx:

代码语言:javascript
复制
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page" 
    xmlns:spring="http://www.springframework.org/tags" 
    xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" 
    xmlns:jQWidgets="urn:jsptagdir:/WEB-INF/tags/jQWidgets" 
    version="2.0">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <jsp:output omit-xml-declaration="yes"/>
  <spring:message code="label_rates_index" htmlEscape="false" var="title"/>
  <util:panel id="title" title="${title}">
    <spring:message code="application_name" htmlEscape="false" var="app_name"/>
    <h3>
      <spring:message arguments="${app_name}" code="welcome_titlepane"/>
    </h3>
  </util:panel>

</div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-22 20:21:02

使用Tiles,您将使用复合模式,而不是像使用Sitemesh那样使用装饰器模式。

Tiles功能强大,非常灵活,您可以从string属性或list属性到通配符的数字选项。看一看瓷砖文档。

另一篇介绍Tiles更高级功能的博客文章是在tech.finn.no上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18923377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档