ky与你
2016-05-13 05:56
采纳率: 90%
浏览 4.2k
已采纳

为什么DispatcherServlet设置成/的时候会拦截.html却不会拦截.jsp

为什么springMVC拦截设成/的时候会拦截.html文件

webapp下有
index.jsp
index.html
web-inf

直接访问项目跳转至webapp/index.jsp

 <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

直接访问项目跳转至404

 <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>

直接访问项目跳转至webapp/index.jsp

 <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

直接访问项目跳转至webapp/index.html

 <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • ky与你 2016-05-13 06:04
    已采纳

    设置成/的话 除了jsp不拦截 其他的都拦截,为什么不会拦截jsp呢?

    点赞 打赏 评论
  • 爱代码的小呆 2019-12-25 21:04
    he / doesn't override any other servlet. It only replaces the servletcontainer's builtin default servlet for all requests which doesn't match any other registered servlet. This is normally only invoked on static resources (CSS/JS/image/etc) and directory listings. The servletcontainer's builtin default servlet is also capable of dealing with HTTP cache requests, media (audio/video) streaming and file download resumes. Usually, you don't want to override the default servlet as you would otherwise have to take care of all its tasks, which is not exactly trivial (JSF utility library OmniFaces has an open source example). This is thus also a bad URL pattern for servlets. As to why JSP pages doesn't hit this servlet, it's because the servletcontainer's builtin JSP servlet will be invoked, which is already by default mapped on the more specific URL pattern *.jsp.
    

    意思是< url-pattern > / </ url-pattern >不重写任何其他servlet。它只为所有不匹配任何其他注册的servlet的请求替换servletcontainer的内置默认servlet(此时在springmvc.xml中是前端控制器DispatcherServlet)
    至于为什么JSP页面没有命中这个servlet,这是因为将调用servletcontainer的内置jspservlet,它在默认情况下已经映射到更具体的URL模式*.JSP上。也就是为什么< url-pattern > / </ url-pattern > 不会匹配到模式为*.jsp这样的后缀型url

    点赞 2 打赏 评论

相关推荐 更多相似问题