美文网首页
JS中怎么获得项目的根目录名

JS中怎么获得项目的根目录名

作者: 浊水i | 来源:发表于2017-12-06 17:16 被阅读0次

    一、在当前JSP中的java代码中获取

    java代码

    
    String path = request.getContextPath();
    
    String basePath = request.getScheme() +"://"
    
    + request.getServerName() +":"+ request.getServerPort()
    
    + path +"/";
    
    

    再在JS中调用:

    
    var basePath ='<%=basePath%>';
    
    alert(basePath);//http://localhost:8080/
    
    

    二、在跳入当前页面的action中获取根路径,放入request中,再在JS中获取

    
    String path = request.getContextPath();
    
    String basePath = request.getScheme() +"://"
    
    + request.getServerName() +":"+ request.getServerPort()
    
    + path +"/";
    
    request.setAttribute("basePath", basePath);
    
    

    再在JS中通过EL表达式调用:

    
    var basePath = ${'basePath'};
    
    alert(basePath);//http://localhost:8080/
    
    

    相关文章

      网友评论

          本文标题:JS中怎么获得项目的根目录名

          本文链接:https://www.haomeiwen.com/subject/mbjlixtx.html