[英]Spring Tomcat and static resources and 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" />
我有同样的问题。 我能够通过使用像。的完整路径来解决这个问题
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?
让我知道,如果在Spring提出一些更好的方法之前解决了你的问题。
我也有同样的问题。 我通过在实际引用资源之前添加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>
干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.