WebApplicationContext context = (WebApplicationContext) servletConfig
.getServletContext()
.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
webrootDir = context.getResource("/").getFile().getAbsolutePath();
servlet中做法:
getServletContext().getRealPath("/");
本文介绍了在Java Web应用中如何通过servlet上下文获取Web应用的根目录绝对路径。展示了两种常用的方法:getServletContext().getRealPath(/) 和通过WebApplicationContext获取。
163

被折叠的 条评论
为什么被折叠?



