简述

项目过程中,有时需要在java中获取项目中静态资源,如图片资源的地址,可以通过System.getProperty(“webapp.root”)来动态获取项目的运行路径。

  • web.xml配置


webAppRootKey
webapp.root

  <!-- 根目录Listener  注意:此配置要配置在-->
<listener>
    <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>

<!-- Spring监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>