zoukankan      html  css  js  c++  java
  • jsp获取绝对路径

    在JavaWeb开发中,常使用绝对路径的方式引入javaScript和CSS文件,这样可以避免因为目录变动导致引入文件找不到的情况,常用的做法是:

    一、使用${pageContext.request.contextPath}

      代码” ${pageContext.request.contextPath}”的作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的。

    例如:

    <!--使用绝对路径的方式引入CSS文件-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/themes/default/css/ueditor.css" type="text/css"/>
    <!--使用绝对路径的方式引入JavaScript脚本-->
    <script type="text/javascript" src="${pageContext.request.contextPath}/ueditor1_3_6-gbk-jsp/ueditor.config.js"></script>

    二、使用<%=request.getContextPath()%>和使用${pageContext.request.contextPath}达到同样的效果

    <script type="text/javascript" src="<%=request.getContextPath()%>/ueditor1_3_6-gbk-jsp/ueditor.all.js"></script>

    拓展:如果页面时HTML,如何获取绝对路径?

      ${pageContext.request.contextPath}在HTML中不会产生我们想要的效果,因此我们只能在页面中使用JS的方式获取绝对路径,常用的方法如下:

     function getWebPath() {
        var strFullPath = window.document.location.href;
        var strPath = window.document.location.pathname;
        var pos = strFullPath.indexOf(strPath);
        var prePath = strFullPath.substring(0, pos);
        var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
        return (prePath + postPath);
    }
  • 相关阅读:
    将表中数据生成SQL语句
    遍历页面所有的控件
    MSN不能登陆
    刷新框架页
    JS传参出现乱码
    iframe攻击
    有关于VS调试Javascript的问题
    C#中StringBuilder类的使用
    前瞻XAML
    Asp.Net在SqlServer中的图片存取
  • 原文地址:https://www.cnblogs.com/longqingyang/p/6093463.html
Copyright © 2011-2022 走看看