一.js文件中获取
function getPathRootJump(){
var pathName = window.location.pathname.substring(1);
var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
var path_root = window.location.protocol + '//' + window.location.host + '/'+ webName + '/';
return path_root;
}
在js文件中,调用以上函数,就能获取例如:http://localhost:8088/ems的路径,其中ems是项目名。
使用的话:形如以下对方法进行调用
var urlPath = getPathRootJump()+"address/rtm";
//console.log("urlPath:"+urlPath); urlPath: http://localhost:8088/ems/address/rtm
$.ajax({
type: "post",
//url: " http://localhost:8088/ems/address/rtm",
url: urlPath,
dataType: "text",
contentType: "application/json;charset=utf-8",
data:JSON.stringify(rtm),
processData : false,
success:function (msg) {
alert("success!!!");
/*ipc.send('close-new-window');*/
},
error : function(jqXHR, textStatus, errorThrown) {
}
});
二.jsp文件中获取
在JSP中我们表示绝对路径可以使用EL表达式:${pageContext.request.contextPath}
这种方式是调用隐式的pageContext对象,然后在得到HttpServletRequest对象,最后再拿到contextPath。
该方式是以属性的形式出现。
同样可以采用JSP的形式来表示:<%=request.getContextPath()%>
该方式调用的jsp的内置的HttpServletRequest对象,此时用点是点不出来的,需要使用get方式的形式。
调用方式:
例如:
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/common.css" />
文章参考
关于request.getServletPath(),request.getContextPath()的总结_jwnba24的博客-CSDN博客