Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences

Edit this page


Referenced by
Main




JSPWiki v2.2.33

[RSS]


Hide Menu

IncludingScriptsAndCSS


In your tiles-config.xml file, configure your base definition to contain the following putList's:

<!-- Base layout definition -->
<definition name="baseLayout" path="/layouts/baseLayout.jsp">
    ...
    <!-- Default Javascript File -->
    <putList name="scripts">
        <add value="/scripts/global.js"/>
    </putList>
    <!-- Default Stylesheet File -->
    <putList name="styles">
        <add value="/styles/default.css"/>
    </putList>
</definition>

Then in your baseLayout.jsp file, add the following to the

<head>:
<%-- Get Javascript List --%>
<tiles:useAttribute id="scriptList" name="scripts" 
    classname="java.util.List" ignore="true"/>

<c:forEach var="js" items="${scriptList}">
    <script type="text/javascript"
        src="<%=request.getContextPath()%><c:out value="${js}"/>"></script>
</c:forEach>

<%-- Get List of Stylesheets --%>
<tiles:useAttribute id="styleList" name="styles" 
    classname="java.util.List" ignore="true"/>

<c:forEach var="css" items="${styleList}">
    <link rel="stylesheet" type="text/css" media="all" 
        href="<%=request.getContextPath()%><c:out value="${css}"/>" /> 
</c:forEach>

Pretty slick huh? If you want to override the scripts or styles in your child definitions, you will need to override the entire list, rather than just adding to the default. This is a limitation of the putList in Tiles.


Notes: A couple of things could stand to be corrected in this short tutorial.
  • No need for that scriptlet to get the context path. Just use <link rel="stylesheet" type="text/css" href="<html:rewrite page="${css}"/>"/>
This only works if you're using Struts' Expression Language library (html-el). -- MattRaible
Even better you can use JSTL and <link rel="stylesheet" type="text/css" media="all" href="<c:url value="${css}"/>" />. -- Jack
  • When grabbing the variable from the tile you can use the abbreviated <tiles:importAttribute name="scripts" ignore="true"/> and it will do automatic type conversion
I've confirmed that this works, here's an example of a modified version:
<tiles:useAttribute name="styles" ignore="true"/>

<c:forEach var="css" items="${styles}">
    <link rel="stylesheet" type="text/css" media="all" 
        href="<html-el:rewrite page="${css}"/>" /> 
</c:forEach>
~ Mojavelinux


Go to top   Edit this page   More info...   Attach file...
This page last changed on 06-Nov-2006 13:52:58 MST by MattRaible.