Search code examples
jspspring-mvcspring-bootjstlspring-el

SpringBoot Application: Accessing Common application properties in a JSP page


I'm trying to access a property set in my application.properties in a JSP page. This is for display purposes which sends a user to a different application from our admin page. Its not needed in code at all.

application.properties entry:

frontend.url=http://example.com/some_endpoint

In my JSP file I've tried these variations below.

<spring:eval  expression="('frontend.url')" />
<spring:eval  expression="'frontend.url'" />

These below throw errors

<spring:eval  expression="@getProperty('frontend.url')" />
<spring:eval  expression="@frontend.url" />
<spring:eval  expression="@applicationProperties.getProperty('frontend.url')" />

Is there a specific syntax to use or should I just expose a request attribute from a controller?

@Value("${frontend.url}")
private String urlForDisplayPurposes;

I know the proposed method explained here enter link description here works but I really don't want an extra properties file.


Solution

  • Using @environment.getProperty() should work. For example:

    <spring:eval expression="@environment.getProperty('frontend.url')" var="frontendUrl" />
    <a href="${frontendUrl}">Click</a>
    

    For this to work, you need to include the Spring taglib:

    <%@ taglib prefix="spring" uri="springframework.org/tags" %>