获取项目的绝对路径

原文
在 Java 代码常用代码:

<%
 String appContext = request.getContextPath();
 String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + appContext; 
%>

在thymeleaf 获取项目的绝对路径:

<a class="hide" th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #httpServletRequest.getServerPort()  + #httpServletRequest.getContextPath() + '/'} "
   id="contextPath"></a>

<script>
     var projectPath = $('#contextPath').attr('href');
</script>

request.getContextPath():是在开发Web项目时,经常用到的方法,是为了解决相对路径的问题,可返回站点的根路径

比如:要生成一个文件放在服务器上得一个目录下,可以使用request.getContextPath()+/dir,组成一个完整得目录结构!

当使用Tomcat作为Web服务器,项目一般部署在Tomcat下的webapps的目录下。具体来说主要用两种部署的路径:

(1)将web项目中的webRoot下的文件直接拷贝到webapps/ROOT下(删除ROOT下的原有文件);

(2)在Tomcat下的webapps中创建以项目名称命名(当然也可以用其他的名称)的文件夹,并将webRoot下的文件直接拷贝到该文件夹下。

对于第一部署方法,request.getContextPath()的返回值为空(即:”“,中间无空格,注意区分null)。

对于第二部署方法,其返回值为:/创建的文件夹的名称。

假定你的web application 名称为news,你在浏览器中输入请求路径:

http://localhost:8080/news/main/list.jsp

则执行下面向行代码后打印出如下结果:

1、 System.out.println(request.getContextPath());

打印结果:/news

 2、System.out.println(request.getServletPath());

打印结果:/main/list.jsp

3、 System.out.println(request.getRequestURI());

打印结果:/news/main/list.jsp

4、 System.out.println(request.getRealPath(“/”));

打印结果:F:\Tomcat 6.0\webapps\news\test

request.getContextPath()可以返回当前页面所在的应用的名字;

request.getSchema()可以返回当前页面使用的协议,http 或是 https;

request.getServerName()可以返回当前页面所在的服务器的名字;

request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80;

实际应用中,一般用来解决jsp测试和生产环境路径不同的问题: 

<%
 String appContext = request.getContextPath();
 String basePath =request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort() + appContext; 
%>

            </div>

request.getContextPath():是在开发Web项目时,经常用到的方法,是为了解决相对路径的问题,可返回站点的根路径

比如:要生成一个文件放在服务器上得一个目录下,可以使用request.getContextPath()+/dir,组成一个完整得目录结构!

当使用Tomcat作为Web服务器,项目一般部署在Tomcat下的webapps的目录下。具体来说主要用两种部署的路径:

(1)将web项目中的webRoot下的文件直接拷贝到webapps/ROOT下(删除ROOT下的原有文件);

(2)在Tomcat下的webapps中创建以项目名称命名(当然也可以用其他的名称)的文件夹,并将webRoot下的文件直接拷贝到该文件夹下。

对于第一部署方法,request.getContextPath()的返回值为空(即:”“,中间无空格,注意区分null)。

对于第二部署方法,其返回值为:/创建的文件夹的名称。

假定你的web application 名称为news,你在浏览器中输入请求路径:

http://localhost:8080/news/main/list.jsp

则执行下面向行代码后打印出如下结果:

1、 System.out.println(request.getContextPath());

打印结果:/news

 2、System.out.println(request.getServletPath());

打印结果:/main/list.jsp

3、 System.out.println(request.getRequestURI());

打印结果:/news/main/list.jsp

4、 System.out.println(request.getRealPath(“/”));

打印结果:F:\Tomcat 6.0\webapps\news\test

request.getContextPath()可以返回当前页面所在的应用的名字;

request.getSchema()可以返回当前页面使用的协议,http 或是 https;

request.getServerName()可以返回当前页面所在的服务器的名字;

request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80;

实际应用中,一般用来解决jsp测试和生产环境路径不同的问题: 

<%
 String appContext = request.getContextPath();
 String basePath =request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort() + appContext; 
%>

            </div>

猜你喜欢

转载自blog.csdn.net/qq_37155959/article/details/81664638