Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 645
oym Avatar asked Feb 04 '10 01:02

oym


People also ask

What is the root directory for Spring boot application?

Spring Boot, by default, serves content on the root context path (“/”).

How context root can be changed in Spring boot?

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.

What is @context annotation in Spring boot?

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.


1 Answers

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' />">
like image 98
axtavt Avatar answered Oct 05 '22 04:10

axtavt