how thymeleaf can find the template location? - spring

i'm getting a warn when i start my spring boot app, is the following:
2023-02-16T13:26:47.508-05:00 WARN 23919
--- [ restartedMain] DefaultTemplateResolverConfiguration
Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
but my application.properties is the following:
spring.thymeleaf.prefix=classpath:/templates/
everything seems to be normal, but I keep getting that error
my resources directory is the following:
enter image description here
does anyone know what's going on? the answers I've looked for don't fit my weird situation.

First of all src/main/resources/templates is the default directory for Thymeleaf templates, so I guess you need to remove property spring.thymeleaf.prefix=classpath:/templates/ letting Spring to use the default one.

Related

Cannot find template location :( please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)

I created a spring boot application and while running it I am getting this warning.
2022-10-27 00:35:12.520 WARN 11512 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
Here, you can see my application.properties file
spring.datasource.url=jdbc:mysql://localhost:3306/springbootblogapp?
createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
Even though there were some related questions here, the provided answers for them are not working for me, because the warnings mentioned there were a little bit deviated from the warning which I got.
Please help me.
My initial comment, transferred to an answer:
Here is the text of the warning message shown in your question:
2022-10-27 00:35:12.520 WARN 11512 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
Did you try what the warning message suggests - for example, by adding:
spring.thymeleaf.check-template-location=false
to your application's config? Or by creating the directory templates mentioned in the warning message (and adding a template there)? By default this directory is expected to be created in the resources location, so that it is on the classpath of the application at runtime.
Update based on your follow-up comment:
Why do you get this warning?
When you create a Spring Boot application, you can choose various additional dependencies (for example by using Spring Initializr).
One such additional dependency is Thymeleaf.
For Maven (assuming you are using Maven), that is the following artifact in your POM:
<artifactId>spring-boot-starter-thymeleaf</artifactId>
There is no point in choosing this option if you don't set up the basic location where you are going to store your Thymeleaf templates.
If you choose to configure the option:
spring.thymeleaf.check-template-location=false
then you are telling Spring not to bother checking if there is a valid configured location for your Thymeleaf templates.
So, even if there is not a valid configured location, you won't see that warning.
If you decide you do want to use Thymeleaf after all, you will need to fix this, by providing that default folder templates on the runtime classpath - or by specifying a custom location via your properties file.
You may see similar warnings (or even errors) with other Spring Boot dependencies you have chosen. For example, if you choose to include the JDBC API, but do not configure a valid data source, then I believe that causes an error (if I recall).

How to access application.properties from freemarker template

In a freemarker template, we can access spring messages.properties like so
messages.properties:
...
my.message=foo
...
example.ftl:
<#spring.message 'my.messsage'/>
output:
foo
is there a way to do the same for properties defined in application.properties and other active profiles? Trying to access the same way has yielded the follwing error:
No message found under code 'my.prop' for locale 'xx_XX'
Edit:
I have seen some examples of doing so with extra configurations to dependencies, but I was looking for an easier way as those suggestions would severely impact current code
Yes you just prefix them with their reference or otherwise....
So instead of my.message, it might be app.someProperty

How to load corectly image in thymeleaf (spring boot project)?

I want to display the image after an error has occurred in my spring boot application. I read the documentation and tried several ways and it still doesn't work.
I read the documentation and tried several ways and it still doesn't work.
I try this:
<img th:src="#{/images/error_image.jpg}"/>
and this:
<img src="../static/images/error_image.jpg" th:width="1000" th:src="#{/images/error_image.jpg}"/>
This is my project structure:
project structure
Hovering over a photo with ctrl directs me to a photo, so the path is good.
link
but all the time I see this:
error
[EDIT]
The issue is with below controller method, you should provide mapping below.
#GetMapping // add mapping here
public String showSignUpForm()
{
return "register";
}
Make sure static resource is not configured to different path other static/ at your end.
If you have below configuration in application.properties, remove it.
spring.resources.static-locations=
Make sure you have below configurations in application.properties, spring boot's auto-configuration will set it to resource/static/
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
It's not an issue but still, change /images/ to images/
<img src="../static/images/error_image.jpg" th:width="1000" th:src="#{images/error_image.jpg}"/>

Externalizing configuration for Hibernate Search

I am running hibernate search with spring boot. I have written a working configuration for my application. How ever, i want to externalize my configuration and use ./config/hibernate.properties instead of src/main/resources/hibernate.properties. After copying my properties file to the desired location, i am getting and exception:
nested exception is java.io.FileNotFoundException: class path resource [hibernate.properties] cannot be opened because it does not exist
Anyone with any idea on how i should tell spring to read my configuration file?
Move your configuration to an src/main/resources/application.properties file and prepend spring.jpa.properties. everywhere, so hibernate.dialect will become spring.jpa.properties.hibernate.dialect, for example.
Then you can use Spring features to move your configuration wherever you want. To move it to ./config/application.properties I suppose you will have to add #PropertySource("./config/application.properties") to one of your #Configuration classes, or something similar.
I'm sure you can also keep the hibernate configuration in a separate file (separate from the rest of your application configuration).
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details about externalizing configuration in Spring Boot.
For some reason, it seems hibernate-search will prevent application from starting as long as a hibernate.properties configuration file does not exist. After trying for a while without success, i found a work around for my problem.
First, i created an empty hibernate.properties file and place it under src/main/resources.
Secondly, i moved all hibernate-search configurations to application.properties as follows:
spring.jpa.properties.hibernate.search.default.indexmanager = elasticsearch
spring.jpa.properties.hibernate.search.default.elasticsearch.host = http://my-server.com
spring.jpa.properties.hibernate.search.default.elasticsearch.index_schema_management_strategy = CREATE
spring.jpa.properties.hibernate.search.default.elasticsearch.required_index_status = yellow
This way, the application will start and spring will get all configuration from the externalized configuration as documented here.

Spring Boot and Eureka: eureka.client.healthcheck.enabled property not found

I have a Spring Boot Hystrix dashboard application where in a .property file I am trying to define some key/value pair(s) ― as per tutorial: https://exampledriven.wordpress.com/2016/07/05/spring-cloud-hystrix-example/
I am getting error when adding property eureka.client.healthcheck.enabled = true. The editor complains with:
Unknown property 'eureka.client.healthcheck'
Please, help me to find out the correct dependency, or if I am missing anything else.

Resources