如何实现“CSS版本控制” (解决缓存问题)使用JSF 2 h:outputStylesheet? [英] How can I implement "CSS versioning" (to solve cache issues) using JSF 2 h:outputStylesheet?
问题描述
我开始使用JSF 2,所以我想尝试h:outputStylesheet。它工作正常,但后来我试图应用模式或添加一个查询字符串到请求,随文件版本更改强制浏览器获取更改的伎俩。
I'm starting to work with JSF 2 so I wanted to give a try to h:outputStylesheet. It works fine but then I tried to apply the "pattern" or trick of adding a query string to the request which changes with the file version to force browsers to fetch changes.
像这里使用的 。
不幸的是我没能做到。实际上,当使用该标签时,它不会生成一个简单的URL,而是一个计算的,已经有一个查询字符串。我在规范和此处找到了有关JSF 2中资源版本控制的一些信息。 / a>,但它似乎指的是一个资源的多个版本,这不是我需要的。
Unfortunately I haven't been able to do it. Actually, when using that tag it doesn't generate a simple URL but a computed one which already has a query string. I've found some info about versioning of resouces in JSF 2 both in the spec and here, but it seems to refer to multiple versions of a resource which is not what I need.
当然,我可以回到不使用新的标签。
Of course I can always go back to NOT use the new tag. But I wanted to share this here for discussion.
更新1 - 例如:
尝试是这样的:
<h:outputStylesheet library="css" name="estilo.css?v=1" target="head"/>
其中呈现为:
<link type="text/css" rel="stylesheet" href="RES_NOT_FOUND" />
非常描述性。 ; - )
Quite descriptive. ;-)
我试图得到的是这样的:
What I try to get is something like this:
<link rel="stylesheet" type="text/css" href="../css/estilo.css?v=1"/>
其中,使用JSP,我习惯这样:
Which, using JSP, I used to put this way:
<link rel="stylesheet" type="text/css"
href="<c:url value='/css/estilo.css?v=${initParam.version}'/>"/>
推荐答案
面对同样的挑战, c $ c> javax.faces.application.ResourceHandlerWrapper和javax.faces.application.ResourceWrapper 将& v = xyz附加到 ResourceWrapper#getRequestString
。
Facing the same challenge, I ended up extending javax.faces.application.ResourceHandlerWrapper and javax.faces.application.ResourceWrapper
to append "&v=x.y.z" to the result of ResourceWrapper#getRequestString()
.
我看到了由Primefaces和Openfaces实现的这种解决方案。
只要看看
I saw this kind of solution implemented by Primefaces and Openfaces. Just take a look at the source of
org.primefaces.application.PrimeResourceHandler#createResource(String resourceName, String libraryName)
和
org.primefaces.application.PrimeResource#getRequestPath()
Available here.
不要忘记将你的实现添加到faces-config.xml:
Don't forget to add your implementation to faces-config.xml:
<application>
<resource-handler>your.package.YourResourceHandlerWrapper</resource-handler>
</application>
这篇关于如何实现“CSS版本控制” (解决缓存问题)使用JSF 2 h:outputStylesheet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!