views:

201

answers:

2

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

A: 

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

cletus
+4  A: 

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' />">
axtavt