开发者

How can I create an absolute url in a jsp?

If a have <c:url value="/article"/> in a jsp, I actually w开发者_运维技巧ant it to produce http://mysite.com/context/article. Is there any simple way to acheive this?


There's no simple way. Either hardcode it or output the following:

${fn:replace(pageContext.request.requestURL, pageContext.request.requestURI, '')}${pageContext.request.contextPath}

Cumbersome, but there's no shorter/nicer way when you want to take the protocol and port parts of the URL correctly into account. You can at highest assign ${pageContext.request} to ${r}.

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

so that you can end up with this

${fn:replace(r.requestURL, r.requestURI, '')}${r.contextPath}

That said, I only fail to see how this requirement is useful/valuable. I always code my webapp-specific links to be relative to the current context or to the HTML <base> tag. Otherwise you'll have to a lot of maintenance when your domain, port and/or even the context changes. Why this requirement?

See also:

  • Is it recommended to use the HTML <base> tag? (sensitive subject actually)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜