SpringMVC零碎笔记

在web.xml里可以配置webapp的默认首页,格式如下:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

这里要注意,如果首页是放在webapp根目录下的index.jsp则不用配置<welcome-file>默认通过http://ip:port/webapp/访问的就是index.jsp文件,

但是如果是index.html是主页则首先要在springmvc.xml里配置

<mvc:resources mapping="/index.html" location="/index.html"/>

因为.html是静态文件会被springMVC拦截(index.jsp不被拦截是因为我的url-pattern值是/,而JspServlet的url-pattern值是.jsp和.jspx故此后缀的被Tomcat转发给了JspServlet处理,而且JspServlet会getSession()),之后还要在web.xml里配置为上面的<welcome-file>..,且index.html前面不能有/,否则

只能通过http://ip:port/webapp/index.html访问index.html页面(不过第一次由IDE弹出的页面这种http://ip:port/webapp/形式可以显示index.html,但是刷新后就没了),

而不能通过http://ip:port/webapp/访问index.html页面,即下面的配置某种意义上说是错误的(不过我觉得这该叫bug吧)

<welcome-file-list>
    <welcome-file>/index.html</welcome-file>
</welcome-file-list>

// seperator
index.html文件也可以放在WEB-INF目录下,然后写一个IndexController路由到index.html,这么做的好处之一是如果有黑名单功能可以令黑名单中的“人”连主页也无法访问。
原文地址:https://www.cnblogs.com/silentdoer/p/7452920.html