如何使更具体的urlPattern覆盖带有注释的更广泛的模式 [英] How to make more specific urlPattern override broader pattern with annotations

查看:48
本文介绍了如何使更具体的urlPattern覆盖带有注释的更广泛的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个带有以下注释的主控制器servlet:

I currently have a main controller servlet with the following annotation:

@WebServlet(name="ControllerServlet", urlPatterns={"/", "/home"})

在我的项目中,我的.jsp文件引用的/resources目录下有.js和.css文件,并且链接已发送到不需要的主控制器.我制作了第二个servlet,以处理带有nn批注的.js .css文件:

I have .js and .css files under the /resources dir in my project that my .jsp files reference and the links are getting sent to my main controller which I don't want. I made a second servlet to handle the .js .css files with nn annotation of:

@WevServlet(name="ResourceServlet", urlPatterns={"/resources"})

希望它可以接收来自我的.jsp文件的请求,但ControllerServlet仍可以接收它们.如何使/resources网址定向到我的ResourceServlet?

hoping that it would pick up the requests coming from my .jsp files but ControllerServlet is still picking them up. How can I make /resources urls get directed to my ResourceServlet?

推荐答案

URL模式/与其他映射不匹配的任何内容匹配.如果有

The url pattern / matches anything that isn't matched by the other mappings. If you have

@WebServlet(name="ResourceServlet", urlPatterns={"/resources"})

然后请求,例如localhost:8080/context/resources将由您的ResourceServlet处理,但不会发送给localhost:8080/context/resources/somescript.js的请求,因为它与ResourceServlet的网址格式不匹配.因此,ControllerServlet将对其进行处理.

then a request to, eg. localhost:8080/context/resources will be handled by your ResourceServlet, but a request to localhost:8080/context/resources/somescript.js won't because it doesn't match ResourceServlet's url pattern. Therefore, ControllerServlet will handle it.

您需要将ResourceServlet映射更改为

@WebServlet(name="ResourceServlet", urlPatterns={"/resources/*"})

这篇关于如何使更具体的urlPattern覆盖带有注释的更广泛的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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