sonicboom
sonicboom

Reputation: 5028

Adding a css file to the <head> element in a jsp file?

I have been searching the net for the last hour but I can't see how to add a css file to the <head> element of a page.

Say I have a jsp called shopping.jsp. And inside it I conditionally include a jsp tag file called products.tag.

I want products.jsp to specify a css file to add to the head element of the page. How can this be done?

EDIT

This code is simplified for the sake of example.

// shopping.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<t:standard_page>
    <jsp:body>
        <t:products product="${product}"/>
    </jsp:body>
</t:standard_page>


// products.tag
// I want to specify a css file in this tag file that will get added to the <head> element of the webpage
<%@tag description="Template for products on the shopping cart page" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@tag import="com.myapp.Product" %>
<%@attribute name="product" required="true" type="com.myapp.Product"%>

<div class="shopping-cart-row">
    <div class="product-name">${product.name}</div>
    <div class="product-quantity">${product.quantity}</div>
</div>

Upvotes: 0

Views: 2188

Answers (1)

Mike Samuel
Mike Samuel

Reputation: 120506

According to HTML5, <style> elements can appear inside

Any element that can contain metadata elements, div, noscript, section, article, aside

so you need not put <style> elements inside the <head> the way you do with <link> elements if you want your page to validate.

<style type="text/css">@import url("/path/to/stylesheet.css");</style>

from within the body will load the external stylesheet just fine.

If it's way down in the <body> or there are any long running non-deferred/async <script> elements before it, then those styles may arrive noticably later than styles loaded from the <head>.

Upvotes: 1

Related Questions