Spring MVC Request URLs in JSP
I prefer to use BASE tag:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
Then, all your links can be like:
<a href="admin/listPeople">Go to People List</a>
The c:url
tag will append the context path to your URL. For example:
<c:url value="/admin/listPeople"/>
Alternately, I prefer to use relative URLs as much as possible in my Spring MVC apps as well. So if the page is at /MyApp/index
, the link <a href="admin/listPeople">
will take me to the listPeople
page.
This also works if you are deeper in the URL hierarchy. You can use the ..
to traverse back up a level. So on the page at/MyApp/admin/people/aPerson
, using <a href="../listPeople">
will like back to the list page
You need to prepend context path to your links.
// somewhere on the top of your JSP
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
...
<a href="${contextPath}/admin/listPeople">Go to People List</a>