Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 957
Gilberto Silva Avatar asked May 19 '16 07:05

Gilberto Silva


People also ask

How to develop a JSP web application using Spring Boot?

For a Spring Boot JSP web application, we will need the following dependencies on the pom.xml file spring-boot-starter-web provides all the dependencies and auto-configuration we need to develop a web application in Spring Boot, including the Tomcat embedded servlet container

What are the application properties in Spring Boot?

Inside the application properties file, we define every type of property like changing the port, database connectivity, connection to the eureka server, and many more. Now let’s see some examples for better understanding. Sometimes when you run your spring application you may encounter the following type of error

What happens when a Spring Boot application is launched from a jar?

In the production environment, when a Spring Boot application is launched from a jar file, the devtools is auto disabled Create a Spring Boot controller file to map HTTP requests to JSP view files

How to map http request to JSP view file in Spring Boot?

Create a Spring Boot controller file to map HTTP requests to JSP view files The @Controller annotation indicates the annotated class is a web controller @GetMapping maps HTTP GET request for "/" (home page) and "/hello" to the hello method


2 Answers

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

<spring:eval expression="@environment.getProperty('frontend.url')" var="frontendUrl" />
<a href="${frontendUrl}">Click</a>
like image 74
g00glen00b Avatar answered Oct 11 '22 15:10

g00glen00b


<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:eval expression="@environment.getProperty('myvar')" var="frontendUrl" />

and you can use "frontendUrl" in jsp

like image 25
Cesar David Guevra Mejia Avatar answered Oct 11 '22 14:10

Cesar David Guevra Mejia