有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java向jsp文件中的<head>元素添加css文件?

我在网上搜索了一个小时,但我不知道如何将css文件添加到页面的<head>元素中

假设我有一个叫做购物的jsp。jsp。在它里面,我有条件地包含一个名为products的jsp标记文件。标签

我想要产品。jsp指定要添加到页面头部元素的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) 个答案

  1. # 1 楼答案

    根据HTML5,^{} elements可以出现在内部

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

    因此,如果希望页面进行验证,则无需像处理<link>元素那样,将<style>元素放入<head>元素中

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

    从主体内部加载外部样式表就可以了

    如果它在<body>中处于低位,或者之前有任何长时间运行的非延迟/异步<script>元素,那么这些样式可能会明显晚于从<head>加载的样式