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

9
我开始从零做一个Web应用程序。以前我总是在处理已经运行了很长时间的应用程序,所以我不需要处理完整的设置阶段。我正在使用Spring 3和Tomcat 6,并且我正在使用Eclipse 3.6。
我在处理图像(或其他与控制器响应不同的内容)时遇到了大问题。事实上,我找不到一种方法来在我的JSP中放置我的图像。我的配置可以使用以下方式进行操作:
 <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/" />

3个回答

7

<mvc:resources>与注解控制器兼容良好,但在其他类型的控制器映射中可能需要一些额外的配置。

我猜在你的情况下,你需要手动声明BeanNameUrlHandlerMapping(通常情况下它是默认注册的,但是<mvc:resources>会覆盖默认设置并应用自己的配置):

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

通过这个配置,图片可以正常工作,但是Spring找不到控制器的映射(如果没有资源标签,它会找到)。<context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan> <mvc:resources mapping="/resources/**" location="/resources/" /> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> - gotch4
请展示你的MainController,@gotch4。 - axtavt
天啊,真是太痛苦了...这肯定需要在Spring中记录为一个bug。 - sourcedelica
@erucacm:我不认为这是一个bug。基本上,覆盖默认值的这种行为在DispatcherServlet的javadoc中有描述。而且,它不会影响到注释控制器,只会影响到按bean名称映射的控制器。 - axtavt
实际上,它会对注释控制器造成影响。我最近遇到了这个问题(这就是为什么我记录了这个错误:) 我添加了mvc:resources,结果我的控制器消失了。 - sourcedelica
@sourcedelica,我知道SPR-8289已经标记为关闭,但是我在使用Spring 3.1.2时遇到了完全相同的问题。是否有选项可以启用mvc:resource映射器上的额外日志记录,以查看它如何影响我的表单提交请求? - emeraldjava

1
我曾经遇到过同样的问题。我通过使用完整路径来解决了这个问题,例如:
CSS
<link rel="stylesheet" type="text/css" media="screen" href="/my-web-app/resources/css/smoothness/jquery-ui-1.8.21.custom.css">

还有针对JavaScript的内容

<script type="text/javascript" src="/my-web-app/static/js/jquery-1.4.4.min.js"></script?

在春季提出更好的方案之前,请让我知道这是否解决了你的问题。


0

我也遇到了同样的问题。我通过在实际资源引用之前添加Spring宏来解决它,以解决servlet路径。

虽然这种解决方案在代码中并不是很好看,但它使您独立于应用程序在服务器上部署的实际上下文和servlet。通常,您不希望在URL中提到实际的servlet。

例如,参考ColdStoneJava的答案,那将是:

  <script type="text/javascript" src="<@spring.url '/static/js/jquery-1.4.4.min.js'/>"></script>

你还必须在 URL 中绝对引用它,所以斜杠' /...' 是必要的。

根据我的经验,这样做是行不通的:

  <script type="text/javascript" src="<@spring.url 'static/js/jquery-1.4.4.min.js'/>"></script>

祝福。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接