1

I have spent the last hours trying to figure out how to access static ressources from a jsp in Spring mvc web application.

Here is a part from my beans config file:

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

now in the first jsp page my css file located at /css/style.css works fine like this:

<link rel="stylesheet" href="css/style.css" type="text/css" />

but in the second jsp page i can't access this image located at /images/logo.gif:

<img src="images/logo.gif" alt="anotherimage" />

can someone explain how the <mvc:ressources /> works with jsp ?

EDIT: here is my context

<?xml version="1.0" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean name="viewResolver" 
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="exposeContextBeansAsAttributes" value="true" />
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"    value="com.mysql.jdbc.Driver" />
        <property name="url"                value="jdbc:mysql://localhost/test" />
        <property name="username"           value="root" />
        <property name="password"           value="" />
    </bean>

    <bean id="sessionFactory"   class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="mmapp.domain" />
        <property name="hibernateProperties">
            <value>
                hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
            </value>
        </property>
    </bean>

    <bean id="transactionManager"     class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="jdbcTemplate"     class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>

    <bean id="simpledata" class="mmapp.util.SimpleDataProvider" />

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:annotation-config />
    <context:component-scan base-package="mmapp" />

    <mvc:default-servlet-handler/>
    <mvc:resources mapping="/images/**" location="/images/" /> 
</beans>

AND my web.xml

<?xml version="1.0" ?>

<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:shemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <servlet-name>mmapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mmapp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
elaich
  • 939
  • 1
  • 19
  • 35
  • By my reading, shouldn't it be /images/logo.gif"? Perhaps the css directory is relative to the current directory, and that allowed that file to be loaded in that fashion. – Marvo Jun 21 '12 at 20:32
  • I have tried that and it didn't work. – elaich Jun 21 '12 at 20:43

3 Answers3

2

The safest way to access resources is to use the jstl core library. At the very least, you can be sure that the correct url is generated in any context. Add the following line of code to your jsp file:

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

and change the img tag to use the c:url tag:

<img src="<c:url val="/images/logo.gif" />" alt="anotherimage" />

Make sure your src attribute looks good after you make this change. You'll also have to add the jstl library to your project (probably a good idea since it's pretty standard). Your mvc:resources mapping looks good as far as I can tell.

threejeez
  • 2,314
  • 6
  • 30
  • 51
1

after this

<mvc:default-servlet-handler/>
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />

to get your css/image :

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" type="text/css" />
<img src="${pageContext.request.contextPath}/images/logo.gif" alt="anotherimage" />
storm_buster
  • 7,362
  • 18
  • 53
  • 75
0

I don't know if that make sense, but here is how I have resolved this:

first my controller looks like:

@Controller
@RequestMapping(value="/reports")
public class ReportsController {

    @RequestMapping(value = "/reporting", method = RequestMethod.GET)
    public String onSubmit() {
        return "home" ;
    }
}

With all configuration listeb above, I just had to change my img tag in my home.jsp from <img src="images/logo.gif" /> to <img src="../images/logo.gif" />, and things worked well, if i remove the value = "/reporting" from my second @RequestMapping it's the <img src="images/logo.gif" /> which work!

elaich
  • 939
  • 1
  • 19
  • 35