Wicket - 添加静态资源的最佳方式

Wicket - best way of adding static resources

在 Wicket 应用程序中,我们可以使用标准 HTML 方式在标记中指定 URL 资源,如下所示:

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>

或者我们可以像这样使用 IHeaderContributor 添加资源:

@Override
public void renderHead(IHeaderResponse response) {

    String contextPath = WebApplication.get().getServletContext().getContextPath();
    UrlResourceReference urlResourceReference = new UrlResourceReference(Url.parse(contextPath
            + "/js/jquery-1.11.3.min.js"));

    response.render(JavaScriptHeaderItem.forReference(urlResourceReference));

    super.renderHead(response);
}

最后我们在 html 代码中有相同的 <script></script> 标记,在第二种方法中我们只能将资源添加到 <header> 部分。那么使用wickey方式添加资源有什么特殊原因吗?

可能还有更多原因,但这是我喜欢的一个:

引自'Whats new in Wicket 6':

Additionally PackageResourceReference and its specializations can deliver minified version of their resource if such is available. For example by using new JavaScriptResourceReference(MyComponent.class,"my.js") Wicket will deliver my.js in development mode but will deliver my.min.js in production mode if it is available in the same folder.

题外话:
Wicket 也使用 jQuery 做很多事情。确保您没有组合不同的版本。

我们这样做是因为:

TL;DR;

开发中 样式和脚本总是在每次重新部署时刷新。在 production 中,它们在进行更改时由客户端获取。缓存工作正常。

来自Wicket User Guide的详细解释:

As you can see Wicket has automatically appended to the file name a version identifier (ver-1297887542000). When Wicket runs in DEVELOPMENT mode this identifier contains the timestamp in millisecond indicating the last time the resource file was modified. This can be useful when we are developing our application and resource files are frequently modified. Appending the timestamp to the original name we are sure that our browser will use always the last version of the file and not an old, out of date, cached version.

When instead Wicket is running in DEPLOYMENT mode, the version identifier will contain the MD5 digest of the file instead of the timestamp. The digest is computed only the first time the resource is requested. This perfectly makes sense as static resources don't change so often when our application runs into production environment and when this appends the application is redeployed.