spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们能够完成一些特定需求的功能; WebAppRootListener 能够将 Web 应用根目录添加到系统参数中,对应的属性名能够经过名为“webAppRootKey”的 Servlet 上下文参数指定java
web.xml文件中 <context-param> <param-name>webAppRootKey</param-name> <param-value>web.root</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.WebAppRootListener</listener-class> </listener>
1) 在java代码中使用String rootPath = System.getProperty("web.root")获取web项目根目录web
String path = System.getProperty("web.root")+"/productImages";
2) 在配置文件resource.properties中使用spring
更常见的使用场景是在配置文件中经过 ${web.roott} 引用 Web 应用的根目录spa
#存放商品图片目录 PRODUCT_IMAGES_PATH=${web.root}/productImages
若是有不正确, 请指出, 谢谢.net