日期:2014-05-19  浏览次数:20891 次

SSH2里怎么获取项目的绝对路径
dir=System.getProperty("user.dir");的结果是F:\java\JDK\eclipse
this.getClass().getResource("/").getPath();的结果是/E:/eclipse%20Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/test/WEB-INF/classes/
相对来说第二句准确一点,第一完全不着边了,但是我想要项目的路径不知道怎么弄

------解决方案--------------------
JSP页面获取路径:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

Class:
Java code

//getRealPath里面可选参数
System.out.println("项目的绝对路径:"+request.getSession().getServletContext().getRealPath(""));
System.out.println("相对路径:"+request.getRequestURI());

------解决方案--------------------
public class Test {
public void test(){
System.out.println(this.getClass().getClassLoader().getResource("").getPath());
}
public static void main(String[] args) {
Test test = new Test();
test.test();
}
}

this.getClass().getClassLoader().getResource("").getPath()使用servlet,jsp
------解决方案--------------------
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

这个就是项目的绝对路径,一般在Jsp页面中使用它来作为基准路径basePath
------解决方案--------------------
学习了,一直没那样写过