Search code examples
urlgwtradix

GWT: is it possible to set base URL as a parameter?


I am trying to make GWT application work in a portlet. Portlet is a part of a page which is formed in a context, other that entire page.

This makes paths not working.

For example, and object

Image logo = new Image("images/online.png");

is rendered as

<img class="gwt-Image" src="images/online.png">

This path is correct in portlet context but wrong in root context.

Is it possible to make GWT to look for paths starting from getContextPath()? It would be great if it was possible to add some parameters into HTML code before main Javascrip insertion.


Solution

  • Use GWT.getModuleBaseURL() as a base for your URLs (possibly using .. to move up); e.g.

    Image logo = new Image(GWT.getModuleBaseURL() + "../images/online.png");
    

    As a rule of thumb, always make your URLs relative to either GWT.getHostPageBaseURL() or GWT.getModuleBaseURL().

    You could of course also use ClientBundle and ImageResource to be deployment-agnostic.