四种方式得到部署工程的路径

来源:互联网 发布:Ios11关闭4g网络 编辑:程序博客网 时间:2024/06/06 05:22

http://15.15.20.252/ecsmc-cp/cardCoupon/list
目的得到 /ecsmc-cp 这个路径-项目的名称

jstl+el表达是

<c:set var="ctx" value="${pageContext.request.contextPath}"/>

el表达式写在script中

<script>var basePath = "${pageContext.request.contextPath}";</script>

jsp的scriptlet写在script中

<script>var basePath = <%=request.getContextPath()%></script>

global.js中根据本地的ip地址

var baseUrl = "http://15.15.20.252";//测试环境//  var baseUrl = "http://wap.hn.10086.cn";//var APP_SERVER = baseUrl+"/ecsmc-sp/";var APP_SERVER_CP = baseUrl+"/ecsmc-cp/";var APP_SP_HTML = baseUrl+"/ecsmc-static/static/sp/html";var APP_CP_HTML = baseUrl +"/ecsmc-static/static/cp/html";

el表达式和jsp的<%=xxx%>可以写在script中,还可以写在html的标签中

<img src="${pageContext.request.contextPath}/static/img">...<a href="${pageContext.request.contextPath}/XXX">...

这样看el表达式和<%=xxx%>是等效的。

el表达式和jsp表达式可以这么用,竟然

我理解的el表达式是为了从后台取数据到前台。
jsp的<%=xxx%>可以将内容转化为servlet的out.print(“xxx”)直接在页面上显示xxx的内容
这里写图片描述
这里写图片描述
它们都能用在script和标签里面,并且都能访问隐式变量。

浏览器对象,EL表达式,request对象获取URL的数据

一个总结
http://15.15.20.252/ecsmc-cp/cardCoupon/list
1. 得到 http

location.protocal;${pageContext.request.protocal}request.getScheme();
  1. 得到ip值
location.host;${pageContext.request.localName}request.getServerName();
  1. 得到端口号
location.port;${pageContext.request.localPort }request.getServerPort()
  1. 得到工程的根目录
location.pathname;这个得到的是完整的端口号后面的地址${pageContext.request.contextPath}request.getContextPath();

用request对象得到完整某个工程完整的路径–写在jsp的小脚本中

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

其他

浏览器内置对象location

location.search;返回url中传的参数 cardId=43类似location.assign('new url');跳转到新的urllocation.href;返回完整的url

还是觉得<%=XXXX%>可以跟el表达式有类似的功能真的长见识,之前学到的<%=xxxx%>就是写到的servlet的out.print(“xxx”)写在页面上了,这么一想,如果<%=xxx%>的作用仅仅只是将xxx的内容直接写在页面上,而没有访问servlet内置变量的功能,不如直接
xxx<br>就可以了啊。

el表达式和<%=xx%>都可以放在script给js用也可以放在标签里面。

原创粉丝点击