【问题标题】:How to get the URL to a Wicket shared resource?如何获取 Wicket 共享资源的 URL?
【发布时间】:2010-11-01 17:42:33
【问题描述】:

网页设计师给我的 HTML 看起来像:

<div .... style="background: transparent url(xxx.png) 170px center no-repeat">

不幸的是,图像xxx.png 的内容是由软件生成的,所以我将其设为WebResource 并使用以下策略生成资源的URL,然后我将其嵌入style= 属性中一个检票口AttributeModifier

// App initialization code
String resourceName = ....;
getSharedResources().add(resourceName, myWebResource);

// Creating the widget
String url = getServletContext().getContextPath()
    + "/resources/org.apache.wicket.Application/" + resourceName ;
String style = "background: transparent url(" + url + ") 170px center no-repeat";
div.add(new AttributeModifier("style", new Model<String>(style)));

当我使用 Eclipse 在本地测试它时,这工作正常,但是:

  • 当我在生产环境中安装它时,我想让 Apache 作为 Jetty 的代理,这样上下文根不可见,即 Apache 将 /foo 的请求以 /context-root/foo 转发到 Jetty。
  • 总的来说,我认为这不是很优雅。我确定我在这里复制 Wicket 代码?

我了解 Wicket 仅通过使用相对 URL 解决了上下文根和 Apache 代理的问题。这将是我怀疑的最优雅的解决方案。但如果我有例如IndexedParamUrlCodingStrategy 然后 URL 可以是任意长度,我不知道要包含多少 .. 才能返回到 /resources

编辑:当前的解决方案是在我上面的代码示例中使用绝对 URL,并在 Apache 中 (a) 像以前一样将 /context-root/* 重写为 /* (b) 然后添加上下文所有请求的根 (c) 转发到 Jetty。这样,大多数 URL 可以没有上下文根,但某些 URL(到我的资源)可以有上下文根,这没关系。但我不喜欢这种解决方案!

【问题讨论】:

  • 这不能回答问题,但您可以稍微简化最后一行:new SimpleAttributeModifier("style", style)
  • @Jonik,太好了,+1 感谢您的提示!

标签: wicket


【解决方案1】:

如果代码是从组件(或页面)内部调用的:

urlFor(new ResourceReference("sharedResourceName"));

RequestCycle.get().urlFor(new ResourceReference("sharedResourceName"));

下面的示例应用程序。为简单起见,我使用了 ByteArrayResource,但任何 Resource 子类都可以:

WicketApplication.java

package app1;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.target.coding.IndexedParamUrlCodingStrategy;
import org.apache.wicket.resource.ByteArrayResource;

public class WicketApplication extends WebApplication {
    @Override
    protected void init() {
        super.init();
        getSharedResources().add("testResource", new ByteArrayResource("text/plain", "This is a test".getBytes()));
        mount(new IndexedParamUrlCodingStrategy("home/very/deep/folder", getHomePage()));
    }
    public Class<HomePage> getHomePage() {
        return HomePage.class;
    }
}

HomePage.java

package app1;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;

public class HomePage extends WebPage {
    public HomePage(final PageParameters parameters) {
        CharSequence resourceHref = urlFor(new ResourceReference("testResource"));
        add(new Label("link", "Click me!")
            .add(new SimpleAttributeModifier("href", resourceHref)));
    }
}

主页.html

<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
    <body>
        <a wicket:id="link"></a>
    </body>
</html>

【讨论】:

  • 完美!检票口的想法非常好。所以我知道它必须是可能的。非常感谢! :)
  • 谢谢,但是。如果没有与当前线程关联的 RequestCycle 怎么办?
  • 我正在使用 Wicket 6,这似乎不再起作用。 ResourceReference(string) 构造函数不存在,并且 ResourceReference 类现在是 Abstract 并且需要实现。有没有其他简单的方法来获取共享资源的 URL?
【解决方案2】:

我认为this answer 中用于创建动态图像 url 的策略将适用于此。

【讨论】:

  • 据我所见,它创建并注册了 Wicket 的资源;这一点我能做到。但是,除了像我的示例中那样手动生成 URL 之外,我不知道如何访问该资源的 URL。我担心如果 Wicket 有一天会改变资源 URL 的工作方式,我的代码会中断。 Wicket 必须在某处有代码来计算资源的 URL,所以我应该可以使用它而不是重新编码它......?
猜你喜欢
  • 2014-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 2021-10-10
  • 2015-10-27
  • 1970-01-01
  • 2022-12-12
相关资源
最近更新 更多