根据问答 这里,我想为在 JBoss 5 中运行的 Web 应用程序实现一个类似的自动版本控制系统。是否已经有任何东西可以做这类事情,或者我需要自己写一些东西?需要明确的是:我没有使用 PHP。
对 PHP 不太了解,我不确定 PHP 的 .htaccess
的 Tomcat/JBoss 类似物是什么。如果我必须编写自己的自动版本控制,我应该从哪里开始?原理对我来说很清楚 - 使用文件的时间戳重写 URL,但我对 JBoss/Tomcat 的 URL 重写不太了解。
更新:
结合 Pascal 和 < a href="https://stackoverflow.com/questions/2630885/auto-versioning-of-static-content-with-jboss/3719518#3719518">新手,这就是我最终得到的结果:
1. 自定义
和
标签,这样我就不必看到 < code> 标签随处可见。
<%@ tag body-content="empty" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="src" required="true" rtexprvalue="true" %>
<script src="<c:url value="${src}" />"></script>
2. 非常接近新手的步骤,但将 UrlRewriteFilter
映射到 web.xml 中的 /*
:
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.将 CACHE_BUST 变量注入到每个新会话(或多或少...),应用程序部署
timestamp:
// On application deploy:
long CACHE_BUST = System.currentTimeMillis() / 1000;
// later...
session.setAttribute("cacheBust", CACHE_BUST);
4. ...这样我就可以在 urlrewrite.xml
中使用这些规则:
<outbound-rule>
<from>^/static/(css|js|images)/(.*)$</from>
<to>%{context-path}/static/%{session-attribute:cacheBust}/$1/$2</to>
</outbound-rule>
<rule>
<from>^/static/\d{10}/(css|js|images)/(.*)$</from>
<to>/static/$1/$2</to>
</rule>
非常感谢 Pascal 和新手寻求他们的帮助。
As per the Q&A here, I'd like to implement a similar auto-versioning system for a web app running in JBoss 5. Is there anything already out there to do this sort of thing, or will I need to write something myself? To be clear: I am not using PHP.
Not knowing much about PHP, I'm not sure what the Tomcat/JBoss analogs of PHP's .htaccess
, etc. are. If I do have to write my own auto-versioning, where would I start? The principle is clear to me - rewriting the URL using the file's timestamp, but I don't know much about URL rewriting with JBoss/Tomcat.
Update:
Combining the approaches recommended by Pascal and novice, here's what I ended up with:
1. Custom <my:script/>
and <my:style/>
tags, so I wouldn't have to see <c:url/>
tags everywhere.
<%@ tag body-content="empty" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="src" required="true" rtexprvalue="true" %>
<script src="<c:url value="${src}" />"></script>
2. Following fairly closely to novice's steps, but mapping UrlRewriteFilter
to /*
in web.xml:
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. Injecting a CACHE_BUST variable to every new session (more or less...), an application deploy
timestamp:
// On application deploy:
long CACHE_BUST = System.currentTimeMillis() / 1000;
// later...
session.setAttribute("cacheBust", CACHE_BUST);
4. ...so that I can use these rules in urlrewrite.xml
:
<outbound-rule>
<from>^/static/(css|js|images)/(.*)lt;/from>
<to>%{context-path}/static/%{session-attribute:cacheBust}/$1/$2</to>
</outbound-rule>
<rule>
<from>^/static/\d{10}/(css|js|images)/(.*)lt;/from>
<to>/static/$1/$2</to>
</rule>
Many thanks to Pascal and novice for their help.
发布评论
评论(3)
以下解决方案更适合生产环境,因为您将增加每个版本的版本号。
方法:
步骤:
使用 url 模式更新 web.xml,例如,
<前><代码><过滤器>
<过滤器名称>urlRewriteFilter
<过滤器类>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
<过滤器映射>
/v/*
<过滤器名称>urlRewriteFilter
更新 jsp 文件中的“abc.js”,例如,
写入urlrewritefilter.xml,例如,
说明:
当jsp提供给客户端时
当浏览器调用js/css/静态文件时
要点:
Following solution is better suited in production environment as you would be incrementing the version number for each release.
Approach:
Steps:
update web.xml with the url pattern, for e.g,
update 'abc.js' in the jsp file, for e.g.,
write urlrewritefilter.xml, for e.g.,
Explanation:
when jsp is served to client
when browser makes a call for js/css/static files
Points:
如果您不想在应用程序中使用 Apache HTTPD,那么您可以使用自定义 servlet 过滤器或重用现有的 Url重写过滤器。该过滤器基于 Apache 的
mod_rewrite
并提供类似的功能。换句话说,它将允许实现与另一个答案中的 PHP 相同的解决方案。好吧,这个想法是完全模仿您链接到的答案的“PHP 解决方案”(让我们将此选项称为 1):
/css/my.123456.css< /code> 到
/css/my.css
Servlet
,它将获取 WAR 内给定资源的File
对象并插入 < code>File#lastModified() 在该资源的返回路径中。Servlet
。另一种方法(选项 2)是将唯一的查询字符串附加到静态内容的 URL,例如服务器启动时间:
ServletContextListener
(比如在键"key" 下) )。
在你的 JSP 中
Pro 中:不再需要 url 重写。缺点:不太理想(重新启动时将请求内容)但可以接受。
在网上搜索一些有助于实现选项 1 的步骤 #2 的代码时,我发现了 Spring 的
oswsResourceServlet
正在做类似的事情,你可以看看它的源代码。但是,在更仔细地阅读其 javadoc 时,我意识到这个 servlet 实际上正是您正在寻找的东西。像这样映射它:并将其
applyLastModified
属性设置为true
。我对 javadoc 的理解是它应该可以解决问题。这是选项 3,如果添加对此 servlet 的依赖关系不是问题的话,这在我看来是最好的选择。If you don't want to front your application with Apache HTTPD, then you could use a custom servlet filter or reuse the existing Url Rewrite Filter. This filter is based on Apache's
mod_rewrite
and offers similar capabilities. In other words, it would allow to implement the same solution than the PHP one of the other answer.Well, the idea was to mimic exactly the "PHP solution" of the answer you linked to (let's call this option 1):
/css/my.123456.css
into/css/my.css
Servlet
that would get aFile
object for a given resource inside the WAR and insertFile#lastModified()
in the returned path to that resource.Servlet
from the JSP for the CSS.Another approach (option 2) would be to append an unique query string to the URL of the static content, e.g. the server startup time:
ServletContextListener
(say under the key"key"
).In you JSP
Pro: no url rewrite stuff anymore. Con: Less optimal (content will be request on restart) but acceptable.
While searching the web for some code that could help to implement the step #2 of option 1, I found Spring's
o.s.w.s.ResourceServlet
that is doing something similar, you could look at its source code. But, while reading its javadoc more carefully, I realized that this servlet is actually exactly what you're looking for. Map it like this:And set its
applyLastModified
property totrue
. My understanding of the javadoc is that it should do the trick. This is option 3 and this is IMO the best option if adding a dependency on this servlet in not an issue.我们在 Web 应用程序中执行以下操作:
We do the following in our web app: