获取webapp路径

1. 如果是Servlet, Action, Controller, 或者 Filter, Listener, 拦截器等相关类时,我们只需要获得 ServletContext,然后通过request.getSession().getServletContext.getRealPath("/")来获取当前应用在服务器上的物理地址;

2.通过配置文件:

在web.xml中的<web-app>节点内加入:

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>plantree.root</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>

然后在普通的Java类中(不是action中),就可以通过System.getProperty("plantree.root")获取了web根目录了。
posted @ 2015-04-14 23:14  nozbwang  阅读(401)  评论(0编辑  收藏  举报