Html 在JSP标记中导入CSS

Html 在JSP标记中导入CSS,html,css,jsp,jsp-tags,Html,Css,Jsp,Jsp Tags,从中调用的JSP标记链接或导入样式表有哪些方法(如果有的话)?如果我能在JSP标记中封装所有必要的导入,那就太好了 现状 index.jsp: <%@ page contentType="text/html; charset=utf-8" %> <%@ taglib prefix="x" tagdir="/WEB-INF/tags" %> <html> <head> <!-- we have to know what

从中调用的JSP标记链接或导入样式表有哪些方法(如果有的话)?如果我能在JSP标记中封装所有必要的导入,那就太好了

现状 index.jsp:

<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
    <head>
        <!-- we have to know what css file somecontent uses and include it here -->
        <!-- the tag below prints <link rel="/somecontent.css"... /> but makes sure this url is only included once -->
        <x:requireOnce loc="/somecontent.css" type="css" />
    </head>
    <body>
        <x:somecontent />
    </body>
</html>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
    <head>
        <%-- nothing for somecontent tag here --%>
    </head>
    <body>
        <x:somecontent />
        ...
    </body>
</html>
<%@ tag description="some independent content" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<%-- the inline attribute will indicate that it is in the body and shouldn't use <link> which won't work here --%>
<x:requireOnce loc="/somecontent.css" type="css" inline="true" />
<%-- this will print <script type="text/javascript" src="/somecontent.js" ...></script> --%>
<x:requireOnce loc="/somecontent.js" type="js" />
...

目标 index.jsp:

<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
    <head>
        <!-- we have to know what css file somecontent uses and include it here -->
        <!-- the tag below prints <link rel="/somecontent.css"... /> but makes sure this url is only included once -->
        <x:requireOnce loc="/somecontent.css" type="css" />
    </head>
    <body>
        <x:somecontent />
    </body>
</html>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
    <head>
        <%-- nothing for somecontent tag here --%>
    </head>
    <body>
        <x:somecontent />
        ...
    </body>
</html>
<%@ tag description="some independent content" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<%-- the inline attribute will indicate that it is in the body and shouldn't use <link> which won't work here --%>
<x:requireOnce loc="/somecontent.css" type="css" inline="true" />
<%-- this will print <script type="text/javascript" src="/somecontent.js" ...></script> --%>
<x:requireOnce loc="/somecontent.js" type="js" />
...

...
somecontent.tag:

<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
    <head>
        <!-- we have to know what css file somecontent uses and include it here -->
        <!-- the tag below prints <link rel="/somecontent.css"... /> but makes sure this url is only included once -->
        <x:requireOnce loc="/somecontent.css" type="css" />
    </head>
    <body>
        <x:somecontent />
    </body>
</html>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<html>
    <head>
        <%-- nothing for somecontent tag here --%>
    </head>
    <body>
        <x:somecontent />
        ...
    </body>
</html>
<%@ tag description="some independent content" %>
<%@ taglib prefix="x" tagdir="/WEB-INF/tags" %>
<%-- the inline attribute will indicate that it is in the body and shouldn't use <link> which won't work here --%>
<x:requireOnce loc="/somecontent.css" type="css" inline="true" />
<%-- this will print <script type="text/javascript" src="/somecontent.js" ...></script> --%>
<x:requireOnce loc="/somecontent.js" type="js" />
...

...
是否有办法在JspWriter中保留对head标记位置的引用,并在必要时插入内容,即新的链接标记


理想情况下,我不想内联样式表的内容或使用javascript包含样式表。希望有@import的方法,或者一些JSP魔法。。。想法?

您可以按照SiteMesh的做法做一些事情


每个
requirence
标记只会将要链接的文件放在文件列表中,存储在请求属性中。servlet过滤器将缓冲整个响应,并在完成时重写它,其中头部分将重写为包含所有链接。

您基本上是在重新设计一些基于组件的MVC框架中已经存在的内容,例如JSF,它有一个用于此目的的标记(以及JS)。我建议你看看,而不是重新发明轮子。谢谢你,巴卢斯。我不确定我们是否可以切换到JSF。你知道仅仅使用JSP和SpringMVC做同样的事情的方法吗?一切都是可能的。对于某些情况,您只需要自己编写比其他情况更多的代码。很抱歉,我不能给出详细的答案,因为我对JSP标签和SpringMVC不太了解。谢谢JB,这听起来是可行的。这可能就是我最终要做的。我同意这个解决方案。不幸的是,为了在没有JSF的情况下重新发明这个轮子,我不得不编写比预期更多的代码,但这似乎是唯一的方法。