Why Thymeleaf templates are not found? - spring-boot

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.

Related

Error Resolving Thymeleaf Templates Files when run a jar file in Spring Boot App but runs fine locally

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

Spring boot not mapping JSP pages Intellj

I am trying to map index1.jsp from controller.
Here is my controller method code:
#GetMapping({"/", "/hello"})
public String hello(Model model, #RequestParam(value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "index1";
}
This is WebConfiguration:
#Configuration
public class WebConfiguration implements WebMvcConfigurer {
#Bean
public UrlBasedViewResolver viewResolver() {
UrlBasedViewResolver resolver
= new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
This is index.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Quiz</title>
</head>
<body>
<h2>Hello ${name}!</h2>
</body>
</html>
This is application.properties:
spring.application.name=quiz
server.port=8090
server.error.whitelabel.enabled=false
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
This is pom.xml:
<?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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>war</packaging>
<groupId>com.test</groupId>
<artifactId>quiz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>quiz</name>
<description>Quiz app</description>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I am getting error: HTTP Status 500 – Internal Server Error
This is from console:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index1], template might not exist or might not be accessible by any of the configured Template Resolvers
...
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index1], template might not exist or might not be accessible by any of the configured Template Resolvers
I think it look up at templates folder, and not in WEB-INF/jsp. If I try to map html file from templates it works. But with JSP makes problem.
Here it is a structure of application:
Link of picture of project structure
There are following steps you need to follow if you want to work with Spring boot + JSP instead of thymeleaf.
Remove thymeleaf dependency from pom.xml
Add below dependencies in pom.xml (Jasper to compile JSPs)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Refer https://mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/ for more information.

spring boot 2.1.0 war file cannot config 'context-path' or else 'include' in jsp not working

I'm using Spring Boot 2.1.0 to develop a web application. I package the codes into a WAR file with maven.
I configured server.servlet.context-path=/marpt in application.properties file so that application can not access by ip+port directly.
Then I run the WAR using java -jar marpt.war command, and it is successed start.
Then I open the running web application in Chrome, there're some problems throw out:
1. The index login page shows corrcetly;
2. After login, jsp include not working, both of two types of inclue command, <jsp:include page="${ctx}/home/sidebar" /> and <%#include file="../../pages/share/partial/header.jsp" %> and any other references.
I use javascript to alert the ${ctx} which I defined in another jsp file, and include it in top of homepage by <%# include file="../../pages/common/taglibs.jsp" %>, it's very strange, the taglibs.jsp can be included. In it I set <c:set var="ctx" value="${pageContext.request.contextPath}" />. In home page I do this: alert('${pageContext.request.contextPath}'), the result is /marpt.
I'm confused where is wrong.
I try to package the jar file, it's also start correctly, but pages cannot open in browser at all.
Below are main configuration files:
POM.xml
<?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.test</groupId>
<artifactId>ma-rpt</artifactId>
<version>1.5.0</version>
<packaging>war</packaging>
<name>ma-rpt</name>
<description>Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!-- Shiro ehCache -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- jstl支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>marpt</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>properties/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</build>
</project>
application.properties
# Spring Public
server.port =8096
server.servlet.context-path =/marpt
# Cache
spring.cache.type =ehcache
# Db
spring.datasource.driver-class-name =oracle.jdbc.driver.OracleDriver
spring.datasource.url =jdbc:oracle:thin:#192.168.0.1:1521:marpt
spring.datasource.username =admin
spring.datasource.password =123456
# MyBatis
mybatis.config-location =classpath:/configs/mybatis/mybatis.cfg.xml
mybatis.mapper-locations =classpath*:/mybatis/**/*Mapper.xml
# Log
spring.output.ansi.enabled =DETECT
logging.path =/data/home/www/marpt/logs
logging.file =ma-rpt
logging.level.* =DEBUG
#debug=false
rpt.project.name =ma-rpt
rpt.project.develop =true
SpringBootConfig.java
#SpringBootConfiguration
public class SpringBootConfig implements WebMvcConfigurer {
#Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
configurer.setUseRegisteredSuffixPatternMatch(true);
}
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true)
.favorParameter(true)
.parameterName("format")
.ignoreAcceptHeader(true)
.defaultContentType(MediaType.TEXT_HTML)
.mediaType("html", MediaType.TEXT_HTML)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xls", MediaType.valueOf("application/vnd.ms-excel"))
.mediaType("xlsx", MediaType.valueOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
}
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
Set<String> modelKeys=new HashSet<>();
modelKeys.add("list");
modelKeys.add("table");
registry.jsp("/views/", ".jsp");
registry.enableContentNegotiation(new MappingJackson2JsonView());
XlsView xlsView=new XlsView();
xlsView.setModelKeys(modelKeys);
registry.enableContentNegotiation(xlsView);
XlsxView xlsxView=new XlsxView();
xlsxView.setModelKeys(modelKeys);
registry.enableContentNegotiation(xlsxView);
}
}
Main of homepage.jsp
<%# page language="java" pageEncoding="UTF-8" errorPage="../error/error.jsp" %>
<%# include file="../../pages/common/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<title>Home</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="${ctx}/assets/global/plugin/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="${ctx}/assets/global/plugin/jquery/jquery.min.js" type="text/javascript"></script>
</head>
<body >
<div class="page-wrapper">
<%#include file="../../pages/share/partial/header.jsp" %>
<div id="pageContainer" class="page-container">
<jsp:include page="${ctx}/home/sidebar" />
<jsp:include page="${ctx}/home/content" />
</div>
</div>
<script src="${ctx}/assets/global/plugin/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script>
$(function() {
alert('${ctx}'); // result is '/marpt'
alert('${pageContext.request.contextPath}'); // result is '/marpt' too
});
</script>
</body>
</html>
The application structure:
application structure
So, if I define the server.servlet.context-path=/marpt configure, how can I make <%#include... or <jsp:include... working in jsp file in Spring Boot? Thanks very much!
======================== supply ===========================
I tried to downgrade too 1.5.18, modified properties to server.context-path=/marpt, the problem is still exist, not relate to the version of spring boot.
The include files are some html tags, js, and some bind model attributes(such as ${param.dt}..), JSP files which are NOT mapping to servlet. If I configure them in a static resources' path, also failure.
======================== figure out ===========================
see JB Nizet's comment
Add the following spring tag in order to get property value "server.servlet.context-path" using #environment.getProperty('some.property') should work.
<spring:eval expression="#environment.getProperty('server.servlet.context-path')" var="ctx" />
<link href="${ctx}/assets/global/plugin/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

Upgrading Spring Boot 1.5 to 2 <sec:authorize> not working

I've been upgrading my app to use spring boot 2 and my views have not been rendering correctly. They content that should be hidden with the is no longer working. My methods and pages are still secured properly so it seems to be an issue with rendering the page. Also, isAuthenticated and isAnonymous don't work either.
I've tried changing to and my security tag to xmlns:sec="http://www.thymeleaf.org/extras/spring-security" from xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
SECURITY CONFIG
#Configuration
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
#Autowired
private DataSource dataSource;
#Autowired
private CustomAccessDenied accessDeniedHandler;
#Value("${spring.queries.users-query}")
private String usersQuery;
#Value("${spring.queries.roles-query}")
private String rolesQuery;
#Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().usersByUsernameQuery(usersQuery).authoritiesByUsernameQuery(rolesQuery).dataSource(dataSource).passwordEncoder(bCryptPasswordEncoder);
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/" , "/home").permitAll()
.antMatchers("/admin/**").hasAnyRole("ADMIN, OWNER")
.antMatchers("/register/**").hasAnyRole("ADMIN, CASHIER")
.antMatchers("/staff/**").authenticated()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/")
.permitAll()
.and()
.headers()
.frameOptions().disable()
.and()
.exceptionHandling()
.accessDeniedHandler(accessDeniedHandler);
}
#Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/pics/**", "/fonts/**");
}
}
HTML PAGE
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<title>Home</title>
<div th:replace="fragments/css"></div>
</head>
<body>
<div th:replace="fragments/header"></div>
<main>
<div class="scale-transition scale-out" sec:authorize="isAnonymous()">
<!-- USER NOT LOGGED IN MENU -->
<div class="row" style="margin-top: 25px">
<div class="col s12 m8 offset-m2">
<form id="idcards">
<h1 class="center-align">SWIPE YOUR CARD TO LOGIN</h1>
<h4 class="center-align">TAP GREY BOX IF NOT WORKING</h4>
<input class="center-align grey lighten-3" style="height: 100px; font-size: 60px" id="cardData" type='password' value='' autofocus>
<input class="hide" type="button" value="Fill fields" id="filler2" onClick="fillValuesInTextBoxes()">
</form>
</div>
<div class="row">
<div class="col s12 m8 offset-m2" style="margin-top: 50px">
<h3 class="center-align" style="text-decoration: underline;">ANNOUNCEMENTS</h3>
<div>
<div class="card-panel col s12 m4" th:each="announcementsList: ${announcementsList}">
<p class="col s12 m10 offset-m1" th:text="${announcementsList.text}"></p>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
<div th:replace="fragments/footer"></div>
</body>
</html>
DEPENDENCIES
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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>
</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-security</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-webflux</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<!-- source output directory -->
<outputDirectory>target/metamodel</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/metamodel</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This issue always tend to be resolve by adding missing dependencies or changing the ones you are using. So, first, try changing your POM's dependencies to springsecurity5. If that doesn't work, try adding the following #Bean.
Configuration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;
#Configuration
public class LeafConfig {
#Bean
public SpringSecurityDialect springSecurityDialect(){
return new SpringSecurityDialect();
}
}
POM
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
Since you are using <artifactId>spring-boot-starter-parent</artifactId>, don't add any version to your Thymeleaf Extras, let Spring Boot manage that for you.
Replaced this
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
with
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

Spring Boot jsp files cannot be found

I'm developing simple Spring Boot MVC application. Previously I had everything working with just Spring + Hibernate (that is there was servlet.xml etc.) and I'm trying to rebuild project to use Spring Boot. Althought I have read many answers on SO I still can't fix Spring Boot not finding my .jsp files - it displays whitelabel error page.
Many answers on SO are about including tomcat-embed-jasper and jstl in pom.xml but I already have it. I'm using IntelliJ IDEA 17 Ultimate, Tomcat 8.5.13, latest versions of Spring and Spring Boot.
Project structure:
Pom.xml:
<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>
<artifactId>spring-invoice</artifactId>
<groupId>spring-invoice</groupId>
<version>1.0.0</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<springframework.version>4.3.8.RELEASE</springframework.version>
<hibernate.version>5.2.9.Final</hibernate.version>
<mysql.connector.version>5.1.41</mysql.connector.version>
<hibernatevalidator.version>5.4.1.Final</hibernatevalidator.version>
<start-class>com.invoices.Application</start-class>
</properties>
<dependencies>
<!-- Spring boot -->
<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-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernatevalidator.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- C3PO -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Servlet + JSP + JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>spring-invoice</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- The Compiler Plugin is used to compile the sources of project -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Builds a Web Application Archive (WAR) file from the project output and its dependencies. -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties file
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/fakturowanie
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
# Hibernate
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
entitymanager.packagesToScan=com.invoices
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
Application class
#SpringBootApplication
public class Application extends SpringBootServletInitializer
{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
One of my jsp files
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="pl-PL">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Zarządzaj kontrahentami</title>
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>Zarządzaj kontrahentami</h2>
</div>
</div>
<div>
<div>
<!-- put new button: Add Customer -->
<input type="button" value="Dodaj nowego kontrahenta"
onclick="window.location.href='addCustomer'; return false;"
class="add-button"/>
<table>
<tr>
<th>Alias</th>
<th>Nazwisko</th>
<th>Imię</th>
<th>Nazwa firmy</th>
<th>NIP/PESEL</th>
<th>Ulica i nr mieszkania</th>
<th>Kod pocztowy</th>
<th>Miejscowość</th>
<th>Sposób zapłaty</th>
<th>Uwzględnij numer faktury</th>
</tr>
<c:forEach var="tempCustomer" items="${allCustomers}">
<c:url var="updateLink" value="/customers/updateCustomer">
<c:param name="customerID" value="${tempCustomer.id}"/>
</c:url>
<c:url var="deleteLink" value="/customers/deleteCustomer">
<c:param name="customerID" value="${tempCustomer.id}"/>
</c:url>
<tr>
<td>${tempCustomer.alias}</td>
<td>${tempCustomer.lastName}</td>
<td>${tempCustomer.firstName}</td>
<td>${tempCustomer.companyName}</td>
<td>${tempCustomer.taxIdentifier}</td>
<td>${tempCustomer.postalCode}</td>
<td>${tempCustomer.city}</td>
<td>${tempCustomer.paymentMethod}</td>
<td>${tempCustomer.includeInCount}</td>
<td>
<!-- display the update and delete link -->
Edytuj
|
<a href="${deleteLink}" onclick="if (!(confirm(
'Czy na pewno usunąć tego kontrahenta?'))) return false">Usuń</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
And controller of it
#Controller
#RequestMapping("/customers")
public class CustomersController implements IUtilities<Customer>
{
#Autowired
private ICustomerDAO customerDAO;
#GetMapping("/manageCustomers")
public String manageCustomers(Model model)
{
List<Customer> allCustomers = convertIterableToCollection(customerDAO.findAll());
model.addAttribute("allCustomers", allCustomers);
return "manage-customers";
}
#GetMapping("/addCustomer")
public String showFormForAdd(Model model)
{
// create model attribute to bind form data
Customer theCustomer = new Customer();
model.addAttribute("customerToEdit", theCustomer);
return "customer-form";
}
#GetMapping("/updateCustomer")
public String showFormForUpdate(#RequestParam("customerID") int customerId, Model model)
{
// get the customer from service
Customer theCustomer = customerDAO.findOne(customerId);
model.addAttribute("customerToEdit", theCustomer);
//redirect to update form
return "customer-form";
}
#GetMapping("/deleteCustomer")
public String deleteCustomer(#RequestParam("customerID") int customerId)
{
//delete the customer
customerDAO.delete(customerId);
return "redirect:/customers/manageCustomers";
}
#PostMapping("/saveCustomer")
public String saveCustomer(#ModelAttribute("customerToEdit") Customer customer) {
//save the customer using our service
customerDAO.save(customer);
return "redirect:/customers/manageCustomers";
}
#Override
public List<Customer> convertIterableToCollection(Iterable<Customer> iterable) {
List<Customer> list = new ArrayList<>();
iterable.forEach(list::add);
return list;
}
}
You are using Spring Boot but your pom is riddled with conflicting dependencies. For starters cleanup your pom.xml. Also in your pom you should take into account what is being done in the JSP Samples pom.xml (Which means at least make the spring-boot-starter-tomcat provided instead of the default compile scope.
Finally you don't need the maven-compile and maven-war plugins as those are already inherited from the parent.
<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>
<artifactId>spring-invoice</artifactId>
<groupId>spring-invoice</groupId>
<version>1.0.0</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<hibernate.version>5.2.9.Final</hibernate.version>
<start-class>com.invoices.Application</start-class>
</properties>
<dependencies>
<!-- Spring boot -->
<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-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- C3PO -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Servlet + JSP + JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<finalName>spring-invoice</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Also take into account the limitations there are when using Spring Boot with JSPs.
However the actual problem is your project structure, you are using Maven with should have a src/main/java directory for your java sources and all your web related things should be in src/main/webapp instead of web, further non java related resources should be in src/main/resources. Due to not having this default structure files aren't going to be copied to the correct location (actually they will be ignored).

Resources