0

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 ?

2 Answers 2

1

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" %>
Sign up to request clarification or add additional context in comments.

5 Comments

The image still fail to load. The only way to make it visible after pressing the second link is by changing the src to <img src="../resources/images/tdyslogo.png" alt="logo" width="150px" border="0">
have altered answer to use the c url jsp tag
the generated img element is wrong (<img src="&lt;c:url value='/resources/images/tdyslogo.png'/&gt;" alt="logo" width="150px" border="0">)
Thanks for pointing that out. By the way the second issue still goes unanswered. Maybe I should create a separate question
the c:url is relative, oh the /business/ewallet/account is not what you want ? I use c:url for everything, links images etc
1

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...

2 Comments

Added that but doesn't solve the issue. This issue only appearing when clicking on link which has /. E.g <a href="ewallet/register">. Other links are fine.
Follow the link I have shared the JSP code as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.