I was following a spring tutorial to make coronavirus tracker application.but then encountered a problem in parsing using thymeleaf - spring-boot

P.S - I just started spring boot
while parsing the file I got this error:
This application has no explicit mapping for /error, so you are seeing
this as a fallback.
Wed Feb 24 00:20:45 IST 2021 There was an unexpected error
(type=Internal Server Error, status=500). An error happened during
template parsing (template: "class path resource
[templates/home.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened
during template parsing (template: "class path resource
[templates/home.html]")
This is my home.html file
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>CoronaVirus Tracker Application</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="${testName}"></p>
<table>
<tr>
<th>State</th>
<th>Country</th>
<th>Total cases reported</th>
</tr>
<tr th:each="locationStat : ${locationStats}">
<td th:text="${locationStat.state}"></td>
<td th:text="${locationStat.country}"></td>
<td th:text="${locationStat.latestTotalCases}">0</td>
</tr>
</table>
</body>
</html>
This is my HomeController
package com.project.coronavirustracker.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.*;
import com.project.coronavirustracker.models.LocationStats;
import com.project.coronavirustracker.services.CoronaVirusDataService;
#Controller
public class HomeController {
#Autowired
CoronaVirusDataService coronaVirusDataService;
#GetMapping("/")
public String home(Model model) {
List<LocationStats> allStats = coronaVirusDataService.getAllStats();
model.addAttribute("locationStats", allStats);
return "home";
}
}
This is my LocationStats file
package com.project.coronavirustracker.models;
public class LocationStats {
private String state;
private String country;
private int latestTotalCases;
public void setState(String state) {
this.state = state;
}
public String getState() {
return this.state;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
public void setlatestTotalCases(int cases) {
latestTotalCases = cases;
}
public int getlatestTotalCases() {
return this.latestTotalCases;
}
#Override
public String toString() {
return "LocationStats{" +
"state='" + state + '\'' +
", country='" + country + '\'' +
", latestTotalCases=" + latestTotalCases +
'}';
}
}
And this is my 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.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>coronavirus-tracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>coronavirus-tracker</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.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

It is true that there is no testName but this does not generate an error. This will output an empty <p></p>.
The problem is that it cannot find the latestTotalCases method. (It must start with a capital letter.)
Solution 1
It will fix when you change this method as follows:
public int getLatestTotalCases() {
return this.latestTotalCases;
}
Solution 2
If you don't want to change the name of method, you need to call the method in the html file as follow:
<td th:text="${locationStat.getlatestTotalCases}">0</td>

Related

404 error in spring boot application on the server but it works on localhost

I made an application and ran it via inteliji. All was well until I wated to move the application onto a server. I am getting a 404 error at the head of it, and my logs are empty.
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>
<artifactId>spring-boot-hello</artifactId>
<packaging>war</packaging>
<name>Spring Boot Hello World Example</name>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
</parent>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>nameofwar</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
demoapplication.java
package com.example;
import com.example.demo.user.User;
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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
}
appcontroller
#RestController
public class AppController {
#Autowired
private final UserRepository userRepo;
private final AssignmentRepository assignmentRepo;
private final AssignmentService assignmentService;
private final ConfigService configService;
private final UserService userService;
#Autowired
public AppController(UserRepository userRepo, AssignmentService assignmentService,
AssignmentRepository assignmentRepo, ConfigService configService, UserService userService) {
this.userRepo = userRepo;
this.assignmentService = assignmentService;
this.assignmentRepo = assignmentRepo;
this.configService = configService;
this.userService = userService;
}
#GetMapping("")
public String viewHomePage() {
return "index";
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/<mydb>
spring.datasource.username=
spring.datasource.password=
spring.sql.init.mode=always
spring.sql.init.platform=mysql
spring.jpa.hibernate.ddl-auto=update
server.error.include-message=always
logging.level.=DEBUG
spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.http.multipart.enabled=true
server.error.include-exception=true
server.error.include-stacktrace=always
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>warname</title>
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" />
<script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container text-center">
<h2>Welcome</h2>
<!--<h3>Login</h3> -->
<div>
<h3><a th:href="#{/register}">Register</a> </h3>
<h3><a th:href="#{/login}"</a>Login</h3>
</div>
</div>
</body>
</html>
where I'm trying to go :
/nameofwar
I've looked through a hundred posts trying to find out what's going wrong and haven't found it yet. I'm using tomcat 9, java 11, java 11 on the server as well. Thanks for your help
Annotate your AppController with #org.springframework.stereotype.Controller instead of a RestController and try to use org.springframework.web.servlet.ModelAndView instead of a string
#GetMapping("")
public ModelAndView viewHomePage() {
return new ModelAndView("index");
}
I did not have my application.properties file filled out correctly to connect with my database and I needed to use Controller and not RestController everywhere
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/<application_name>
spring.datasource.username=<username>
spring.datasource.password=<password>

Adding Spring Security starter to my Spring Boot app automatically redirects all paths to one that's set in a completely different project

I added Spring Security Starter dependency (I use Maven) to secure my app (following a tutorial); the problem is that once I add it, whenever I go to localhost to check my progress, I'm always redirected to a path that's coded in another project (I can't even find the view right now).
I currently follow the tutorial on TutorialsPoint, but I use Spring Tools Suite 4 for adding dependencies, coding and running my Spring application instead of CLI on localhost:8080.
I tried finding the view in question, tinkering with another project's web.xml, but I can't find the source of the problem, all I know is that Spring Security Starter is somehow mucking it up.
Thymeleaf template
<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1" />
<link href = "css/styles.css" rel = "stylesheet"/>
<title>Spring Boot Application</title>
</head>
<body>
<h4>Welcome to Thymeleaf Spring Boot web application</h4>
</body>
</html>
Web controller
package com.example.vj7.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class WebController {
#RequestMapping(value = "/index")
public String index() {
return "index";
}
}
Application class
package com.example.vj7;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Vj7Application {
public static void main(String[] args) {
SpringApplication.run(Vj7Application.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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>vj7</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Vj7</name>
<description>Vježba 7</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The expected result from "localhost:8080" is just a header "Demo" (like in index.html and set in web controller), but instead a form I worked on before on another Spring project shows up with path "/login".
You need to override some of web security's default configurations if you want index.html to be accessed without authorization. taken from https://docs.spring.io/spring-security/site/docs/4.2.5.RELEASE/apidocs/org/springframework/security/config/annotation/web/configuration/EnableWebSecurity.html
#Configuration
#EnableWebSecurity
public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/", "/index").permitAll().anyRequest()
.permitAll();
}
}

Spring Boot + Thymeleaf = "Whitelabel Error Page" when trying to render index.html

I am trying to do simple Spring MVC LibraryDo You have any idea why I have problem with View, when i use homepage method in my controller it should show me index.html but i only get Whitelabel Error Page all the time, and i dont know why:/
My structure:
[https://i.imgur.com/5TrGGrB.png]
My controller:
package controller;
import model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import service.BookService;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
#Controller
public class LiberianController{
#Autowired
private BookService bookService;
#RequestMapping(value = "/")
public String homepage() {
return "index";
}
#GetMapping(value = "/allBooks")
public ModelAndView allBooks(ModelAndView modelAndView) {
List<Book> books = bookService.getAllBooks();
modelAndView.addObject("listBooks", books);
modelAndView.setViewName("allBooks");
return modelAndView;
}
#GetMapping(value = "/addBook")
public ModelAndView newBook(ModelAndView modelAndView) {
Book book = new Book();
modelAndView.addObject("book", book);
modelAndView.setViewName("addBook");
return modelAndView;
}
#GetMapping(value = "updateBook")
public ModelAndView updateBook(HttpServletRequest httpServletRequest) {
long id = Long.parseLong(httpServletRequest.getParameter("id"));
Book book = bookService.getBook(id);
ModelAndView modelAndView = new ModelAndView("addBook");
modelAndView.addObject("book", book);
return modelAndView;
}
#RequestMapping(value = "/saveBook",method = RequestMethod.POST)
public ModelAndView saveBook(#ModelAttribute Book book) {
if (book.getId() == 0) {
bookService.addBook(book);
} else {
bookService.updateBook(book.getId(), book);
}
return new ModelAndView("redirect:/allBooks");
}
#GetMapping(value = "/deleteBook")
public ModelAndView deleteBook(HttpServletRequest httpServletRequest) {
long id = Long.parseLong(httpServletRequest.getParameter("id"));
bookService.deleteBook(id);
return new ModelAndView("redirect:/allBooks");
}
}
My POM:
<?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>MyLibrary</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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-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>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Something</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
aplication.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/book?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
spring.thymeleaf.mode=LEGACYHTML5
There are a few things wrong with your demo project.
1. Incorrect package structure
You put your Runner class to a default package. It not a good idea, because in this case Spring Boot is not able to set a default package for component scanning. When you run your application you should see something like:
** WARNING ** : Your ApplicationContext is unlikely to start due to a #ComponentScan of the default package.
Solution: move all your classes to common package.
2. spring-boot-starter-thymeleaf is missing
In your pom.xml file add following dependency to make Thymeleaf templates working:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Then remove following dependency that is present in your pom.xml:
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
3. spring.thymeleaf.mode=LEGACYHTML5 requires additional dependency
When you run your app and you open http://localhost:8080 you will see following exception:
org.thymeleaf.exceptions.ConfigurationException: Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in "LEGACYHTML5" mode [http://nekohtml.sourceforge.net]. Maven spec: "net.sourceforge.nekohtml::nekohtml::1.9.15". IMPORTANT: DO NOT use versions of nekoHTML older than 1.9.15.
at org.thymeleaf.templateparser.html.AbstractHtmlTemplateParser.parseTemplate(AbstractHtmlTemplateParser.java:90) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:278) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
This is because you have specified:
spring.thymeleaf.mode=LEGACYHTML5
and you haven't add nekoHTML library to the classpath.
Solution: add following dependency to your pom.xml:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
After applying all these steps you will see "Hello" on your app's home page. Hope it helps.

Spring boot cannot compile views in 1.5.8 release

Environment:
STS-3.9.0.RELEASE
Java Version: 1.7
Spring Starter Project, web
I have a spring boot project that works fine with 1.4.2 release but not with 1.5.8. When I change to 1.5.8, it stops compiling JSP and prints the page as it is.
Here are my 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.educo</groupId>
<artifactId>sprboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>sprboot</name>
<description>Event tracker project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<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-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.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
src/main/java, com.educo.Application.java
package com.educo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
#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);
}
}
src/main/resources, application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
welcome.message: Hello
src/main/java, com.educo.controller
package com.educo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class TestController {
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping("/body")
#ResponseBody
public String body() {
System.out.println("there");
return "Hello world!";
}
// /param?name=ashutosh
#RequestMapping("/param")
#ResponseBody
public String params(#RequestParam String name) {
return "Hello " + name;
}
// /path/ashutosh
#RequestMapping("/path/{name}")
#ResponseBody
public String paths(#PathVariable String name) {
return "Hello " + name;
}
// /path-view/ashutosh
#RequestMapping("/path-view/{name}")
public String pathView(#PathVariable String name, ModelMap map) {
map.addAttribute("name", name);
return "path-view";
}
}
src/main/webapp, /WEB-INF/jsp/index.jsp
<b>Welcome</b> to my website
src/main/webapp, /WEB-INF/jsp/path-view.jsp
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"
%>
Hello ${name} by path view
On changing to:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
I get the error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Oct 29 23:58:06 EDT 2017
There was an unexpected error (type=Not Found, status=404).
No message available
No error gets printed on tomcat console.
I tried to read the changelog for 1.5.8 but could not find anything on this. What I'm missing here?

spring boot 1.3.5 form:errors not showing for jsp

I am upgrading a spring-mvc web app from spring 4.X to be a spring boot war.
The page serves, the form is posted, the validation is executed (and records an error) but the jsp does show any errors in form:errors
THe same jsp works fine outside of spring-boot.
To be sure I'm setting my spring boot jsp app correctly I've simply added a form post to the existing "spring-boot-sample-web-jsp" (see https://github.com/spring-projects/spring-boot/tree/1.3.x/spring-boot-samples )
Here is the model object
package sample.jsp;
import java.io.Serializable;
public class EmailHolderPageModel implements Serializable {
private String emailAddress;
public EmailHolderPageModel() {
super();
}
public EmailHolderPageModel(String emailAddress) {
super();
this.emailAddress = emailAddress;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
Here is the server side:
#Autowired
private EmailSaveValidator emailSaveValidator;
#RequestMapping("/saveEmail.html")
public ModelAndView processEmail(#ModelAttribute("myModel") EmailHolderPageModel pageModel, BindingResult result){
ModelAndView modelAndView = null;
emailSaveValidator.validate(pageModel, result);
if(result.hasErrors()){
modelAndView = new ModelAndView("enterEmail");
EmailHolderPageModel pm = new EmailHolderPageModel("");
modelAndView.addObject("myModel", pm);
System.err.println("!!!Failed Validation!!!");
} else {
modelAndView = new ModelAndView("thankyou");
ThankyouPageModel thankYoupageModel = new ThankyouPageModel();
modelAndView.addObject("thankyouModel", thankYoupageModel);
}
return modelAndView;
}
Here is the validator
#Component
public class EmailSaveValidator implements Validator {
public boolean supports(Class candidate) {
return EmailHolderPageModel.class.isAssignableFrom(candidate);
}
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "emailRequired", "required field");
}
}
Here is the jsp (truncated a little because stackoverflow is getting confused)
<%# page session="false"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<title>test entering email</title>
</head>
<body>
<form:form commandName="myModel" method="POST" action="saveEmail.html" >
<form:errors path="emailAddress" htmlEscape="false" />
<div id="formIntro">
<spring:message text="enter email address" />
<p><strong><label>
<spring:message text="email address:" /> </label><form:input path="emailAddress" size="35" maxlength="200"/>
</label>
</strong></p>
</div>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
The pom file is (unmodified from spring-boot-sample-web-jsp)
<?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>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.3.6.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-web-jsp</artifactId>
<packaging>war</packaging>
<name>Spring Boot Web JSP Sample</name>
<description>Spring Boot Web JSP Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
<dependencies>
<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.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
==============
And the solution is do not create a new model object on error (though works fine when not a spring boot app):
#RequestMapping("/saveEmail.html")
public ModelAndView processEmail(#ModelAttribute("myModel") EmailHolderPageModel pageModel, BindingResult result){
ModelAndView modelAndView = null;
emailSaveValidator.validate(pageModel, result);
if(result.hasErrors()){
modelAndView = new ModelAndView("enterEmail");
// !! SOLUTION !!
// DO NOT CREATE A NEW MODEL OBJECT
// !! SOLUTION !!
// EmailHolderPageModel pm = new EmailHolderPageModel("");
modelAndView.addObject("myModel", pageModel);
System.err.println("!!!Failed Validation!!!");
} else {
modelAndView = new ModelAndView("thankyou");
ThankyouPageModel thankYoupageModel = new ThankyouPageModel();
modelAndView.addObject("thankyouModel", thankYoupageModel);
}
return modelAndView;
}
You commandName should be exacty the name of your model class: eg.
public class EmailHolderPageModel{}...
And in your controller any function:
public ModelAndView anythink(#Valid EmailHolderPageModel email...)
So, in you jsp form should be:
<form:form commandName="emailHolderPageModel" />...

Resources