这些mvc:resources映射标记在Spring MVC应用程序的servlet-context.xml中声明的含义是什么意思? [英] What means these mvc:resources mapping tags declared into the servlet-context.xml of a Spring MVC application?

查看:432
本文介绍了这些mvc:resources映射标记在Spring MVC应用程序的servlet-context.xml中声明的含义是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手,我正在开发Spring MVC应用程序。

I am pretty new in Spring and I am working on a Spring MVC application.

进入 servlet-context.xml 配置文件我找到了这些行:

Into the servlet-context.xml configuration file I found these lines:

<!-- resources exclusions from servlet mapping -->
<mvc:resources mapping="/assets/**" location="classpath:/META-INF/resources/webjars/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:resources mapping="/js/**" location="/js/"/>

评论说来自servlet映射的资源排除。

这些线的确切含义是什么?我知道这个 servlet-context.xml 应该包含处理MVC行为的 DispatcherServlet 配置(这对我来说非常清楚)但是前面的行究竟是什么呢?

What exactly means these lines? I know that this servlet-context.xml should contain the DispatcherServlet configuration that handle the MVC behavior (and it is pretty clear for me) but what exactly does the previous lines?

你能详细解释一下我的意思和以前的语法吗?

Can you explain me in details the meaning and the previous syntax?

推荐答案

如果您考虑将所有请求映射到 DispatcherServlet 的spring mvc配置,则可以将这些请求分类为对静态和动态资源的请求。

If you consider a spring mvc configuration where all the requests are mapped to a DispatcherServlet, you can categorize those requests as requests for static and dynamic resources.

对动态资源的请求与您在控制器方法中编程的内容相匹配,它们是典型框架处理的主题,例如路径匹配,内容协商,验证,绑定,转换,格式化,论证解决。

The requests for dynamic resources are matched by what you program inside your controller methods, and they are the subject of the typical framework processing such as path matching, content negotiation, validation, binding, conversion, formating, argument resolving.

对静态资源的请求是对.js,.css或其他一些未创建的资源的请求,而这些资源已经部署在您的应用程序中。它们不是由程序控制器方法处理,而是由 ResourceHttpRequestHandler ,原因很简单,因为它们与动态请求相比具有完全不同的处理操作集(除了路径匹配)。您可以为给定的映射定义静态文件的位置(这可以是类路径,其他一些webapp文件夹或文件系统),资源的缓存策略,转换(例如css中的modyfing链接,将LESS转换为CSS)

The requests for static resources are the requests for .js, .css, or some other resources that are not getting created rather already exist deployed with your application. They are not handled by the programmatic controller methods rather by the ResourceHttpRequestHandler, simply because they have a completly different set of processing actions comparing to dynamic request (apart from path matching). You can define the location of static files for the given mapping (this can be the classpath, some other webapp folder or a file system), caching strategy for the resource, transformations (such as modyfing links in css, tranforming LESS to CSS)

因此,您不希望servlet处理静态资源,实际上您可以通过这样做获得很多可能性和灵活性,检查例如处理静态网络资源只是静态和动态请求是不同类型操作的主题,并且通过使用 mvc:resources 标记,您可以通过映射指定哪些请求是作为静态请求处理

So its not really that you don't want the static resources to be handled by the servlet, in fact you can get a lot of possibilities and flexibility by doing so, check for example handling static web resources it is just that static and dynamic requests are a subject of different kind of actions, and by using mvc:resources tag, you designate by mappings which requests are to be handled as static requests

这篇关于这些mvc:resources映射标记在Spring MVC应用程序的servlet-context.xml中声明的含义是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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