Spring Tomcat和静态资源以及mvc:resources


问题内容

我从头开始制作一个Web应用程序。在我一直在研究已经运行很长时间的应用程序之前,因此我不必处理整个设置阶段。我正在使用Spring 3和Tomcat
6,并且正在使用Eclipse 3.6

提供图片(或与控制器响应不同的其他问题)时遇到了一个大问题。实际上,我找不到在jsps中保存图像的方法。我的配置适用于:

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

在web.xml和

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

</bean>

用于Servlet上下文(当然还有其他内容)。

我在这里和其他论坛上读过很多关于此的消息:

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

但是,如果将其插入到servlet-context.xml中,则可以提供图像,但是控制器“
accise”将无法访问。我是滥用还是误解了资源标签?正确的方法是什么?


找到更新解决方案!!! :)

问题是我的servlet-config.xml缺少一个声明:

现在是(使用控制器上的注释):

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

问题答案:

<mvc:resources> 与带注释的控制器一起使用时效果很好,但可能需要与其他类型的控制器映射一起进行一些额外的配置。

我猜在您的情况下,您需要BeanNameUrlHandlerMapping手动声明(通常默认情况下已注册,但<mvc:resources>会覆盖默认值,这是应用其自身配置的副作用):

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