call environment from context.xml

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

call environment from context.xml

Giulio
Hello everyone.

I have a web application that use spring and thymeleaf.
In the file 'context.xml' I entered the following properties:

<Environment name="my/path" type="java.lang.String" value="myValue" />

Now I would like to call this property from a web page to parameterize loading a css file, for example:

<link rel="stylesheet" th:href="@{/themes/XXX/css/myCss.css}" />

How do I replace 'XXX' with my environment value?

I was clear? is it possible?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: call environment from context.xml

Zemi
Administrator
Hello,

I'm afraid that there is no specific Thymeleaf support for this "environment" variables.

You should set them into the Model in your controller.

   javax.naming.Context ctx = new javax.naming.InitialContext();
   javax.naming.Context env = ctx.lookup("java:comp/env");
   String cssPath = (String) env.lookup("my/path");
   model.setAttribute("cssPath", cssPath);

(use context.setAttribute() if you're not using Spring).

And in your template:

   <link rel="stylesheet" th:href="@{/themes/__${cssPath}__/css/myCss.css}" />

Reply | Threaded
Open this post in threaded view
|

Re: call environment from context.xml

Giulio
Zemi, is the same solution that I thought, but I did not want to use it because the times when I call the controller are many, and I did not want to make a drastic change on the controller.
Reply | Threaded
Open this post in threaded view
|

Re: call environment from context.xml

danielfernandez
Administrator

If you are using Spring, you can add a bean at the view resolver's staticVariables property that retrieves your environment property.

These static properties will be always added to the context by Spring without the need of you doing it manually at the controller level.

Regards,
Daniel

Reply | Threaded
Open this post in threaded view
|

Re: call environment from context.xml

blandger
In reply to this post by Giulio
Does that work with JS src?

I have code in controller:
model.addAttribute("googleApiKey", environment.getProperty("google.api.key"));

I try to include it into js src like:
<script th:src="@{https://maps.googleapis.com/maps/api/js?key=${googleApiKey}&amp;sensor=false}" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

but I'm get error:
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "@{https://maps.googleapis.com/maps/api/js?key=${googleApiKey}&sensor=false}" (/index.html:63)
        org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:187)

Code:
    <script th:src="@{https://maps.googleapis.com/maps/api/js?key=__${googleApiKey}__&amp;sensor=false}" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
doesn't work either.

What do I do wrong?
Reply | Threaded
Open this post in threaded view
|

Re: call environment from context.xml

Zemi
Administrator
Use the following syntax for parameters:

   th:src="@{https://maps.googleapis.com/maps/api/js(key=${googleApiKey}, sensor='false')}"
Reply | Threaded
Open this post in threaded view
|

Re: call environment from context.xml

blandger
Hm.... I don't understand that, but that works.