Controller class cannot find html template - spring-boot

My directory
I'm trying to create spring boot application for school that lists books to a html page from database with a controller.
Personally, i think that the problem is that the controller cannot find the template for some reason. Because when i navigate to the wanted template through chrome, it just shows "booklist" on the page, nothing else.
I've tried creating totally new project and copying the code from my other files to the new files with no results.
My controller class:
#Controller
#ResponseBody
public class BookController {
#Autowired
BookRepository bookRepository;
#RequestMapping(value = "/books", method = RequestMethod.GET)
public String getBooks(Model model) {
List<Book> books = (List<Book>) bookRepository.findAll();
model.addAttribute("books", books);
return "booklist";
}
My html template:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Book List</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Books</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Year</th>
<th>Isbn</th>
<th>Price</th>
</tr>
<tr th:each="book : ${books}">
<td th:text="${book.id}">id</td>
<td th:text="${book.title}">title</td>
<td th:text="${book.year}">year</td>
<td th:text="${book.isbn}">isbn</td>
<td th:text="${book.price}">price</td>
</tr>
</table>
</body>
</html>
pom.xml 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>hh.swd20</groupId>
<artifactId>Bookstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Bookstore</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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-web</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties file:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true

Remove the annotation #ResponseBody from your controller class, because:
The #ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.
Then the returned String booklist will be used by Spring-MVC to resolve the named HTML template file.
The template file (e.g. booklist.html) will be looked for by default within default template directory is src/main/resources/templates.
Otherwise make sure to have configured the ViewResolver properly.
For Thymeleaf you have to add dependency to your Maven POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
See also this Spring-Boot & Thymeleaf tutorial

You can try this .
Hit : localhost://yourportnumber/api/books
//#Controller
#RestController
//#ResponseBody
#RequestMapping("/api")
public class BookController {
#Autowired
BookRepository bookRepository;
#GetMapping(value = "/books", method = RequestMethod.GET)
public ModelAndView getBooks(Model model) {
List<Book> books = (List<Book>) bookRepository.findAll();
model.addAttribute("books", books);
ModelAndView mav = new ModelAndView("booklist");
return mav;
}

Related

Thymeleaf doesn't work in spring boot and also doesn't show in console

after trying multiple times on freemarker to work, I switched to thymeleaf and also faced the same problem. When returning the html file, it's just a string value and not the html file in the templates folder.
I don't know why it doesn't even start in console, when I watched youtube thymeleaf videos it shows in their console.
package com.example.demo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class controller {
#GetMapping
public String getUser(Model model) {
model.addAttribute("something", "welcome to the club");
return "user";
}
}
here is the html file in main/resources/templates
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Code</title>
</head>
<body>
<h1 th:text="${something}"></h1>
</body>
</html>
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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Solved now, I replace #RestController with #Controller because #RestController adds #ResponseBody and prevents to get the view.

I can't see any result with using thymeleaf

I get error like this when I used thymeleaf as view engine.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Feb 13 15:57:19 EET 2021
There was an unexpected error (type=Not Found, status=404).
No message available
My MainCotroller is like :
public class MainController {
#RequestMapping("/")
public ModelAndView homePage(){
ModelAndView homepage=new ModelAndView("homepage");
homepage.addObject("username","Ayberk");
return homepage;
}
}
My homepage.html file is like this:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--/*#thymesVar id="username" type="String"*/-->
<p th:text="${username}"></p>
<br />
<p>GoodBye</p>
</body>
</html>
My pom.xml file is 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 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.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>newproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>newproject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
My idea is Intellij 2020 and I have read this problem is related with intellij version. But i have not seen any solution. Is there any solution for it? What can i do?
EDIT AFTER RUDI'S ANSWER BUT STILL SAME ERROR.
#RequestMapping("/")
public String homePage(Model model){
model.addAttribute("username" , "ayberk");
return "src\\main\\resources\\templates\\homepage";
//return "homepage"; also tried like this.
}
My ModelAndView packages is org.springframework.web.servlet.ModelAndView

Why controller class not return jsp page in springBoot application

I created just a simle springBoot application,and I want when i lunch
my application it retun me my jsp page in my browser. but when I type
localhost:8080 in my browser it return me this type of error"There was
an unexpected error (type=Not Found, status=404)."I added jasper
dependency got same error,I use jsp api dependency in place jasper i
got same error.
what is the reason i am not getting jsp page as a result,why it throws
error404
this type of error
project file strcture
HomeController.java
package com.main.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping("/")
public String host() {
return "Katak.jsp";
}
}
Katak.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome to security World
</body>
</html>
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.0.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.main</groupId>
<artifactId>BasicSecurity-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>BasicSecurity-2</name>
<description>Demo project for SpringSecurity</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.5.37</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Make sure that you are using proper jsp names, in project structure, jsp name is showing as "Katakile.jsp". But in controller you are returning "Katak.jsp".
If names are proper and still not working, please use "tomcat-embed-jasper" dependency instead of "tomcat-jasper" and remove the tomcat-jasper dependency from your classpath, if exists.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.6</version>
<scope>provided</scope>
</dependency>
Just edit this line
#RequestMapping("/katak")
public String host() {
return "/Katakile.jsp";
}
And try acces localhost:8080/katak

Spring boot WAR file Runs successfully on Eclipse IDE and by terminal but not running on external Tomcat 9.0

I have created a SpringBoot application with thymeleaf template, This application running successfully on Eclipse IDE and Running War file by MAC terminal command:
java -jar ./SpringBootTestMe.war
But problem is that it is not running on external tomcat-9.0, Giving below ERROR:
Sun Jan 26 09:01:37 IST 2020 There was an unexpected error
(type=Internal Server Error, status=500). Error during execution of
processor
'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'
(template: "index" - line 13, col 28)
My source code are
File 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>
<groupId>com.spboot</groupId>
<artifactId>SpringBootTestMe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootTest</name>
<packaging>war</packaging>
<!-- lookup parent from repository -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.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>
<start-class>com.spboot.web.TestApp</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringBootTestMe</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SpringBootMainClass
package com.spboot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class TestApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
System.out.println("--------AppStarted--------");
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TestApp.class);
}
}
HomeController
#Controller
#RequestMapping("SpringBootTestMe")
public class HomeController {
#RequestMapping(value={"", "login"})
public String login(UserRecord userRecord) {
System.out.println("HomeController Called...");
return "index";
}
}
ModalClass
public class UserRecord implements Serializable {
private Long id;
private String username;
private String email;
//Setters and getters
}
index.html
<!DOCTYPE html>
<html xmlns:th="https://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<div>
<form action="#" th:action="#{/login}" th:object="${userRecord}" method="post">
<table>
<tr>
<td><label for="username">User Name</label></td>
<td><input type="text" th:field="*{username}" /></td>
<td th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Username Error</td>
</tr>
<tr>
<td><label for="email">Email</label></td>
<td><input type="text" th:field="*{email}"></input></td>
<td th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Name Error</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></input></td>
</tr>
</table>
</form>
</div>
</body>
</html>
It's giving error on username text field, tomcat not recognising the th:field
Can anybody help me please.
Thanks

Spring Boot 404 when trying to load a HTML file using Thymeleaf

Like the title says, I'm getting the Whitelabel 404 error page when trying to access localhost:8080.
Main class:
package com.michaelLong.studentaddressbook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class StudentAddressBookApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(StudentAddressBookApplication.class, args);
}
}
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.michaelLong</groupId>
<artifactId>student-address-book</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>student-address-book</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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-actuator</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</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-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller:
package controller;
import model.Student;
import model.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import java.util.Map;
#Controller
public class StudentController {
#Autowired
StudentRepository studentRepository;
#GetMapping("/")
public String showStudents(Model model){
model.addAttribute("students", studentRepository.findAll());
return "showStudents";
}
}
application.properties:
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/StudentAddressBook
spring.datasource.username=root
spring.datasource.password=SQLpassword
showStudents.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student List</title>
</head>
<body>
<h2>List of students</h2>
<table>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
</tr>
<tr th:each="student: ${students}">
<td th:text="${student.id}">Id</td>
<td th:text="${student.firstName}">First name</td>
<td th:text="${student.lastName}">Last name</td>
</tr>
</table>
</body>
</html>
Project structure:
src
|__main
|__java
| |__com.example.studentaddressbook
| | |__StudentAddressBookApplication
| |__controller
| | |__StudentController
| |__model
| |__Student
| |__StudentRepository
|__resources
|__static
|__templates
| |__showStudents.html
|__application.properties
I've tried looking at a lot of different tutorials, and a few SO posts like these:
Spring Boot and Thymeleaf: Can't find HTML templates & Why the html page doesn't get showed in thymeleaf?
I was originally trying to use JSPs, but I couldn't get those to work either. I feel like I'm banging my head against the wall at this point and I'm not sure what else to do. This is my first time trying to use Spring Boot and Thymeleaf, so I'm having some difficulty figuring this out.
Any help trying to figure out why I can't access the HTML page would be greatly appreciated.
You have your StudentAddressBookApplication located in package "com.michaelLong.studentaddressbook" => it will scan only the beans from this parent package.
StudentController is located in package "controller" => the application will not scan it at all.
Very simple solution : move StudentController to package com.michaelLong.studentaddressbook. Also, the same applies for StudentRepository.
P.S packages in java are always lower case.
The problem is happening because of you project structure. Create package controller and model under com.example.studentaddressbook. Create project structure like this image. Let us know it it work
For those who are using Intellij, I had problem with Intellij run. After:
ctrl+shift+a -> type: reimport... and select: "Reimport All Maven Projects". Made It working again.
Replacing common thymeleaf dependency by thymeleaf starter made the trick for me:
[Error]
implementation 'org.thymeleaf:thymeleaf:3.1.0.M1'
[Correct]:
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:2.6.7'

Resources