web工程常用路径的获取方法

此文章是基于  搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

 

一. 利用 Spring 取得web工程根路径

  1. web.xml 中添加如下:

<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>webapp.root</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
View Code

  

  2. Java中通过如下代码取得根路径:

private static String webrootPath = System.getProperty("webapp.root");

 

二. Java取得当前类的class路径

String classPath = this.getClass().getResource("").getPath();

 

posted on 2016-12-08 17:34  大饼酥  阅读(615)  评论(0编辑  收藏  举报

导航