简体   繁体   English

Wicket应用程序中的全球可访问资源

[英]Globally accessible resources in Wicket application

Currently, I have an Application which consists of a BasePage which as a header (panel), footer(panel) and in the center for inherited page content. 目前,我有一个Application,它由一个BasePage组成,它作为标题(面板),页脚(面板)和继承页面内容的中心。 The problem I am running into is with ResourceReferences (Perhaps this isn't even the right way). 我遇到的问题是ResourceReferences(也许这甚至不是正确的方法)。 I am looking for a solution which will allow me to do the following: 我正在寻找一个解决方案,允许我做以下事情:

  1. Have a single directory for globally used images, js, css 为全局使用的图像,js,css设置一个目录
  2. Register (or not) those resources so that they can be accessible from any inherited page or sibling pages to BasePage I might create in the future 注册(或不注册)这些资源,以便可以从任何继承的页面或兄弟页面访问我可能在将来创建的BasePage
  3. Allow those resources to be accessible within CSS and JS (eg urls to images) 允许在CSS和JS中访问这些资源(例如,图像的URL)

So far I have read through several examples which show how to package resource for a component or application level scope, but none that seem to address all 3 issues I am looking for help with. 到目前为止,我已经阅读了几个示例,这些示例展示了如何为组件或应用程序级别范围打包资源,但似乎没有一个能够解决我正在寻求帮助的所有3个问题。 It is critical that I don't have to copy globally used images (edit icon, logos, etc) into each component package for referencing, and it would be nice for maintenance reasons that these bindings be made in one place globally for easy reference and updating. 至关重要的是,我不必将全局使用的图像(编辑图标,徽标等)复制到每个组件包中以进行引用,并且出于维护原因这些绑定可以在一个地方全局进行以便于参考,这将是很好的。更新。

This has changed significantly since Wicket 1.4. 自Wicket 1.4以来,这已经发生了重大变化。 Please see 'Adding Javascript or CSS using a Resource' in Wicket's Wiki 请参阅Wicket Wiki中的“使用资源添加Javascript或CSS”

To make images and other resources globally accessible (especially from CSS and JS files), mount them in your Applications's init() method: 要使图像和其他资源可以全局访问(特别是从CSS和JS文件),请将它们安装在Applications的init()方法中:

mountSharedResource("/images/submit.jpg", new ResourceReference(MyComponent.class, "foo.jpg").getSharedResourceKey());

There is absolutely no need to duplicate resources in any way. 绝对不需要以任何方式复制资源 Resource don't have to be in the same packages as the component itself. 资源不必与组件本身位于相同的包中。 In our applications, we put globally used resources into a dedicated packages (say com.example.myapp.images) and put a single class in it (eg ImagesScope.java) - same for JS and CSS. 在我们的应用程序中,我们将全局使用的资源放入专用包(比如com.example.myapp.images)并在其中放入一个类(例如ImagesScope.java) - 对于JS和CSS也是如此。

For images, you won't need ResourceReference as you won't need those references rendered in your code (except for org.apache.wicket.markup.html.image.Image). 对于图像,您不需要ResourceReference因为您不需要在代码中呈现这些引用(org.apache.wicket.markup.html.image.Image除外)。 For JS and CSS use 用于JS和CSS使用

add(CSSPackageResource.getHeaderContribution(PanelOne.class, "PanelOne.css"));

By the way, I'm the author of a little library called wicketstuff-merged-resources available from GitHub. 顺便说一下,我是一个名为wicketstuff-merged-resources小库的作者,可以从GitHub获得。 Using this library you may skip the manual mounting in your Application's init() and use annotations instead. 使用此库,您可以跳过Application的init()中的手动安装,而是使用注释。

EDIT: updated link. 编辑:更新链接。 unfortunately, documentation in the wicketstuff wiki seems to be gone now. 不幸的是,wicketstuff wiki中的文档现在似乎已经消失了。 There are some articles on our blog though. 但是我们的博客上一些文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM