从< body>?中调用的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>
的index.jsp:
<%@ 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>
somecontent.tag:
<%@ 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有一些方法,&lt; link&gt;还是一些JSP魔术......想法?
答案 0 :(得分:1)
你可以按照SiteMesh的做法做点什么。
每个requireOnce
标记只会将文件链接到文件列表中,并存储在请求属性中。 servlet过滤器会缓冲整个响应,并在完成后重写它,并重写头部分以包含所有链接。