我开始从头开始开发网络应用程序。之前我一直在处理已经运行了很长时间的应用程序,所以我不必处理完整的设置阶段。我正在使用 Spring 3 和 Tomcat 6,我正在使用 Eclipse 3.6
我在提供图像(或其他与 Controller 响应不同的东西)方面遇到了很大的问题。事实上,我找不到在我的 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 中,我将能够提供图像,但 Controller “accise”将无法访问。我是在误用还是误解了资源标签?正确的做法是什么?
找到更新解决方案!!! :)
问题是我的 servlet-config.xml 遗漏了一个声明:
现在是(在 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/" />
最佳答案
<mvc:resources>
与带注释的 Controller 一起玩得很好,但可能需要对其他类型的 Controller 映射进行一些额外的配置。
我想在你的情况下你需要声明 BeanNameUrlHandlerMapping
手动(它通常默认注册,但 <mvc:resources>
覆盖默认值作为应用其自己的配置的副作用):
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
关于java - Spring Tomcat和静态资源和mvc :resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4784060/