SpringMVC可以配置为处理所有请求,但是排除静态内容目录? [英] Can SpringMVC be configured to process all requests, but exclude static content directories?

查看:255
本文介绍了SpringMVC可以配置为处理所有请求,但是排除静态内容目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将我的spring应用程序映射为处理所有传入的请求('/ *'),则静态内容的请求返回404。例如,对myhost.com/css/global.css的请求将返回404,即使资源存在,因为Spring拦截了请求。



是将SpringMVC映射到子目录(例如'/ home / '),但在这种情况下,您必须在应用程序中的所有链接中传递此目录。有没有办法将SpringMVC映射到/ / />并从处理中排除一组目录?



我当前的web.xml配置是:

 < servlet> 
< servlet-name> springApp< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< load-on-startup> 2< / load-on-startup>
< / servlet>

< servlet-mapping>
< servlet-name> springApp< / servlet-name>
< url-pattern> / home / *< / url-pattern>
< / servlet-mapping>

Idealy我想要的映射类似如下:

 < servlet-mapping> 
< servlet-name> springApp< / servlet-name>
< url-pattern> / *< / url-pattern>
< exclude> / css / *,/ js / *< / exclude>
< / servlet-mapping>这种类型的事情是可能的吗?



$ b >解决方案

如果你只想用Spring做这个,可能会有些麻烦:


    <您需要使用 SimpleUrlHandlerMapping ,您可以明确指定应映射到控制器的URL模式,或将其扩展为支持css /**\".
  1. 等忽略URL。自己撰写 HttpRequestHandler 实现,基本上由getServletContext()。getRequestDsipatcher()。include()调用来返回所请求的资源。

  2. 您必须将该处理程序注册为defaultHandler上述SimpleUrlHandlerMapping。

完成所有操作后,无法映射到您的控制器的所有请求都将转发到您的 HttpRequestHandler ,并以原样投放。


If I map my spring application to process all incoming requests ('/*'), then requests for static content return 404's. For example, a request for "myhost.com/css/global.css" would return a 404, even though the resource exists as Spring intercepts the request.

The alternative is to map SpringMVC to a subdirectory (for example '/home/'), but in this case, you must pass this directory in all links within the application. Is there a way to map SpringMVC to '/' and exclude a set of directories from processing?

My current web.xml configuration is:

<servlet>
    <servlet-name>springApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springApp</servlet-name>
    <url-pattern>/home/*</url-pattern>
</servlet-mapping>

Idealy I would like to have the mapping be something like the following:

 <servlet-mapping>
    <servlet-name>springApp</servlet-name>
    <url-pattern>/*</url-pattern>
    <exclude>/css/*,/js/*</exclude>
 </servlet-mapping>

Is this type of thing possible?

解决方案

If you want to do this with Spring only, it's possible but a bit messy:

  1. You'll either need to use a SimpleUrlHandlerMapping for which you can explicitly specify URL patterns which should be mapped to controllers OR extend it to support "ignore" URLs like "css/**".
  2. You'll need to write your own HttpRequestHandler implementation that would basically consist of "getServletContext().getRequestDsipatcher().include()" call to return the requested resource as is.
  3. You'll have to register that handler as defaultHandler for the above SimpleUrlHandlerMapping.

Once all that is done, all requests that can't be mapped to your controllers will be forwarded to your HttpRequestHandler and served "as is".

这篇关于SpringMVC可以配置为处理所有请求,但是排除静态内容目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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