I have developed one Full Stack Spring Boot application using thymeleaf and it works fine locally without any issues.
Now I am trying to deploy it to AWS cloud but it gave me error [ internal server error ]
I inspected Relational database and tried connecting from app and it works fine.
The only problem it seems to have is it's unable to resolve templates when a jar file is created and no templates are getting rendered.
When I launch app, it sends HttpRequest to '/' which renders home template like so and it fails :
// displays home page
#GetMapping("/")
public String homePage(Model model) {
model.addAttribute("title", "Home Page - Information Keeper");
return "home"; // return home.html page from templates folder
}
I have all the templates under resources/template/home [ and so on]
My properties file is
server.port = 5000
spring.thymeleaf.enabled=true
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.datasource.url=jdbc:postgresql://RDS_Endpoint:5432/myinfokeeper
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect
spring.jpa.show-sql = true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
And my pom.xml is as follows :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>info.keeper</groupId>
<artifactId>info_keeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Info_Keeper</name>
<description>Information Keeper using Spring boot, JPA and Thymeleaf with Spring Security.</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- use postgres sql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${project.parent.version}</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<goal>repackage</goal>
</configuration>
</plugin>
</plugins>
<finalName>InfoKeeperWebApp</finalName>
</build>
What should I exactly do ? Why it is not able to resolve those templates with jar but works perfectly on localhost ?
I have looked and tried many solutions on stackoverflow, many of them seem to have problem on localhost but mine is working perfectly on localhost but not on jar leading to error in AWS beanstalk as well.
I have been stuck for days :)
Any help would be appreciated.
And My home.html looks like this :
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
th:replace="base::layout(~{::div})">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Information Keeper || Home Page</title>
</head>
<body>
<div class="home d-flex justify-content-center align-items-center">
<section>
<h3>Keep Your Information Secure on Web</h3>
<h4>Recall Anywhere Anytime</h4>
<a class="btn btn-warning btn-lg text-white" href="/register">Get Started</a>
</section>
<script>
let items = document.getElementsByClassName("item");
<!-- alert(items.length); -->
<!-- remove active class first -->
for(i in items) {
<!-- alert(items[i].className)-->
if(items[i].className === 'item active') {
items[i].classList.remove('active');
}
}
const homeLink = document.getElementById("home-link");
homeLink.classList.add("active");
</script>
</div>
</body>
</html>
You can try with these configs and make sure your HTML file is containing in the "templates" folder, not "template" folder in your question
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
For some reason, after the deployment, I started to give a 500 error when saving (that is, the post fulfills the request, but reloading the same page already by get causes an error. And the first time the get request this page opens to show the filling form). Although everything works well on the local computer. I ask for help!
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.asu</groupId>
<artifactId>pdn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pdn</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- JAXB -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.0.8</version>
</plugin>
</plugins>
</build>
</project>
My Controller:
#GetMapping("/new_violation")
public String showNewViolationForm(Model model) {
Violation violation = new Violation();
model.addAttribute("violation", violation);
return "new_violation";
}
#PostMapping("/new_violation")
public String saveViolation(
#Valid #ModelAttribute Violation violation,
BindingResult bindingResult,
Model model
) {
if (bindingResult.hasErrors()) {
Map<String, String> errorsMap = getErrors(bindingResult);
model.mergeAttributes(errorsMap);
model.addAttribute("violation", violation);
} else {
violationService.save(violation);
}
return "/new_violation";
}
And some of my Thymeleaf:
<tr class="text-center">
<form action="#" method="post" th:action="#{/new_violation}" th:object="${violation}">
<div>
<th>
<label>
<input th:field="*{numProtocol}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{numProtocol}"
th:if="${#fields.hasErrors('numProtocol')}">
</span>
</th>
<th>
<label>
<input th:field="*{dateProtocol}" type="date"/>
</label>
<span class="form-control is-invalid" th:errors="*{dateProtocol}"
th:if="${#fields.hasErrors('dateProtocol')}">
</span>
</th>
<th>
<label>
<input th:field="*{violationAddress}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{violationAddress}"
th:if="${#fields.hasErrors('violationAddress')}">
</span>
</th>
<th>
<div>
<label>
<input th:field="*{child.fio}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{child.fio}"
th:if="${#fields.hasErrors('child.fio')}">
</span>
</div>
</th>
<th>
<label>
<input th:field="*{child.address}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{child.address}"
th:if="${#fields.hasErrors('child.address')}">
</span>
</th>
<th>
<button class="btn btn-primary bg-danger" type="submit">Сохранить</button>
</th>
</div>
</form>
Looks like there is a typo in your return statement.
Instead of
return "/new_violation";
try this:
return "new_violation";
As the same appears in your get mapping. I am assuming the same should be in your post mapping too.
I have a Spring Boot 2.1.6 application (Spring 5), and I'd like to use Thymeleaf as my templating engine. I followed online tutorials to setup my project, the views and the controllers, and when I wanted to start it up I noticed that Thymeleaf complains that it is unable to find any templates:
2019-07-12T17:14:25,269 WARN [main] o.s.b.a.t.ThymeleafAutoConfiguration$DefaultTemplateResolverConfiguration: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
I think I setup the project as it should be (at least according to the tutorials and forums I could find):
src/main/
java/
a.b.c.MyController
rest of the classes and packages
resources/
static/
css/
bootstrap.min.css
main.css
js/
bootstrap.min.js
jquery-3.4.1.min.js
login.js
main.js
templates/
login.html
main.html
My controller looks like this:
#ApiOperation(value = "Get login page", nickname = "login", notes = "", tags = { "My App", })
#ApiResponses(value = { #ApiResponse(code = 200, message = "Success") })
#GetMapping(value = { "/", "/login" })
#ResponseStatus(code = HttpStatus.OK)
public String login(Model model, String error, String logout) {
if (error != null) {
model.addAttribute("error", "Your username and/or password is invalid.");
}
if (logout != null) {
model.addAttribute("message", "You have been logged out successfully.");
}
return "login";
}
The login.html looks like this:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>My App :: Login</title>
<link rel="stylesheet" type="text/css" href="#{/css/bootstrap.min.css}">
<link rel="stylesheet" type="text/css" href="#{/css/main.css}">
</head>
<body>
<h1>My App</h1>
<div class="container">
<form id="userform" method="post" action="#" th:action="#{/authenticate}" th:object="${userForm}" class="form-signin">
<h2 class="form-heading">Log In</h2>
<span>${message}</span>
<div class="form-group ${status.error ? 'has-error' : ''}">
<input type="text" class="form-control" placeholder="Username" autofocus th:field="*{username}"></input>
</div>
<input name="password" id="password" type="password" class="form-control" placeholder="Password" th:field="*{password}"/>
<input type="hidden" th:name="${ _csrf.parameterName }" th:value="${ _csrf.token }"/>
<button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>
</form>
</div>
<script src="#{/js/jquery-3.4.1.min.js}"></script>
<script src="#{/js/bootstrap.min.js}"></script>
<script src="#{/js/login.js}"></script>
</body>
</html>
When I open the login page, I get a simple HTML page, with the word "login" written in the body.
I find this strange, Thymeleaf is looking for the templates in 'classpath:/templates/', which should be correct, because I have log4j2 XML configured in application.properties as 'logging.config=classpath:log4j2-${spring.profiles.active}.xml', and this XML is found in the same src/main/resources folder. So what can be the reason why the templates folder is not found there?
Update:
I forgot to mention: I tried to run it from Eclipse as a Spring Boot app, and also tried to run it with Maven as mvn spring-boot:run, with same results.
Also, I'm using Java 12. My pom.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.groupid</groupId>
<artifactId>my.artifactid</artifactId>
<packaging>war</packaging>
<name>MyApp</name>
<version>${baseversion}.${gitcommitcount}.${buildnumber}</version>
<description>My App</description>
<properties>
<baseversion>1.0.0</baseversion>
<java.version>12</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<springfox-version>2.9.2</springfox-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
<buildnumber>0</buildnumber>
<gitcommitcount>0</gitcommitcount>
</properties>
<distributionManagement>
<repository>
<id>id</id>
<name>Internal Local Releases</name>
<url>http://x.x.x.x:xxxx/repository/local_release/</url>
</repository>
<snapshotRepository>
<id>id</id>
<name>Internal Local Snapshots</name>
<url>http://x.x.x.x:xxxx/repository/local_snapshot/</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>id</id>
<url>x.x.x.x:xxxx/repository/local_group/</url>
</repository>
</repositories>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<build>
<finalName>${project.name}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<!--
Will need to be excluded from final WAR
<exclude>*.properties</exclude>
<exclude>*.xml</exclude>
-->
</excludes>
<includes>
<!-- Include is only for running locally -->
<include>*.properties</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>12</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmArguments>--enable-preview</jvmArguments>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archiveClasses>false</archiveClasses>
<warSourceDirectory>WebContent</warSourceDirectory>
<archive>
<manifestEntries>
<Built-On>${maven.build.timestamp} UTC</Built-On>
<ModuleName>${project.name}</ModuleName>
<ModuleVersion>${project.version}</ModuleVersion>
</manifestEntries>
<manifestSections>
<manifestSection>
<name>Release section</name>
<manifestEntries>
<BaseVersion>${baseversion}</BaseVersion>
<BuildNumber>${buildnumber}</BuildNumber>
<GITRevision>${gitrevision}</GITRevision>
</manifestEntries>
</manifestSection>
</manifestSections>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>rename-wars</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>scripts/rename-wars.bat</executable>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--SpringFox dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>com.github.joschi.jackson</groupId>
<artifactId>jackson-datatype-threetenbp</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.10.6</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.10.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.10.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Commons HttpClient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!-- Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- Oracle JDBC -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1.0</version>
</dependency>
<!-- CSV parsing -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.6</version>
</dependency>
<!-- Javax Mail for email validation -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- Configuration -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Actuator to gather metrics and health -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- JSON -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<!-- Testing dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Update 2: application.properties
server.port=9080
spring.profiles.active=dev
spring.jackson.date-format=a.b.c.RFC3339DateFormat
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
logging.config=classpath:log4j2-${spring.profiles.active}.xml
# Setting session timeout
server.servlet.session.timeout=10m
# ThymeLeaf settings
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# dbcp2 settings
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.test-on-return=false
spring.datasource.dbcp2.validation-query=select 1 from dual
spring.datasource.dbcp2.validation-query-timeout=30000
spring.datasource.dbcp2.time-between-eviction-runs-millis=30000
spring.datasource.dbcp2.min-evictable-idle-time-millis=30000
spring.datasource.dbcp2.initial-size=10
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true
spring.datasource.dbcp2.log-abandoned=true
spring.datasource.dbcp2.log-expired-connections=true
spring.datasource.dbcp2.max-wait-millis=1000
spring.datasource.dbcp2.remove-abandoned-on-borrow=true
spring.datasource.dbcp2.remove-abandoned-on-maintenance=true
spring.datasource.dbcp2.remove-abandoned-timeout=60
spring.datasource.dbcp2.num-tests-per-eviction-run=3
spring.datasource.dbcp2.default-auto-commit=true
# File upload settings
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
# Actuator settings
# Actuator endpoint settings
management.endpoint.shutdown.enabled=true
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always
management.endpoint.metrics.enabled=true
management.endpoint.loggers.enabled=true
management.endpoint.info.enabled=true
management.endpoints.web.exposure.include=health,metrics,loggers,info
management.health.cassandra.enabled=false
management.health.couchbase.enabled=false
management.health.db.enabled=true
management.health.diskspace.enabled=true
management.health.diskspace.path=/
management.health.elasticsearch.enabled=false
management.health.influxdb.enabled=false
management.health.ldap.enabled=false
management.health.mail.enabled=false
management.health.mongo.enabled=false
management.health.neo4j.enabled=false
management.health.rabbit.enabled=false
management.health.redis.enabled=false
management.health.solr.enabled=false
# App info for actuator
info.app.name=My App
info.app.description=My App
info.app.version=1.0.0
info.customer=My App
Update 3: added Template and View resolvers as follows:
#Configuration
public class TemplateBeans implements WebMvcConfigurer {
#Autowired
private ServletContext servletContext;
#Bean
#Description("Thymeleaf template resolver serving HTML5")
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver servletContextTemplateResolver = new ServletContextTemplateResolver(
servletContext);
servletContextTemplateResolver.setPrefix("classpath:/templates/");
servletContextTemplateResolver.setCacheable(false);
servletContextTemplateResolver.setSuffix(".html");
servletContextTemplateResolver.setTemplateMode("HTML5");
servletContextTemplateResolver.setCharacterEncoding("UTF-8");
return servletContextTemplateResolver;
}
#Bean
#Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.setTemplateResolver(templateResolver());
return springTemplateEngine;
}
#Bean
#Description("Thymeleaf view resolver")
public ViewResolver viewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");
return thymeleafViewResolver;
}
}
With this, I get exception:
2019-07-15T14:43:21,382 DEBUG [http-nio-9080-exec-3] o.s.w.s.FrameworkServlet: Failed to complete request: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "/classpath:/templates/login.html")
2019-07-15T14:43:21,389 ERROR [http-nio-9080-exec-3] o.a.j.l.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [/AICGDPR] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "/classpath:/templates/login.html")] with root cause
java.io.FileNotFoundException: ServletContext resource "/classpath:/templates/login.html" does not exist
I also tried with ClassLoaderTemplateResolver instead of ServletContextTemplateResolver, I got a somewhat different exception:
2019-07-15T14:48:54,208 DEBUG [http-nio-9080-exec-1] o.s.w.s.FrameworkServlet: Failed to complete request: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "classpath:/templates/login.html")
2019-07-15T14:48:54,217 ERROR [http-nio-9080-exec-1] o.a.j.l.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [/AICGDPR] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "classpath:/templates/login.html")] with root cause
java.io.FileNotFoundException: ClassLoader resource "classpath:/templates/login.html" could not be resolved
Change the location of your login page to static instead of template.
It should be as follows,
resources/static/login.html instead of resources/templates/login.html.
And don't forget to specify the extension in your controller, return "login.html";
(Hope you have configured view resolver, because if not your endpoint will be returning the string "login.html")
Ended up using JSP instead. Works flawlessly on first try.
I was having the same issue as well. What I did to solve it was explicitly stating the templates path at the application.properties file from this post https://stackoverflow.com/a/41319170/14550345 :
spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.
spring.thymeleaf.prefix=src/main/resources/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
(I replaced the classpath: by the hardcoded path)
In my case this issue happened because the resources folder in IntelliJ was not marked as "Resources". I needed to:
File > Project Structure > Modules > pet-clinic-web
Click on sources tab
src>main>java>resources
Click on the resources folder
Click on Mark as: Resources
Click on Apply
Click on OK
hey i was suffering with same type of problem. After a long time i discovered that i was missing the class level mapping. For example i was writing this code for my case:
#Controller
public class ProviderController {
#GetMapping(value = "providers")
public String getAllProviders(Model model){
model.addAttribute("roomReservations", null);
return "provider";
}
and thyleaf wasn't detecting my view. I solved this issue by using following code:
#RequestMapping(value = "providers")
#Controller
public class ProviderController {
#GetMapping
public String getAllProviders(Model model){
model.addAttribute("roomReservations", null);
return "provider";
}
i hope it will resolve your issue too.
I am attempting to install bootstrap for use in my spring boot project, which uses thymeleaf. I am getting this error with the template (index.html, shown below):
Malformed markup: Attribute "class" appears more than once in element
I assume this is because bootstrap isn't installed properly. Below I have an image of the files also.
I am pretty sure all the dependencies are right.
INDEX HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="/bootstrap-3.3.7/css/bootstrap.min.css"/>
<script src="/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<!-- <link rel="stylesheet" type="text/css"
th:href="#{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}" />
<link rel="stylesheet" type="text/css" th:href="#{/css/main.css}" />
<script
src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js">
</script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script> -->
<title>Home</title>
<style></style>
</head>
<body>
<div class="navbar navbar-default" role="navigation" id="topnavbar">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-
toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="navbar-main">
<ul class="nav navbar-nav">
<li><span class="glyphicon glyphicon-home"></span>Profile</li>
<li>
<a href="/competition">
<span class="glyphicon glyphicon-star"></span> Competitions
</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="/logout"> <span class="glyphicon glyphicon-user"></span>
<strong>Log out</strong>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container" class="row">
<div class="page-header" id="banner">
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-6">
<img src="ban.png">
</div>
<div class="col-lg-4 col-md-5 col-sm-6">
<div class="sponsor"></div>
</div>
</div>
</div>
</div>
<div class="container"></div>
</body>
<script th:inline="javascript">
$(document).ready(function () {
var panels = $('.user-infos');
var panelsButton = $('.dropdown-user');
panels.hide();
//Click dropdown
panelsButton.click(function () {
//get data-for attribute
var dataFor = $(this).attr('data-for');
var idFor = $(dataFor);
//current button
var currentButton = $(this);
idFor.slideToggle(400, function () {
//Completed slidetoggle
if (idFor.is(':visible')) {
currentButton.html('<i class="glyphicon glyphicon-
chevron - up
text - muted
"></i>');
}
else {
currentButton.html('<i class="glyphicon glyphicon-
chevron - down
text - muted
"></i>');
}
})
});
$('[data-toggle="tooltip"]').tooltip();
$('button').click(function (e) {
e.preventDefault();
alert("This is a demo.\n :-)");
});
});
</script>
</html>
POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.finalYearProject</groupId>
<artifactId>student-life</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>student-life</name>
<description>Jill's Student Life Final Year Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8
</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- upgrade to thymeleaf version 3 -->
<thymeleaf.version>3.0.8.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-
dialect.version>
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-
extras-springsecurity4.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency> -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId1>spring-session</artifactId>
<version>1.2.2.RELEASE</version>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Project structure:
The exception will give the line # in the html where the error is occurring... in any case, the actual problem is here:
<div class="container" class="row">
Thymeleaf won't allow an element to have two attributes with the same name (Attribute "class" appears more than once in element). Just change it to:
<div class="container row">
I'm create small project with Thymeleaf + spring-boot.
And now I stuck with problem that sec:authorize="isAuthenticated()" and sec:authorize="isAnonymous()" return false for error pages. As result content from both this sections are hiden.
My project dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
My html page:
<div sec:authorize="isAuthenticated()">
<span class="navbar-text text-success"> <i class="fas fa-user fa-lg"></i>
<span sec:authentication="name"></span> </span>
<a class="header-btn" th:href="#{/logout}">
<i class="fas fa-sign-out-alt fa-lg"></i> Sign Out
</a>
</div>
<div sec:authorize="isAnonymous()">
<span class="navbar-text text-success"><i class="fas fa-user-secret fa-lg"></i> Anonymous</span>
<a class="header-btn" th:href="#{/login}">
<i class="fas fa-sign-in-alt fa-lg"></i> Sign In
</a>
</div>
In my case for error pages like 403 or 404 both div hidden.
What need to change than make it start to work correctly?
In my case, I added the following to application.properties:
security.filter-dispatcher-types=ASYNC, FORWARD, INCLUDE, REQUEST, ERROR
... and restarted the application. The security context was now available on my error pages.