开发者

get absolute URL with EL

How can I build up an absolute URL using no scriptlets (only EL) to the current server, using th开发者_运维知识库e current protocol, port, application etc?


You can get the base URL up to with the context root with help of JSTL as follows:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<c:set var="baseURL" value="${fn:replace(pageContext.request.requestURL, pageContext.request.requestURI, pageContext.request.contextPath)}" />
...
<link rel="stylesheet" href="${baseURL}/foo.css" />
<script src="${baseURL}/foo.js"></script>
<a href="${baseURL}/foo.jsp">link</a>


Another way is:

http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/thePage.jsp


Yet another way is:

  <c:set var="serverPath" value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}"/>

I'm quite surprised when the GitHub Copilot just suggested the code snippet. ~~ :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜