Spring Tomcat 和静态资源以及 mvc:resources
我从头开始做一个网络应用程序。之前我一直在开发已经运行很长时间的应用程序,因此我不必处理完整的设置阶段。我正在使用 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/" />
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
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:
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
in web.xml and
<bean name="/accise" class="it.jsoftware.jacciseweb.controllers.MainController">
</bean>
for the servlet context (plus other of course).
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?
Update solution found!!! :)
The problem was that my servlet-config.xml missed one declaration:
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/" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与带注释的控制器配合良好,但可能需要对其他类型的控制器映射进行一些额外配置。我想在你的情况下,你需要手动声明
BeanNameUrlHandlerMapping
(它通常是默认注册的,但
会覆盖默认值,作为应用其自己的副作用配置):<mvc:resources>
plays well with annotated controllers, but may require some extra configuration with other kinds of controller mappings.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):我也有同样的问题。我能够通过使用
CSS
和 javascript 的
完整路径来解决这个问题,在 Spring 提出一些更好的方法之前,请告诉我这是否可以解决您的问题。
I had the same problem. I was able to fix this by using the full path like for
CSS
and for javascript
Let me know if this solves your problem before Spring comes up with some better approach.
我也有同样的问题。我通过在实际引用资源之前添加 spring 宏来解决这个问题,以便解析 servlet 路径。
它在代码中并不是很好,但是这个解决方案使您独立于应用程序部署在服务器和 servlet 上的实际上下文。通常,您不希望在 url 中提及实际的 servlet。
例如,参考ColdStoneJava的答案,那就是:
您还必须在url中绝对引用它,因此斜杠“/...”是必不可少的。
根据我的经验,这行不通:
干杯。
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.
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.
For instance, refering to the answer of ColdStoneJava, that would be:
You also have to reference it absolute in the url, so the slash '/...' is essential.
This, won't work in my experience:
Cheers.