为< head>指定唯一的内容使用apache磁贴的部分 [英] Specifying unique content for <head> section using apache tiles

查看:109
本文介绍了为< head>指定唯一的内容使用apache磁贴的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Tiles的新手,所以这可能是一个非常简单的问题。我已经使用Java / Spring 3 / JSP创建了一个不使用图块的Web应用程序,现在我意识到有了它,我的生活会变得更加轻松。

I'm brand new to Tiles so this is probably a very easy question. I've created a web application using Java / Spring 3 / JSPs without the use of tiles and now I'm realizing my life would be much easier with it.

I到目前为止,在一个方面还是有些困惑,那就是定义布局。我的问题是,在网上看到的示例中,您定义了一个layout.jsp,其中包含信息(包括标签和标签)。

I'm a bit confused on one aspect thus far, and that's defining the layouts. My problem is that in examples I've seen online you define a layout.jsp which contains the information (including tags and tags).

我的问题是,布局 .jsp文件应该是通用的一个大小适合所有的文件类型,但是当我创建另一个应该使用布局模板的文件(例如welcome.jsp)时会发生什么,但是我需要定义更多和标签...如果我在welcome.jsp文件中定义它们,则呈现的JSP文件格式不正确...即:

My question is, the "layout.jsp" file is supposed to be the generic "one-size-fits-all" type of file, but what happens when I create another file (say welcome.jsp) which should USE the layout template, but I need to define more and tags... if I define them inside of the welcome.jsp file then the rendered JSP file is not formatted correctly... i.e:

<head>
  <!-- This is content from the layout.jsp file -->
  <title>Welcome</title>
  <link rel="shortcut icon" href="images/favicon.ico"/>
  <script type="text/javascript">
     // some javascript
  </script>
</head>

<body>
  <!-- This is content from the welcome.jsp file which is malformed -->
  <head>
    <script src="js/jquery.mousewheel.min.js"></script>
  </head>
</body>

任何帮助将不胜感激。

这是我的tile.xml:

Here's my tiles.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
  <definition name="baseLayout" template="/WEB-INF/jsp/layout/layout.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/WEB-INF/jsp/layout/header.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/WEB-INF/jsp/layout/footer.jsp" />
  </definition>

  <definition name="videos" extends="baseLayout">
    <put-attribute name="title" value="Videos" />
    <put-attribute name="body" value="/WEB-INF/jsp/videos.jsp" />
  </definition>

</tiles-definitions>

layout.jsp

layout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ include file="../tracking.jsp" %>
<!DOCTYPE HTML>
<html>
<head>
  <title><tiles:insertAttribute name="title" ignore="true" /></title>

  <link rel="shortcut icon" href="images/favicon.ico"/>

  <link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
  <link rel="stylesheet" type="text/css" href="css/jquery-ui-1.10.css" media="screen" />

  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/jquery.lightbox-0.5.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
</head>
<body>
  <tiles:insertAttribute name="header" />
  <tiles:insertAttribute name="body" />
  <tiles:insertAttribute name="footer" />
</body>
</html>


推荐答案

创建新定义以更改标题内容:

Create a new definition to change the header content:

<definition name="baseLayout" template="/WEB-INF/jsp/layout/layout.jsp">
    <!-- Defined here -->
    <put-attribute name="head" value="" />
    <put-attribute name="title" value="" />
    ...
</definition>

<definition name="videos" extends="baseLayout">
    <!-- Overridden here -->
    <put-attribute name="head" value="/WEB-INF/jsp/videos-additionalHeadDefinitions.jsp" />
    ...
</definition>

并将其添加到您的layout.jsp中:

And add this to you layout.jsp:

<head>
  <!-- This is content from the layout.jsp file -->
  <title>Welcome</title>
  ...
 <tiles:insertAttribute name="head" />
</head>

这篇关于为&lt; head&gt;指定唯一的内容使用apache磁贴的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆