将css文件添加到jsp文件中的元素?

时间:2013-02-09 19:23:27

标签: java jsp servlets

我一直在网上搜索最后一小时,但我看不到如何将css文件添加到页面的<head>元素。

假设我有一个叫做shopping.jsp的jsp。在其中,我有条件地包含一个名为products.tag的jsp标记文件。

我希望products.jsp指定要添加到页面head元素的css文件。怎么办呢?

修改

为了举例,此代码已经过简化。

// 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>

1 个答案:

答案 0 :(得分:1)

根据HTML5,<style> elements可以出现在

  

任何可以包含元数据元素的元素divnoscriptsectionarticleaside

因此,如果您希望页面验证,则无需像<style>那样将<head>元素放在<link>元素内。

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

从正文中加载外部样式表就好了。

如果它在<body>中向下,或者在它之前有任何长时间运行的非延迟/异步<script>元素,那么这些样式可能会比从{{1}加载的样式晚到达}。