Accessing the full url, including hostname with js

2020-06-17 04:49发布

<c:url var="myUrl" value="/MyPath/${MyID}"/>

which I then use later (to enable users to copy links) :

<input size="35" disabled value="${myUrl}" />

and it shows

/my-app-name/MyPath/23

however I want it to be

http://myHost/my-app-name/MyPath/23

I can prepend the string sure, but wanted a way to actively get the correct hostname ... ?

2条回答
乱世女痞
2楼-- · 2020-06-17 05:30

You need to prepare it yourself based on HttpServletRequest#getRequestURL() and a little help of JSTL functions:

<c:set var="req" value="${pageContext.request}" />
<c:set var="baseURL" value="${fn:replace(req.requestURL, fn:substring(req.requestURI, 1, fn:length(req.requestURI)), req.contextPath)}" />
...
<c:url var="myUrl" value="${baseURL}/${MyID}"/>
查看更多
疯言疯语
3楼-- · 2020-06-17 05:30

HttpServletRequest object has all the details:

  • getProtocol
  • getServerName
  • getContextPath

so I think you can use:

${request.protocol} :// ${request.serverName} ${request.contextPath} /etc

to build what you want.

查看更多
登录 后发表回答