0

I'm facing following problem actually:

I'm using Spring with jQuery. I have Controller:

@Controller
@RequestMapping(value = "/A")
public class AController {
    // not important
}

That is handling all host/A/... URLs fine. But jQuery CSS styles are using url(images/...), so there are references from host/A/index.jsp to host/A/images/.... But I have no such folder since /A/ is just "logical" URL.

I tried to add

<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/A/images/**" location="/images/" />

to my web.xml, but it seems it is not working (first one is working fine). For example when I try to test this, host/A/test.png is not working.

Of course I can modify jQuery sources, but I do not preffer this way.

Maybe I can use UrlRewriteFilter if there is not simpler solution.

Community
  • 1
  • 1
Betlista
  • 10,327
  • 13
  • 69
  • 110

2 Answers2

1

I'd suggest you to use BASE tag in your resulting HTML:

<html>
   <head>
       <base href="http://localhost:8080/myApp/" />
       ....

Then, all your image request will be done to http://localhost:8080/myApp/images/... whether if you are located in http://localhost:8080/myApp/ or in http://localhost:8080/myApp/A/

sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • Just note for others, there is [problem with base tag in IE8 and IE9](http://stackoverflow.com/questions/3926197/html-base-tag-and-local-folder-path-with-internet-explorer). – Betlista Mar 22 '12 at 10:22
  • @Betlista: It's not a bug. As the W3Schools link says: `The base URL must be an absolute URL!`. – sinuhepop Mar 22 '12 at 10:31
  • But IMHO `` is not relative URL (it starts with '/' so it's absolute), but it is not working in IE8 and IE9... My opinion is that it is not good practice to have host name in page - in development-testing-production cycle there are different host names. In my JSP I used `<%= %>` to generate this, in pure HTML that is a problem I think. – Betlista Mar 22 '12 at 12:54
  • @Betlista: No, it's an absolute URL (http://stackoverflow.com/questions/904046/absolute-urls-relative-urls-and). I must agree with you about not hardcoding your hostname. So, another reason why never use pure HTML. I use a custom taglib or velocity macro which gets the correct value from a properties file. Sometimes more sofisticated logic is required if you are behind a proxy, for example. – sinuhepop Mar 22 '12 at 13:25
  • base tag is causing [form submitting with opening new window problem](http://stackoverflow.com/questions/9825612/why-form-submit-opens-new-window-tab), just letting others know... – Betlista Mar 23 '12 at 12:45
  • @Betlista: I was following that interesting question too. Very strange behaviour. Thanks to advice us! – sinuhepop Mar 23 '12 at 12:49
  • 1
    base tag is ok, I had `target="_blank` in it (copied from w3schools) :-/ – Betlista Mar 23 '12 at 13:27
0

If you're in a app server like tomcat, you can do this in your web.xml file:

   <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/images/*</url-pattern>
   </servlet-mapping>

In my projects, normally I put something like this ;)

   <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.PNG</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.swf</url-pattern>
    <url-pattern>*.avi</url-pattern>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>

With this, you need to create a 'physical' "./A/images/" folder.

In another application server, the value can change.

Imaky
  • 1,227
  • 1
  • 16
  • 36
  • Hm, but in this way I have to copy images multiple times for all logical URLs `/A/...`, `/B/...`, I do not like the idea. If I'd like to do that, then simple `` will work exactly the same way. – Betlista Mar 21 '12 at 13:40
  • Oh, okok.... then, I don't know an answer without using UrlRewriteFilter.... sorry :( – Imaky Mar 21 '12 at 13:47