目录
一、在springMVC配置文件中:/,/*,/**三者的关系。
(1)/:拦截所有路径和静态资源(动态资源如.jsp资源就不拦截)。
一、在springMVC配置文件中:/,/*,/**三者的关系。
1. /:精确匹配路径,只能匹配一个路径。
2. /*:通配符匹配,可以匹配一个路径及其下层所有子路径。例如,/user/* 可以匹配/user/list和/user/info等。
3. /**:匹配任何路径及其下层所有子路径。例如,/user/** 可以匹配/user/list、/user/info或者/user/hobby/sports等。
总体来说,使用/会更高效,并且只会映射精确匹配的URL;而\*和\*\*则可以实现更灵活的URL映射。根据具体情况选择合适的方式来进行URL映射。
二、在web.xml与注解中:/,/*,/**三者的关系。
//@WebServlet("/")
//@WebServlet("/*")
//@WebServlet("/**")
//@WebServlet("/user/")
//@WebServlet("/user/*")
@WebServlet("/user/**")
public class Demo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("成功访问get!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("成功访问post!");
}
}
笔记:以上servlet类就可以测试,不信者可自行测试。
(1)/:拦截所有路径和静态资源(动态资源如.jsp资源就不拦截)。
(1.1)/:映射地址。
/:映射地址。开启服务器时如果有index.html,也会被servlet类拦截执行三次,index.jsp就不会被拦截执行,下面没有标注的都是被servlet类拦截执行 * http://localhost:8044/users:拦截成功,执行servlet类。 * http://localhost:8044/userss/sfjdo:拦截成功,执行servlet类。 * http://localhost:8044/userss/sfjdo/sifdo:拦截成功,执行servlet类。 * http://localhost:8044/userss/sfjdo/sifdo.html:拦截成功,执行servlet类。 * http://localhost:8044/userss/sfjdo/sifdo.js:拦截成功,执行servlet类。 * http://localhost:8044/userss/sfjdo/sifdo.do:拦截成功,执行servlet类。 * http://localhost:8044/userss/sfjdo/sifdo.dosfdfsodfdjsoe/sjofd:拦截成功。 * http://localhost:8044/userss/sfjdo/sifdo.jsp:访问失败,因为里面没有这个动态资源 * http://localhost:8044/quick.html:访问静态资源,也会被servlet类拦截执行 * http://localhost:8044/quick.jsp:成功访问动态资源,没有被servlet类拦截 * 所以静态资源的访问权限需要springmvc开放,而动态资源直接就能访问(没有被springmvc拦截)
(1.2)/user/:映射地址。
* /user/:映射地址。 * http://localhost:8044/user/quick:拦截失败 * http://localhost:8044/user:拦截失败 * http://localhost:8044/user/a.jsp:拦截失败 * http://localhost:8044/user/a.html:拦截失败* http://localhost:8044/user/:成功被拦截执行
(2)/*:拦截所有路径和静态资源、动态资源。
(1)/*:映射地址。(包含/路径)
/*:映射地址。服务器开启时跳转的首页也被servlet类拦截执行3次,即开启服务器就执行servlet类3次(index.html或index.jsp都会被拦截) * 下面都是可以被servlet类拦截成功并执行的访问路径: * http://localhost:8044/sser * http://localhost:8044/sser/sjfod * http://localhost:8044/sser/sjfod/fjdofdfjdo * http://localhost:8044/sser/sjfod/fjdofdfjdo/sf.do * http://localhost:8044/sser/sjfod/fjdofdfjdo/sf.html * http://localhost:8044/sser/sjfod/fjdofdfjdo/sf.jsp * http://localhost:8044/index.jsp:有这个资源,但是也被servlet类拦截执行 * http://localhost:8044/index.html:有这个资源,但是也被servlet类拦截执行
(2) /user/*:映射地址。(包含/user路径)
/user/*:映射地址。 * http://localhost:8044/user:拦截成功 * http://localhost:8044/user/:拦截成功 * http://localhost:8044/user/sdfdo:拦截成功 * http://localhost:8044/user/sdfdo/sa.so:拦截成功 * http://localhost:8044/user/sdfdo/sa.so/s.jsp:拦截成功 * http://localhost:8044/user/sdfdo/sa.so/s.html:拦截成功
(3)/**: 全部都不拦截(有**出现,这路径就废了)。
(1)/**:映射地址。
/**:映射地址。没有任何意义的映射地址。 * http://localhost:8044/(index.html):没有被拦截成功 * http://localhost:8044/index.jsp:没有被拦截成功 * http://localhost:8044/inde/sfodf:访问失败,没有这个文件或路径 * http://localhost:8044/inde/sfodf/sjfdof:访问失败,没有这个文件或路径 * http://localhost:8044/inde/sfodf/sjfdof.do:访问失败,没有这个文件或路径 * http://localhost:8044/inde/sfodf/sjfdof.html:访问失败,没有这个文件或路径 * http://localhost:8044/inde/sfodf/sjfdof.jsp:访问失败,没有这个文件或路径
(2)/user/**:映射地址。
/user/**:映射地址。没有任何意义的映射地址。 * http://localhost:8044/user/quick:访问失败,没有这个文件或路径 * http://localhost:8044/user/quick.do:访问失败,没有这个文件或路径 * http://localhost:8044/user/quick.html:访问失败,没有这个文件或路径 * http://localhost:8044/user/quick.jsp:访问失败,没有这个文件或路径 * http://localhost:8044/user:访问失败,没有这个文件或路径
注意:拦截成功之后,就会去执行该servlet类(即便是访问的是index.html页面,也不会访问成功) 。
注意:tomcat服务器开启之后,就会默认返回(访问)index.html(比jsp优先级高)或 index.jsp页面,所以当这个/index.html或者/index.jsp路径就会有被拦截的可能。
三、/与/*的优先级。


同时存在这两个,会只执行一个,我的是注解执行。 (/*应该是会覆盖/)
本文详细解析了SpringMVC框架中/、/*、/**三种URL映射的区别及应用场景,包括它们在配置文件和web.xml中的具体表现,并探讨了优先级问题。

被折叠的 条评论
为什么被折叠?



