Java 在 spring 3 中使用 <mvc:resources .../> 会导致所有其他视图停止工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4057529/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:04:40  来源:igfitidea点击:

Using <mvc:resources .../> in spring 3 causes all other views to stop working

javaspring-mvc

提问by David Parks

Simplest example:

最简单的例子:

I have a dispatcher servlet configured to catch everything:

我有一个调度程序 servlet 配置为捕获所有内容:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

I have a simple test controller:

我有一个简单的测试控制器:

@RequestMapping("/index")
@ResponseBody
public String rootTest(){
    return "Main page displayed from TestController";
}

In this test case I am adding (or removing) the following line to dispatcher-servlet.xml:

在此测试用例中,我将以下行添加(或删除)到dispatcher-servlet.xml

<mvc:resources mapping="/public/**" location="/public/"/>

My lofty goal:to serve static content (images, css, js) along with my dynamic content (generated via Velocity within a Jetty servlet container, tied together with the almighty Spring).

我的崇高目标:提供静态内容(图像、css、js)以及我的动态内容(通过 Jetty servlet 容器中的 Velocity 生成,与全能的 Spring 捆绑在一起)。

My Dilema:When I add <mvc:resources .../>I get a 404 for http://localhost/index, but I can serve an image from http://localhost/public/img/42.png. If I remove <mvc:resources .../>then http://localhost/indexworks fine, but of course, how do I serve static content?

我的困境:当我添加时,<mvc:resources .../>我得到 404 for http://localhost/index,但我可以提供来自http://localhost/public/img/42.png的图像。如果我删除<mvc:resources .../>然后http://localhost/index工作正常,但当然,我如何提供静态内容?

Bonus question:Why can I never have my cake and eat it too?

额外问题:为什么我永远不能吃我的蛋糕,也不能吃它?

采纳答案by axtavt

There are 2 problems:

有2个问题:

  1. Never use /*in servlet mapping:

    <servlet-mapping> 
        <servlet-name>dispatcher</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    
  2. <mvc:resources>requires <mvc:annotation-driven>(or explicitly declared handler mappings, etc).

    This happens because DispatcherServletapplies default configuration of handler mappings only when no custom handler mappings found in the context. Since <mvc:resources>adds its own handler mapping, defaults are broken, therefore other handler mappings should be decalred explicitly, either by <mvc:annotation-driven>or manually as beans.

    Also note that <mvc:resources>declares only DefaultAnnotationHandlerMappingand doesn't declare other mappings such as BeanNameUrlHandlerMapping, though they are in defaults of DispatcherServlet. Declare them manually if you need them.

  1. 永远不要/*在 servlet 映射中使用:

    <servlet-mapping> 
        <servlet-name>dispatcher</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    
  2. <mvc:resources>需要<mvc:annotation-driven>(或显式声明的处理程序映射等)。

    发生这种情况是因为DispatcherServlet仅在上下文中找不到自定义处理程序映射时才应用处理程序映射的默认配置。由于<mvc:resources>添加了它自己的处理程序映射,默认值被破坏,因此其他处理程序映射应该被显式地标记<mvc:annotation-driven>为 bean ,或者通过或手动作为 bean。

    另请注意,<mvc:resources>仅声明DefaultAnnotationHandlerMapping而不声明其他映射,例如BeanNameUrlHandlerMapping,尽管它们默认为DispatcherServlet。如果需要,请手动声明它们。

回答by Kyle

I also met this problem before. My situation was I didn't put all the 62 spring framework jars into the lib file(spring-framework-4.1.2.RELEASE edition), it did work. And then I also changed the 3.0.xsd into 2.5 or 3.1 for test, it all worked out. Of course, there are also other factors to affect your result.

我之前也遇到过这个问题。我的情况是我没有将所有 62 个 spring 框架 jar 放入 lib 文件(spring-framework-4.1.2.RELEASE 版)中,它确实有效。然后我也把 3.0.xsd 改成 2.5 或 3.1 进行测试,一切都解决了。当然,还有其他因素会影响您的结果。

回答by shailesh kashyap

add "mvc:annotation-driven" line into view resolvers SpringWeb.XML file. it is work for me

将“mvc:annotation-driven”行添加到视图解析器 SpringWeb.XML 文件中。这对我有用