9

I started doing a web app from scratch. Before I've always been working on apps that were already running for a long time, so I didn't have to deal with the full setup phase. I am using Spring 3 and Tomcat 6, and I am using Eclipse 3.6

I've a big problem with serving images (or other things different from controller responses). In fact I can't find a way to have my images in my jsps. My configuration, works with:

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

in web.xml and

<bean name="/accise" class="it.jsoftware.jacciseweb.controllers.MainController">

</bean>

for the servlet context (plus other of course).

I've read many messages here and other forums talking about this:

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

but if I insert that in my servlet-context.xml, I will be able to serve images, yet the controller "accise" won't be reachable. Am I misusing or I misunderstood the resources tag? What's the correct way?


Update solution found!!! :)

The problem was that my servlet-config.xml missed one declaration:

Now it is(using annotations on the controller):

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.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
        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">


    <context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan>
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

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

3 Answers 3

7

<mvc:resources> plays well with annotated controllers, but may require some extra configuration with other kinds of controller mappings.

I guess in your case you need to declare BeanNameUrlHandlerMapping manually (it's usually registered by default, but <mvc:resources> overrides defaults as a side-effect of applying its own configuration):

<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
6
  • with this the images work but spring doesn't find the mapping for the controller (that it finds without the resources tag). <context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan> <mvc:resources mapping="/resources/**" location="/resources/" /> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    – gotch4
    Commented Jan 24, 2011 at 17:02
  • @gotch4: Show your MainController.
    – axtavt
    Commented Jan 24, 2011 at 17:18
  • Jesus what a pain... This needs to be logged as a bug in Spring for sure. Commented May 3, 2011 at 8:21
  • @erucacm: I don't believe it's a bug. Basically, this behaviour with overriding defaults is described in DispatcherServlet's javadoc. Also, it don't harm annotated controllers, only controllers mapped by bean name.
    – axtavt
    Commented May 3, 2011 at 8:51
  • Actually it does harm annotated controllers. I got bit by this just recently (which is why I logged the bug :) I added <mvc:resources> and boom! my controllers disappeared. Commented May 18, 2011 at 13:59
1

I had the same problem. I was able to fix this by using the full path like for

CSS

<link rel="stylesheet" type="text/css" media="screen" href="/my-web-app/resources/css/smoothness/jquery-ui-1.8.21.custom.css">

and for javascript

<script type="text/javascript" src="/my-web-app/static/js/jquery-1.4.4.min.js"></script?

Let me know if this solves your problem before Spring comes up with some better approach.

0

I had the same problem, too. I solved it by adding the spring macro before the actual reference to the resource, in order to resolve the servlet path.

It is not really nice in the code, but this solution makes you being independent from the actual context under which your application is deployed on the server and your servlet too. Usually, you don't want to have the actual servlet being mentioned in your url.

For instance, refering to the answer of ColdStoneJava, that would be:

  <script type="text/javascript" src="<@spring.url '/static/js/jquery-1.4.4.min.js'/>"></script>

You also have to reference it absolute in the url, so the slash '/...' is essential.

This, won't work in my experience:

  <script type="text/javascript" src="<@spring.url 'static/js/jquery-1.4.4.min.js'/>"></script>

Cheers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.