How to get the url for a Wicket share?

The web designer gave me HTML that looks like this:

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

Unfortunately, the content of the xxx.png image xxx.png generated by the software, so I made it a WebResource and used the following strategy to create a resource URL, which I then embed in the style= attribute using the AttributeModifier Wicket.

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

This works fine when I test it locally with Eclipse, but:

  • When I install this during production, I want Apache to be a Jetty proxy so that the context root is not displayed, i.e. Apache sends a /foo request to Jetty as /context-root/foo .
  • All in all, I don’t think it is very elegant. Am I sure I duplicate Wicket code here?

I understand that Wicket solves this issue of Apache's contextual roots and proxy using only relative URLs. That would be the most elegant solution I suspect. But if I have, for example, a IndexedParamUrlCodingStrategy , then the URL can be of arbitrary length, and I do not know how much .. to include in order to return to /resources .

Change The current solution is to use absolute URLs, as in my code example above, and in Apache (a) rewrite /context-root/* in /* (b), as before, then ADD the root context for all requests (c) forward jetty. Thus, most URLs can be without a root context, but some URLs (to my resources) can have a root context and this is normal. But I do not like it!

+6
wicket
source share
2 answers

If the code is called from within a component (or page):

 urlFor(new ResourceReference("sharedResourceName")); 

or

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

Sample application below. For simplicity, I used ByteArrayResource, but any subclass of Resource will do:

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> 
+12
source share

I think the tactics used in this> to create dynamic image URLs will apply here.

0
source share

All Articles