HTTP Status 404 – Not Found Spring Boot - spring

I am creating simple crud system using spring boot. When I load the page it is working currectly. when I add a new link it display as HTTP Status 404 – Not Found Spring Boot I don't why everything I made currect way I attached screenshot with code below.
Error Screen
EmployeeController
#Autowired
private EmployeeService service;
#RequestMapping("/")
public String viewHomePage(Model model) {
List<Employee> listemployee = service.listAll();
model.addAttribute("listemployee", listemployee);
return "index";
}
#RequestMapping(value = "/new")
public String add(Model model) {
model.addAttribute("employee", new Employee());
return "new";
}
index.html
<table class="table">
<tbody>
<tr>
<td>
<a th:href="#{'/new'}">Add new</a>
</td>
</tr>
<tr th:each="employee : ${listemployee}">
<td th:text="${employee.id}">Employee ID</td>
<td th:text="${employee.firstName}">FirstName</td>
<td th:text="${employee.lastName}">LastName</td>
<td>
<a th:href="#{'/edit/' + ${employee.id}}">Edit</a>
<a th:href="#{'/delete/' + ${employee.id}}">Delete</a>
</td>
</tr>
</tbody>
</table>
folder structure
porm.xml
<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>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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/lindaschool?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.error.whitelabel.enabled=false
server.port=7020
spring.datasource.username=root
spring.datasource.password=
#logging.level.root=WARN
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false

The problem is package structure, you not following the standard package structure of spring boot and spring framework. By default spring boot application will only scans the classes annotated with any stereotype from main class and it's sub packages. I would recommend to move all dependency classes under sub packages of main class
com.example.crudbank
|
-------------------->CrudBankApplication.java
com.example.crudbank.service
|
--------------------->StudentService
com.example.crudbank.controller
|
----------------------> StudentController.java
com.example.crudbank.repository
|
-----------------------> StudentRepository.java
com.example.crudbank.domain
|
------------------------>StudentDomain.java

Related

Controller class cannot find html template

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;
}

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'

Spring Boot + Thymeleaf Security not recognized

i've been trying to use thymeleaf's security tags but i can't get them to work.
This is my security class:
#Configuration
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private Environment env;
#Autowired
private UserSecurityService userSecurityService;
private BCryptPasswordEncoder passwordEncoder() {
return SecurityUtility.passwordEncoder();
}
private static final String[] PUBLIC_MATCHERS = { "/css/**", "/js/**", "/image/**", "/", "/myAccount" };
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().
antMatchers(PUBLIC_MATCHERS).permitAll().anyRequest().authenticated();
http.csrf().disable().cors().disable().formLogin().failureUrl("/login?error").defaultSuccessUrl("/")
.loginPage("/login").permitAll().and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/?logout")
.deleteCookies("remember-me").permitAll().and().rememberMe();
}
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
}
}
Pom.xml
<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>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.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
</dependencies>
Index.html
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<ul class="navbar-nav navbar-right">
<li class="nav-item"><a class="nav-link" href="#">Shopping
Cart</a></li>
<li class="nav-item"><a sec:authorize="isAnonymous()"
class="nav-link" th:href="#{/login}">My Account</a></li>
<li class="nav-item"><a sec:authorize="isAuthenticated()"
class="nav-link" th:href="#{/myProfile}">My Account</a></li>
<li class="nav-item"><a class="nav-link"
sec:authorize="isAuthenticated()" href="#{/logout}">Logout</a></li>
</ul>
My problem is that both the nav-items called My Account are showing on the page and i guess the tag sec:authorize doesn't get recognized or something like that or i'm clearly doing something wrong:( Do i need to do any other configurations? I'm using spring boot 2, i've tried solving this in different ways from changing the version of thymeleaf-extras-springsecurity4 to adding a bean in the spring context but it's still not working:(
In spring boot auto configuration, an instance of SpringSecurityDialect is not auto configured. Therefore, you are having this problem. Try adding a bean of SpringSecurityDialect like below, and I hope, it will work.
#Bean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}

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).

isAuthenticated and isAnonymous are returning false simultaneously

In my current spring project, I had a view with this html code:
<th:block sec:authorize="isAuthenticated()">
<h2 sec:authentication="name"></h2>
</th:block>
<th:block sec:authorize="isAnonymous()">
<p> <a th:href="#{/loginPage}">Login</a> </p>
<p>
<ul>
<li> <a th:href="#{/homeFacebook}">Connect to Facebook</a> </li>
<li> <a th:href="#{/homeTwitter}">Connect to Twitter</a> </li>
</ul>
</p>
</th:block>
which when I run the application, nothing is being displayed in the page, because the conditionals sec:authorize="isAuthenticated()" and sec:authorize="isAnonymous()" are both returnin false;
My configuration:
At my Application.java class
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect( new SpringSecurityDialect() );
return engine;
}
At my pom.xml file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
I have other projects whit similar configuration working fine, but in this one i stuck this issue. Someone can see what's wrong here?
update
I change the pom.xml to this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>LATEST</version>
</dependency>
and now instead of show nothing, both blocks are being displayed. (if i remove thymeleaf-spring3 from list of dependencies in the pom.xml file, the page display none of the blocks, like before)
update 2
with this code, the page display nothing:
<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>
with that one display both blocks:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>LATEST</version>
</dependency>
but sec:authentication is not processed.

Resources