项目中静态资源的路径处理

1、绝对路径:不可改变的路径

  • 本地绝对路径:增加盘符的路径(e:/test/test.html)
  • 网络绝对路径:增加协议,IP地址,端口号的路径(http://localhost:8080/test/test.html)

2、相对路径

  • 可以改变的路径,但是以基准路径为参考,查找其他路径,默认情况下,相对路径的基准路径是以当前资源的访问路径为基准。路径以斜杠开头,表示的特殊的相对路径,在不同的场景中,相对的位置会发生变化。
    例如:
url : http://localhost:8080/web/test/test.html
	前台路径:`<a href=”/sssss”><img src=””>`
	相对服务器的根 : `http://localhost:8080/sssss`
	后台路径:forward(”/user.jsp”)
	相对web应用的根:http://localhost:8080/web/user.jsp

在这里插入图片描述
当然了,也可以使用pageContext.request.contextPath代替
编写ServerStartupListener

public class ServerStartupListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        String contextPath = servletContext.getContextPath();
        servletContext.setAttribute("APP_PATH", contextPath);
    }
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}

配置在web.xml中
在这里插入图片描述

发布了14 篇原创文章 · 获赞 0 · 访问量 221

猜你喜欢

转载自blog.csdn.net/qq_36609994/article/details/104384417