jsp中的js中获取项目路径的方法

在jsp中加上

 

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

 

然后在jsp中的其他地方都可以通过  <%=bathPath%> 的方式来引用

 


 在js中直接定义成变量使用:

<script>

  $(function(){
     var rootPath = "<%=bathPath%>";
        alert(rootPath );
  }
           
</script>

注意:要有 等号:

       var rootPath = "<%=bathPath%>";

 

其他使用方式:

<!-- 在各种src/href中使用 -->
<script type="text/javascript" src="<%=path%>/js/echarts.js"></script>

<!-- 在隐藏域中使用 --> <input type="hidden" id="rootPath" value="<%=path%>">

<!-- 在a标签中使用 --> <a href="<%=path%>/trend_downTrendReport.action?discussForm.id=${discussFrom.id}">导出报告按钮</a>

 

  

 

posted @ 2018-04-25 14:15  戈博折刀  阅读(1654)  评论(0编辑  收藏  举报