在js中获取request.contextPath项目路径
2018-01-03 00:00
351 查看
第一种方法:
在jsp 中加入java代码
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
在JS中调用var basePath ='<%basePath%>'
或者
$.ajax({
type:"POST",
url:"<%=request.getContextPath()%>/xxxx",
data:{},
async: false,
dataType: "json",
contentType:'application/json;charset=UTF-8',
success:function(result){
}
})
第二种方法:
采用EL表达式
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
request.setAttribute("basePath", basePath);
在jsp 的script中 var basePath="${basePath}";
注意大括号两边的双(单)引号不能少。
第三种方法:
在单独的js文件中
function getContextPath() {
var pathName = document.location.pathname;
var index = pathName.substr(1).indexOf("/");
var result = pathName.substr(0,index+1);
return result;
}
在jsp 中加入java代码
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
在JS中调用var basePath ='<%basePath%>'
或者
$.ajax({
type:"POST",
url:"<%=request.getContextPath()%>/xxxx",
data:{},
async: false,
dataType: "json",
contentType:'application/json;charset=UTF-8',
success:function(result){
}
})
第二种方法:
采用EL表达式
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
request.setAttribute("basePath", basePath);
在jsp 的script中 var basePath="${basePath}";
注意大括号两边的双(单)引号不能少。
第三种方法:
在单独的js文件中
function getContextPath() {
var pathName = document.location.pathname;
var index = pathName.substr(1).indexOf("/");
var result = pathName.substr(0,index+1);
return result;
}
相关文章推荐
- ${pageContext.request.contextPath}|request.getContextPath() 项目路径怎么在js(jquery)中获得
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- js中获取 <%=request.getContextPath()%>
- 转载【js中获取 <%=request.getContextPath()%>】
- request.getContextPath()获取的是什么路径?
- String path = request.getContextPath();获取应用的上下文路径。
- thymeleaf 获取项目路径 contextPath
- js文件中获取${pageContext.request.contextPath}
- ###request获取请求路径内容的相关方法总结。例如:request.getContextPath()。。。
- 纯js文件中获取${pageContext.request.contextPath}
- [js]js中获取 <%=request.getContextPath()%>
- request.getContextPath获取绝对路径
- 通过request.getContextPath获取绝对路径
- 项目路径配置[pageContext.request.contextPath]和c标签
- 获取当前路径 ${pageContext.request.contextPath}
- js中获取 <%=request.getContextPath()%>
- ${pageContext.request.contextPath} jsp中获取绝对路径