i'm dynamically generating css and js using js

2019-04-13 04:32发布

问题:

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>

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>

But what i need is

<head>
<link rel="stylesheet" href="/css/mycss.css" type="text/css">
</head>

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.

回答1:

I achieved this.

I created a new route /resource/css|js , I use internal resource view resolver - (folder).

My link url is moething like /resource/css/qtn/page1.css.

In controller /resources/{type}/{page}.css.

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.

Hint :

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.

To solve this i passed page values as /resource/css/qtn/page1.css?sub=${page}.

In controller we can easily cath this sub value and send to our dynamic jsp as model.