Specifying unique content for <head> section using apache tiles

跟風遠走 提交于 2019-12-24 23:43:56

问题


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'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).

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>

Any help would be greatly appreciated.

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

<%@ 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>

回答1:


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>

And add this to you layout.jsp:

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


来源:https://stackoverflow.com/questions/16512224/specifying-unique-content-for-head-section-using-apache-tiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!