2010-11-01 65 views
6

网页设计师给我的HTML看起来像:如何获取Wicket共享资源的URL?

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

不幸的是,图像xxx.png的内容是由软件生成的,所以我做了它WebResource并使用以下策略来生成的URL然后我使用Wicket AttributeModifier将该资源嵌入style=属性中。

// 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作为代理,码头使得上下文根ISN” t可见,即Apache将/foo的请求转发给Jetty,作为/context-root/foo
  • 一般来说,我觉得这不是很优雅。我确定我在这里复制Wicket代码?

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

编辑:目前的解决方案是使用绝对URL如上述我的代码示例,并且在Apache中的(a)重写/context-root/*/*(b)中如在此之前的上下文根添加到所有请求(c)向到码头。这样,大多数URL可以没有上下文根,但一些URL(对我的资源)可以有上下文根,并且没问题。但我不喜欢这个解决方案!

+1

这没有按” t回答这个问题,但是可以简化最后一行:'new SimpleAttributeModifier(“style”,style)' – Jonik 2010-11-01 20:08:45

+0

@Jonik,太棒了,+1谢谢你的提示! – 2010-11-02 15:19:51

回答

12

如果代码是由组分(或页)的内部称为:

urlFor(new ResourceReference("sharedResourceName")); 

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

样品下面的应用程序。我曾经为了简单起见,使用ByteArrayResource,但是任何资源的子类会做的事:

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))); 
    } 
} 

HomePage.html

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

完美! Wicket如此深思熟虑。所以我知道它必须是可能的。非常感谢! :) – 2010-11-19 08:02:02

+0

谢谢,但。如果没有与当前线程关联的RequestCycle会怎么样? – 2014-09-09 14:52:49

+1

我正在使用Wicket 6,这似乎不再有效。 ResourceReference(字符串)构造函数不存在,ResourceReference类现在是Abstract,需要实现。有没有其他简单的方法来获取共享资源的URL? – 2016-07-06 07:45:07

0

我认为this answer用于创建动态图片网址的策略适用于此。

+0

据我所知,用Wicket创建和注册资源;这一点我可以做。但是我不知道如何才能访问该资源的URL,除了像我的示例中那样手动生成URL之外。我很担心,如果Wicket有一天改变了资源URL的工作方式,我的代码就会崩溃。Wicket必须有代码才能找出资源的URL,所以我应该可以使用它而不是重新编码它......? – 2010-11-02 15:23:26