js,jsp获取项目的绝对路径

一.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博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值