java Spring Tomcat 和静态资源以及 mvc:resources
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4784060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Tomcat and static resources and mvc:resources
提问by gotch4
I started doing a web app from scratch. Before I've always been working on apps that were already running for a long time, so I didn't have to deal with the full setup phase. I am using Spring 3 and Tomcat 6, and I am using Eclipse 3.6
我从头开始做一个网络应用程序。之前我一直在处理已经运行了很长时间的应用程序,所以我不必处理完整的设置阶段。我使用的是 Spring 3 和 Tomcat 6,我使用的是 Eclipse 3.6
I've a big problem with serving images (or other things different from controller responses). In fact I can't find a way to have my images in my jsps. My configuration, works with:
我在提供图像(或与控制器响应不同的其他事物)方面遇到了大问题。事实上,我找不到在我的 jsps 中包含我的图像的方法。我的配置,适用于:
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
in web.xml and
在 web.xml 和
<bean name="/accise" class="it.jsoftware.jacciseweb.controllers.MainController">
</bean>
for the servlet context (plus other of course).
对于 servlet 上下文(当然还有其他的)。
I've read many messages here and other forums talking about this:
我在这里和其他论坛上阅读了许多讨论此问题的消息:
<mvc:resources mapping="/resources/**" location="/resources/" />
but if I insert that in my servlet-context.xml, I will be able to serve images, yet the controller "accise" won't be reachable. Am I misusing or I misunderstood the resources tag? What's the correct way?
但是如果我将它插入到我的 servlet-context.xml 中,我将能够提供图像,但控制器“accise”将无法访问。我是在滥用还是误解了资源标签?正确的方法是什么?
Update solution found!!! :)
找到更新解决方案!!!:)
The problem was that my servlet-config.xml missed one declaration:
问题是我的 servlet-config.xml 漏掉了一个声明:
Now it is(using annotations on the 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/" />
采纳答案by axtavt
<mvc:resources>
plays well with annotated controllers, but may require some extra configuration with other kinds of controller mappings.
<mvc:resources>
与带注释的控制器一起玩得很好,但可能需要对其他类型的控制器映射进行一些额外的配置。
I guess in your case you need to declare BeanNameUrlHandlerMapping
manually (it's usually registered by default, but <mvc:resources>
overrides defaults as a side-effect of applying its own configuration):
我猜在你的情况下你需要BeanNameUrlHandlerMapping
手动声明(它通常默认注册,但<mvc:resources>
覆盖默认值作为应用它自己的配置的副作用):
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
回答by ColdStoneJava
I had the same problem. I was able to fix this by using the full path like for
我有同样的问题。我能够通过使用完整路径来解决这个问题
CSS
CSS
<link rel="stylesheet" type="text/css" media="screen" href="/my-web-app/resources/css/smoothness/jquery-ui-1.8.21.custom.css">
and for javascript
和 javascript
<script type="text/javascript" src="/my-web-app/static/js/jquery-1.4.4.min.js"></script?
Let me know if this solves your problem before Spring comes up with some better approach.
在 Spring 提出更好的方法之前,让我知道这是否能解决您的问题。
回答by matthaeus
I had the same problem, too. I solved it by adding the spring macro before the actual reference to the resource, in order to resolve the servlet path.
我也有同样的问题。我通过在实际引用资源之前添加 spring 宏来解决它,以解析 servlet 路径。
It is not really nice in the code, but this solution makes you being independent from the actual context under which your application is deployed on the server and your servlet too. Usually, you don't want to have the actual servlet being mentioned in your url.
它在代码中并不是很好,但是这个解决方案使您独立于您的应用程序部署在服务器和您的 servlet 上的实际上下文。通常,您不希望在您的 url 中提到实际的 servlet。
For instance, refering to the answer of ColdStoneJava, that would be:
例如,参考 ColdStoneJava 的答案,那将是:
<script type="text/javascript" src="<@spring.url '/static/js/jquery-1.4.4.min.js'/>"></script>
You also have to reference it absolute in the url, so the slash '/...' is essential.
您还必须在 url 中绝对引用它,因此斜杠 '/...' 是必不可少的。
This, won't work in my experience:
这在我的经验中不起作用:
<script type="text/javascript" src="<@spring.url 'static/js/jquery-1.4.4.min.js'/>"></script>
Cheers.
干杯。