开发者

Spring application root variable

I access my Spring application right now as follows:

http://localhost:8080/my-app

In my JSPs I'd like to have access to my application root instead of hardcoding hyperlinks to the homepage as <a href="/my-app">. Is there some sort of variable that I can access in my jsps to point to the 开发者_开发技巧application root? Thanks


You can use ${pageContext.request.contextPath}. For example, to get href = "/my-app/somePage", use:

<a href="${pageContext.request.contextPath}/somePage">

However, this approach doesn't support URL rewriting required to maintain sessions in the case of disabled cookies. So, the better way to do it is JSTL's <c:url/> tag:

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
...
<a href="<c:url value = '/somePage' />">


You can access the context root with getContextPath() on the request object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜