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

got*_*ch4 9 java spring jsp tomcat spring-mvc

我从头开始做一个Web应用程序.在我一直在处理已经运行了很长时间的应用程序之前,所以我没有必要处理完整的设置阶段.我使用的是Spring 3和Tomcat 6,我正在使用Eclipse 3.6

我提供图像(或其他与控制器响应不同的东西)存在很大问题.事实上,我无法找到一种方法将我的图像放在我的jsps中.我的配置,适用于:

 <servlet-mapping> 
     <servlet-name>springDispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
 </servlet-mapping> 
Run Code Online (Sandbox Code Playgroud)

在web.xml和

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

</bean>
Run Code Online (Sandbox Code Playgroud)

对于servlet上下文(当然还有其他).

我在这里阅读了很多消息,其他论坛也在谈论这个:

<mvc:resources mapping="/resources/**" location="/resources/" />
Run Code Online (Sandbox Code Playgroud)

但如果我在我的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/" />
Run Code Online (Sandbox Code Playgroud)

axt*_*avt 7

<mvc:resources> 使用带注释的控制器可以很好地运行,但可能需要一些额外的配置与其他类型的控制器映射.

我想在你的情况下你需要BeanNameUrlHandlerMapping手动声明(它通常默认注册,但<mvc:resources>覆盖默认值作为应用自己的配置的副作用):

<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
Run Code Online (Sandbox Code Playgroud)