Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Tomcat and static resources and mvc:resources

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

like image 750
gotch4 Avatar asked Jan 24 '11 15:01

gotch4


People also ask

Does Spring Boot add static web resources automatically?

While this may not be a new revelation to those of you that have been following Spring Boot since the SpringOne announcement, there is one detail for which you may not be aware. Spring Boot will automatically add static web resources located within any of the following directories:

What is resourcehttprequesthandler in Spring Boot?

2. Using Spring Boot Spring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

How do I use Tomcat with Spring Boot?

Using the Spring Boot command line interface and a minimal amount of Groovy code you can direct Spring Boot to start Tomcat. Consider the following app.groovy file: The single empty class, annotated with @Controller, will trigger AutoConfiguration within Boot, which will set up a full Spring MVC stack and enable support for Tomcat.

What is static web resources versioning?

Static web resources versioning is a central concern when pushing web apps to production and very much a server-side concern.


2 Answers

<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" />
like image 178
axtavt Avatar answered Nov 14 '22 20:11

axtavt


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.

like image 24
ColdStoneJava Avatar answered Nov 14 '22 20:11

ColdStoneJava