Shows image in JSP

Asked

Viewed 1,646 times

2

I’m having trouble showing an image on my JSP page. I’m trying to use several different ways to get the file and I can’t show it on the page.

Folders are arranged as follows

WebContent
|_1.jpg
|_ParaEmpresas.png
|_img
    |_1.jpg
    |_ParaEmpresas.png

The code on my jsp page

<img src="../../1.jpg"/>
<img src="<c:url value="/img/ParaEmpresas.png"/>" />
<img src="<c:url value="/ParaEmpresas.png"/>" />

and the error when trying to load the page

ADVERTÊNCIA: No mapping found for HTTP request with URI [/DataIdea/img/1.jpg] in DispatcherServlet with name 'springmvc'set 08, 2015 4:13:08 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/javaee/web-app_3_0.xsd" id="Webapp_id" version="3.0"> Dataidea

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
        <description>postgreSQL Datasource example</description>
         <res-ref-name>jdbc/postgres</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
         <res-auth>Container</res-auth>
    </resource-ref>
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>

</servlet>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>

</servlet-mapping>
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/WEB-INF/views/erro/erro.jsp</location>
</error-page>
<listener>
    <listener-class>br.com.quartz.ListenToMeFirst</listener-class>
</listener>

  • What is the difference between your JSP and IMG directories? You are probably stating the incorrect path, you can confirm in the browser console if you have given erro 404

  • You have Servlet configured to expose the directory publicly?

  • @Brunswick I had already tried using JSP Scriplet. I tried using JSTL and also did not give.

  • @flpms excuses my ignorance, but since the application is great then and I am new project I had not yet touched the XML, so I updated the question with the code. If you need any information, let me know

1 answer

1


One solution I found was to add this code to WEB.xml

    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>

and make reference to the image using the following tag

<img src="<c:url value="….. "/>" />

Using this hierarchy in folders

WebContent
|_img
    |_ParaEmpresas.png

The code would look like this

 <img src="<c:url value="/img/ParaEmpresas.png"/>" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.