Spring MVC配置使能 [英] Spring MVC configuration enable

查看:131
本文介绍了Spring MVC配置使能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从划痕设置项目,目前我正在使用java配置配置 Spring MVC 4.1.5 的步骤。整个应用程序是在tomcat gradle插件上运行的。

I'm setting up a project from scratches, currently I'm on step of configuring Spring MVC 4.1.5 using java config. The whole app is beeing run on tomcat gradle plugin.

有人可以解释我为什么需要对类进行以下调用 DefaultServletHandlerConfigurer 为了使请求映射到我的控制器?

Can someone explain me why I need to make the following call to the class DefaultServletHandlerConfigurer in order to make requests map to my controllers ?

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

没有启用它所有的请求beeing被拒绝,服务器说没有映射

Without enabling it all my requests are beeing rejected and server says there is no mapping for particular request.

我阅读了春天 doc 找出,但说明并不会告诉我太多。

I read spring doc to find out, but the description doesn't tell me too much.


启用转发到默认Servlet。当使用此方法
时,DefaultServletHttpRequestHandler将尝试自动检测
defaultServlet名称。或者,您可以通过enable(String)指定
默认Servlet的名称。

Enable forwarding to the "default" Servlet. When this method is used the DefaultServletHttpRequestHandler will try to auto-detect the "default" Servlet name. Alternatively, you can specify the name of the default Servlet via enable(String).


推荐答案

在Spring MVC部分文档中解释得很好。此处

It's explained very well in the Spring MVC section documentation here.


这允许将DispatcherServlet映射到/从而重写
容器的默认Servlet的映射),同时仍然允许
静态资源请求由容器的缺省
Servlet处理。它配置一个DefaultServletHttpRequestHandler与一个URL
映射/ **和相对于其他URL
映射的最低优先级。

This allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container’s default Servlet), while still allowing static resource requests to be handled by the container’s default Servlet. It configures a DefaultServletHttpRequestHandler with a URL mapping of "/**" and the lowest priority relative to other URL mappings.

将所有请求转发到默认Servlet。
因此,重要的是它保持最后所有
其他URL HandlerMappings的顺序。这将是这种情况,如果你使用
或者如果你正在设置你的
自己的自定义的HandlerMapping实例,一定要设置其顺序
属性的值比
DefaultServletHttpRequestHandler,它是Integer.MAX_VALUE。

This handler will forward all requests to the default Servlet. Therefore it is important that it remains last in the order of all other URL HandlerMappings. That will be the case if you use or alternatively if you are setting up your own customized HandlerMapping instance be sure to set its order property to a value lower than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.

这段代码相当于xml行< ; mvc:default-servlet-handler /> 在通常定义为servletname-servlet.xml的Spring Web MVC特定组件文件中

That piece of code is the equivalent to the xml line <mvc:default-servlet-handler/> in the Spring Web MVC-specific components file defined usually as servletname-servlet.xml

你必须使用该调用为了配置转发到Spring默认Servlet,如果你不使你的服务器将尝试使用自己的servlet处理和正如你解释的,如果你没有任何具体定义它赢了找不到您的请求的任何映射。

You have to use that call in order to configure the forwarding to the Spring default Servlet, if you don't make your server will try to use his own servlet handling and as you explained, if you don't have any specific defined it won't find any mapping for your requests.

这篇关于Spring MVC配置使能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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