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
Spring Boot, by default, serves content on the root context path (“/”).
Using properties or yaml file The simplest way of configuring Spring Boot Application Context path is to do so using application properties file. The properties defined in application properties or yaml files have the lowest priority. Hence other ways of setting the properties can override them.
Overview. The @Context annotation can be used to inject any of the below instances into an instance field or directly into the resource method as a parameter. The object instances that it can inject are the following: SecurityContext – Security context instance for the current HTTP request.
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' />">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With