Spring Tomcat和静态资源以及mvc:资源 [英] Spring Tomcat and static resources and mvc:resources

查看:144
本文介绍了Spring Tomcat和静态资源以及mvc:资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从头开始做一个网络应用程序。在我一直在处理已经运行很长时间的应用程序之前,所以我没有必要处理完整的设置阶段。我正在使用Spring 3和Tomcat 6,而我正在使用Eclipse 3.6

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

我在提供图像(或其他与控制器响应不同的事情)方面存在很大问题。事实上,我无法找到一种方法将我的图像放在我的jsps中。我的配置适用于:

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> 

在web.xml中

<bean name="/accise" class="it.jsoftware.jacciseweb.controllers.MainController">

</bean>

用于servlet上下文(当然还有其他)。

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/" />

但是如果我在我的servlet-context.xml中插入它,我将能够提供图像,然而控制器accise将无法访问。我误用了还是误解了资源标签?什么是正确的方法?

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-config.xml错过了一个声明:

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/" />

推荐答案

< mvc:resources> 可以使用带注释的控制器,但可能需要对其他类型的控制器映射进行额外配置。

<mvc:resources> plays well with annotated controllers, but may require some extra configuration with other kinds of controller mappings.

我想在你的情况下你需要手动声明 BeanNameUrlHandlerMapping (它通常默认注册,但是< 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):

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

这篇关于Spring Tomcat和静态资源以及mvc:资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆