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/" />

Dani
  • 3,744
  • 4
  • 27
  • 35
gotch4
  • 13,093
  • 29
  • 107
  • 170

3 Answers3

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" />
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • with this the images work but spring doesn't find the mapping for the controller (that it finds without the resources tag). – gotch4 Jan 24 '11 at 17:02
  • @gotch4: Show your `MainController`. – axtavt Jan 24 '11 at 17:18
  • Jesus what a pain... This needs to be logged as a bug in Spring for sure. – sourcedelica May 03 '11 at 08:21
  • @erucacm: I don't believe it's a bug. Basically, this behaviour with overriding defaults is described in [`DispatcherServlet`'s javadoc](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html). Also, it don't harm annotated controllers, only controllers mapped by bean name. – axtavt May 03 '11 at 08:51
  • Actually it does harm annotated controllers. I got bit by this just recently (which is why I logged the bug :) I added and boom! my controllers disappeared. – sourcedelica May 18 '11 at 13:59
  • @sourcedelica I appreciate SPR-8289 is marked closed but i'm having the exact same issue with spring 3.1.2. Is there an option to enable extra logging on the mvc:resource mapper to see how it might be effecting my form submission request? – emeraldjava Oct 16 '14 at 08:45
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.

matthaeus
  • 797
  • 2
  • 7
  • 17