我正在使用jsp/jstl/spring动态生成css和js.如何将此结果放置在头部的链接标签中.不在头部的脚本标签中 [英] i'm dynamically generating css and js using jsp/jstl/spring. How to place this result in link tag at head section. not in script tag in head section
问题描述
我在jsp中生成CSS,输出为:
I'm generating css in jsp and the output is:
<style type="text/css">
body{background: #ffffff url('<c:url value='/resources/images/logo/logo_small.png'/>') no-repeat scroll center center}
</style>
现在,我使用以下命令在jsp中调用此代码:
Now i'm call this code in jsp using:
<%@include file="/WEB-INF/common/css/sitemap_common.jsp" %>
<%@include file="/WEB-INF/common/css/index.jsp" %>
它可以正常工作,并且输出是这样的:
It works and the out put is something like this:
<head>
<style type="text/css">
body{background: #ffffff url('<c:url value='/resources/images/logo/logo_small.png'/>') no-repeat scroll center center}
</style>
</head>
但是我需要的是
<head>
<link rel="stylesheet" href="/css/mycss.css" type="text/css">
</head>
THis是一个已完成的项目,具有100的css生成jsp.我需要更新以获得更好的性能.链接标记将在浏览器中被捕获.但是脚本标签会每次加载.
THis is a completed project with 100's of css generating jsp. and i need to update for better performance. link tag will be catched in browser. but script tag will load every time.
推荐答案
我实现了这一目标.
我创建了一个新路由/resource/css | js,我使用了内部资源视图解析器-(文件夹).
I created a new route /resource/css|js , I use internal resource view resolver - (folder).
我的链接网址像/resource/css/qtn/page1.css 一样.
My link url is moething like /resource/css/qtn/page1.css.
在控制器/resources/{类型}/{页面}.css中.
In controller /resources/{type}/{page}.css.
在这里,我获得了 type 和 page 值,并在我的 dynamic jsp 页面中使用了该值以实现结果.
Here i get type and page values and used this in my dynamic jsp page to achieve my result.
为了安全起见,我检查了网址,它是否来自我自己的页面.
For security i checked the url is it comes from my own page or not.
提示:
在使用内部资源视图解析器时,当您将页面值直接传递到资源url时,它将抛出错误,因为有时页面URL将是/page1/block1 ,因此我们的控制器会引发错误.
While using internal resource view resolver it will throw error when you pass page values directly to resource url because sometimes page url will be /page1/block1 so our controller throws error.
为解决此问题,我将页面值传递为/resource/css/qtn/page1.css?sub = $ {page} .
To solve this i passed page values as /resource/css/qtn/page1.css?sub=${page}.
在控制器中,我们可以轻松获取此 sub 值并将其作为 model 发送到我们的动态jsp.
In controller we can easily cath this sub value and send to our dynamic jsp as model.
这篇关于我正在使用jsp/jstl/spring动态生成css和js.如何将此结果放置在头部的链接标签中.不在头部的脚本标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!