简体   繁体   中英

Resource mapping Spring framework 3

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.

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM