abiieez
abiieez

Reputation: 3189

Mapping URL in Spring MVC

I have the following navigation links

<li><a href="account">My Account</a></li>
<li><a href="ewallet/register">Register E-Wallet</a></li>

Both links refer to a page template which contains an img element as follows

<img src="resources/images/tdyslogo.png" alt="logo" width="150px" border="0">

The issues:

  1. upon clicking the second link, the img element is resolving to wrong URL that is /business/ewallet/resources/images/tdyslogo.png thus image not loaded
  2. when I tried to access other navigation links, the URL also resolving to wrong url /business/ewallet/account hence I get 404 upon clicking

I tried to change my mvc:resources to <mvc:resources mapping="/resources/**" location="resources/" /> in order to map any URL containing resources to the correct resources folder but no avail.

Based on Absolute vs relative URLs it seems like I need to use relative or absolute URL correctly. If this is correct, how can I fix it ?

Upvotes: 0

Views: 227

Answers (2)

Braj
Braj

Reputation: 46871

If you are using tomcat server then try with default servlet to serve static resoruces directly.

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

Better use c:url and use prefix / in the value to make the url relative the context path.

<img id="logo" src="<c:url value='/resources/images/logo.png'/>" />

Dynamic project structure:

WebContent
         |
         |__resources
         |          |
         |          |__images
         |                   |
         |                   |__logo.png
         |
         |__WEB-INF
                  |
                  |__web.xml

Read more...

Upvotes: 1

NimChimpsky
NimChimpsky

Reputation: 47300

You would conventionally have all the images in a static resources that is under the root, so

<img src="<c:url value='/resources/images/tdyslogo.png'/>" alt="logo" width="150px" border="0">

Should work, using jsp c tag, which is included like below :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Upvotes: 1

Related Questions